现在有一个项目使用SSH框架的 写邮件要添加附件,读邮件如果有附近可以下载。
怎么才能上传一个文件,然后使用HIBERNATE存储到数据库,
然后读取的时候,从数据库下载呢
struts就支持上传下载。。。。我以前也做过邮件系统。。
但是附件一般都不存数据库的,数据库只是存附件路径
private String fileDesc;
private File file;
private String fileFileName;// 命名要规范,前面是所上传文件后面是FileName,表示上传文件名
private String fileContentType;// 同上上传文件类型
//getter and setter
public String execute() {
// 取到服务器上传文件存放的路径
String path = ServletActionContext.getServletContext().getRealPath(
"/Files/");
// 取到上传文件的完整路径
String FilePath = path + File.separator + fileFileName;
InputStream is = null;
OutputStream os = null;
try {
is=new FileInputStream(file);
os=new FileOutputStream(FilePath);
byte[] b=new byte[1024];
int len=0;
while((len=is.read(b))!=-1){
os.write(b,0,len);
os.flush();
}
is.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
一般的附件或者图片的处理方案:
1、可以把附件存到服务器上,然后把路径存到数据库。下次从数据库拿到指定路径的文件即可。
2、以二进制直接把文件存到数据库。
用jspsmartupload实现将文件上传到服务器
//定义SmartUpload对象
SmartUpload upload = new SmartUpload();
//初始化
upload.initialize(this.getServlet(), request, response);
//上传文件
upload.upload();