base64文件下载
@RequestMapping(value = “download”)
public void download(String id,HttpServletRequest request,HttpServletResponse response){
OutputStream os = null;//
InputStream is = null;
ZipOutputStream zos = null;
try {
os = response.getOutputStream();
response.reset();
response.setContentType(“application/octet-stream”);
KhAttachment khAttachment = khAttachmentService.get(id);
// CreditReport report = reportService.getId(id);
if(khAttachment!=null){
String fileName = khAttachment.getName();
String file=System.getProperty(“java.io.tmpdir”)+File.separator+khAttachment.getId()+File.separator+khAttachment.getName();
boolean iscreateFile=FileUtils.createFile(file);
if(iscreateFile){
String base64DataStr=StringUtils.substringAfter(khAttachment.getContent(), “base64,”);
FileUtils.decoderBase64File(base64DataStr,file);
}
is = new FileInputStream(file);
// fileName = parseGBK(fileName);
if (request.getHeader(“User-Agent”).toUpperCase().indexOf(“MSIE”) >0){
fileName = URLEncoder.encode(fileName, “UTF-8”);//IE浏览器
}else{
fileName = new String(fileName.getBytes(“UTF-8”), “ISO8859-1”);//firefox浏览器
}
response.addHeader(“Content-Disposition”, “attachment; filename=\“”+ fileName+”\“”);
// report.setDownloadCount(report.getDownloadCount()+1);
// reportService.save(report);
// response.getWriter().print(“123123123”);
IOUtils.copy(is, os);
}
} catch (Exception e) {
e.printStackTrace();
// ResponseUtils.renderText(response,”“);
}finally{
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
IOUtils.closeQuietly(zos);
}
}
1,注意,
还没有评论,来说两句吧...