📄 affix.java
字号:
package com.t60.oa.common.uploadfiles;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;
import com.t60.oa.common.uploadfiles.AffixForm;
import com.t60.oa.po.Accessories;
import hong.javanet.dao.HibernateUtil;
import org.bsf.tools.guid.GUID;
/**
*
* <p>Title: </p>
*
* <p>Description: </p>
* 上传附件类
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Affix {
private AffixForm form;
private Date d=new Date();
private String affixFilePath;// 上传文件路径
private GUID guid;
private static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
public void save() throws IOException {
if(form.getFile1()!=null && form.getFile1().getFileName().trim().length()>0)
{
GUID guid=new GUID();//生成全球唯一ID
try {
FileOutputStream fos = new FileOutputStream(affixFilePath + guid);
fos.write(form.getFile1().getFileData());
fos.flush();
fos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
Accessories acc=new Accessories();
acc.setId(guid.toString());
acc.setCreateDate(new java.sql.Timestamp(d.getTime()));
acc.setSize(new Integer(form.getFile1().getFileSize()));
acc.setName(form.getFile1().getFileName());
HibernateUtil.currentSession().save(acc);
this.setGuid(guid);
}
if(form.getFile2()!=null && form.getFile2().getFileName().trim().length()>0)
{
GUID guid=new GUID();
try {
FileOutputStream fos = new FileOutputStream(affixFilePath +
guid);
fos.write(form.getFile2().getFileData());
fos.flush();
fos.close();
} catch (Exception ex1) {
ex1.printStackTrace();
}
Accessories acc=new Accessories();
acc.setId(guid.toString());
acc.setCreateDate(new java.sql.Timestamp(d.getTime()));
acc.setName(form.getFile2().getFileName());
acc.setSize(new Integer(form.getFile2().getFileSize()));
HibernateUtil.currentSession().save(acc);
}
}
//下载附件,带三个参数,一个是File,一个是HttpServletResponse
public void downLoadAffix(String accId,String affixFilePath, HttpServletResponse response) throws
FileNotFoundException, IOException {
File file=new File(affixFilePath+accId);
String filename="";
response.setContentType("application/x-msdownload");
response.setContentLength((int) file.length());
Accessories acc=(Accessories) HibernateUtil.currentSession().load(Accessories.class,accId);
filename=acc.getName();//取得附件名
response.setHeader("Content-Disposition","attachment;filename="+new String(filename.getBytes(), "ISO-8859-1"));
FileInputStream fis = new FileInputStream(file);
BufferedInputStream fbis = new BufferedInputStream(fis);
byte abyte0[] = new byte[1024];
int k = 0;
OutputStream out = response.getOutputStream();
while ((long) k < file.length()) {
int j = fbis.read(abyte0, 0, 1024);
k += j;
out.write(abyte0, 0, j);
}
out.flush();
}
public void delAffix(){
}
public AffixForm getForm() {
return form;
}
public String getAffixFilePath() {
return affixFilePath;
}
public GUID getGuid() {
return guid;
}
public void setForm(AffixForm form) {
this.form = form;
}
public void setAffixFilePath(String affixFilePath) {
this.affixFilePath = affixFilePath;
}
public void setGuid(GUID guid) {
this.guid = guid;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -