关于MySQL字段BLOB二进制下载,浏览器可另存为(chrome除外)

来源:互联网 发布:端口占用查询工具 编辑:程序博客网 时间:2024/06/11 16:22

1.页面使用 a 标签 利用struts2 跳转后台后台(本人是利用springmvc注释);

<a href=<s:url value="/procuratorate/documentFile/download" />?id='+data[i].fileId+'>下载</a>

2.后台方法

@ParentPackage("default")@Namespace("/procuratorate/documentFile")public class DocumentFileAction extends CRUDAction<NewDocumentFile, Long> {
@Action(value = "download")public void download() throws Exception {   NewDocumentFile byId = getService().findById(_id);   byte[] fileBody = byId.getFileBody();   if (null != fileBody && fileBody.length > 0) {      //Blob blob = Hibernate.createBlob(fileBody);      String realPath = ServletActionContext.getServletContext().getRealPath("/");//获取相对路径      File file = new File(realPath, "download");      if (!file.isDirectory()) {         file.mkdir();//新建download文件夹      }      FileOutputStream fileOutputStream = new FileOutputStream(file + "/" + byId.getDescript() + ".doc");//文件存放位置      fileOutputStream.write(fileBody);      fileOutputStream.close();      FileInputStream fileInputStream = null;      OutputStream outputStream = null;      try {         fileInputStream = new FileInputStream(file + "/" + byId.getDescript() + ".doc");         HttpServletResponse response = super.getResponse();         response.setCharacterEncoding("utf-8");         response.setContentType("application/msword");//文档类型:Word         response.addHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(byId.getDescript() + ".doc", "UTF-8"));// 设置浏览器 另存为路径选择 处理该文件名         outputStream = response.getOutputStream();         byte[] buffer = new byte[1024]; // 缓冲区         int bytesToRead;         while ((bytesToRead = fileInputStream.read(buffer)) != -1) {            outputStream.write(buffer, 0, bytesToRead);         }      } finally {         if (fileInputStream != null)            fileInputStream.close();         if (outputStream != null)            outputStream.close();         if (file != null) {            // 删除临时文件夹            String cmd = "cmd.exe /c rd /s /q "+file.getAbsolutePath();            Runtime.getRuntime().exec(cmd);         }      }   } else {//暂无   }}
}


--------------------------------------------------------------------------------------------------

3.以上代码优化

@Action(value = "download")public void download() throws Exception {   NewDocumentFile byId = getService().findById(_id);   byte[] fileBody = byId.getFileBody();   if (null != fileBody && fileBody.length > 0) {      OutputStream outputStream = null;      try {         HttpServletResponse response = super.getResponse();         response.setCharacterEncoding("utf-8");         response.setContentType("application/msword");//文档类型:Word         response.addHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(byId.getDescript() + ".doc", "UTF-8"));// 设置浏览器 另存为路径选择 处理该文件名         outputStream = response.getOutputStream();         outputStream.write(fileBody);      } finally {         if (outputStream != null)            outputStream.close();      }   }}

原创粉丝点击