|
String fileIdStr = request.getParameter("fileId");
if(StringUtils.isBlank(fileIdStr)){
response.sendRedirect(request.getContextPath()+"/pages/client/error.jsp");
return;
}
Long fileId = Long.parseLong(fileIdStr);
DevApplicationService fileService = (DevApplicationService)Application.lookupBean(DevApplicationServiceImpl.BEAN_NAME);
File tmpFile = (File) fileService.get(File.class,fileId);
response.setContentType(tmpFile.getContentType());
response.setHeader("Content-Disposition", "attachment; filename=" +new String(tmpFile.getFileName().replace(' ','_').getBytes("gb2312"),"iso8859-1"));
try {
String filePath = tmpFile.getPath();
FileInputStream inputStream = null;
if (!StringUtils.isEmpty(filePath)) {
inputStream = new FileInputStream(FileUtil.REPOSITORY_PATH+java.io.File.separator + filePath);
}
String fileName = FilenameUtils.getName(filePath);
fileName = new String(fileName.getBytes("GBK"), "ISO8859_1");
response.addHeader("Content-Disposition",
"attachment; filename=" + fileName);
response.setContentType("application/file");
BufferedInputStream fif = new BufferedInputStream(inputStream);
ServletOutputStream outputStream = response.getOutputStream();
int ch;
while ((ch = fif.read()) != -1) {
outputStream.write(ch);
}
outputStream.flush();
outputStream.close();
inputStream.close();
} catch (Exception ex) {
String simplename = ex.getClass().getSimpleName();
if(!"ClientAbortException".equals(simplename)){
ex.printStackTrace();
}
}finally{
out.clear();
out = pageContext.pushBody();
} |
|