在实际项目中,如果文件过大,给数据库带来访问压力,因此,这里介绍,
当数据库中 存 文件在应用服务器上的路径时,文件的下载。
关于文件的上传部分,可以参考我的上一篇 博文。
文件的保存:
public void upload(User user,MultipartFile file,String number,HttpServletRequest request)
throws IOException, IllegalAccessException, InvocationTargetException{
Contract contract = this.getContract(Long.parseLong(number));
String fileName=file.getOriginalFilename();
InputStream fis = file.getInputStream();
//此处不再将合同原件 保存在contract中,而是上传到服务器某路径下
SysConstants sysconstants=new SysConstants(request);
String path= sysconstants.getUPLOAD_PATH()+"合同资料\\"+contract.getContractNumber()+"\\合同原件"+"\\";
//将相关信息存入 fileshare
FileShare fileShare=new FileShare();
fileShare.setFileName(fileName);
fileShare.setFilePath(path+fileName);
fileShare.setFileType("合同原件");
fileShare.setFileSize(file.getSize());
Long time = System.currentTimeMillis();//获取当前时间
Timestamp uploadTime = new Timestamp(time);
fileShare.setUploadTime(uploadTime);
fileShare.setUser(user);
fileShare.setContractId(contract.getId());
@SuppressWarnings("unchecked")
List<FileShare>list=(List<FileShare>) hibernateTemplate.find
("from FileShare where fileName=? and fileType=?",fileName,"合同原件");
if(list.size()>0){
if(!(new File(list.get(0).getFilePath()).exists()))
throw new IOException("文件名已存在");
}
if(file.getSize()>0){
fileshareService.SaveFileFromInputStream(file.getInputStream(),path,fileName);
hibernateTemplate.save(fileShare);
hibernateTemplate.flush();
//更新contract
contract.setContractStatus("合同已上传");
hibernateTemplate.update(contract);
hibernateTemplate.flush();
}
else{
throw new IOException("文件大小不能为0!");
}
// byte[] buffers = new byte[(int) file.getSize()];
// fis.read(buffers);
// Session session = hibernateTemplate.getSessionFactory().openSession();
// LobHelper lobHelper = session.getLobHelper();
// Blob blob = lobHelper.createBlob(buffers);
// contract.setContractFile(blob);
// contract.setContractStatus("合同已上传");
// hibernateTemplate.update(contract);
// hibernateTemplate.flush();
}
SaveFileFromInputStream函数:
public void SaveFileFromInputStream(InputStream stream,String path,String filename) throws IOException
{
File file = new File(path);
if (!file.exists()||!file .isDirectory()) {
// check file exists or not
file.mkdirs();
}
FileOutputStream fs=new FileOutputStream( path + "/"+ filename);
byte[] buffer =new byte[1024*1024];
int bytesum = 0;
int byteread = 0;
while ((byteread=stream.read(buffer))!=-1)
{
bytesum+=byteread;
fs.write(buffer,0,byteread);
fs.flush();
}
fs.close();
stream.close();
}
下载文件:
public void download(Long cid,HttpServletResponse response) throws IOException, SQLException {
List<FileShare> list=fileshareService.listAllContractId(cid);
FileShare fileShare=list.get(0);
response.setHeader("Content-Disposition", "attachment; filename="
+ URLEncoder.encode(fileShare.getFileName(), "UTF-8"));
InputStream stream;
//这里判断 fileShare中 是否存有文件的二进制,
//如果没有的话,则从文件在应用服务器中的绝对路径获取 stream
if(fileShare.getContent()!=null){
stream = fileShare.getContent().getBinaryStream();
}else{
stream = new FileInputStream(new File(fileShare.getFilePath()));
}
if(stream!=null){
ServletOutputStream fs= response.getOutputStream();
byte[] buffer =new byte[1024*1024];
int bytesum = 0;
int byteread = 0;
while ((byteread=stream.read(buffer))!=-1) {
bytesum+=byteread;
fs.write(buffer,0,byteread);
fs.flush();
}
fs.close();
}else{
//系统里面没有文件,请上传文件
this.hibernateTemplate.delete(fileShare);
this.hibernateTemplate.flush();
}
}