关于MySQL字段BLOB二进制下载,浏览器可另存为(chrome除外)
2017-11-28 09:22
337 查看
1.页面使用 a 标签 利用struts2 跳转后台后台(本人是利用springmvc注释);
2.后台方法
--------------------------------------------------------------------------------------------------
3.以上代码优化
<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(); } } }
相关文章推荐
- 各大浏览器实现当前网页下载功能(当前加载时自动弹出另存为窗口,支持Firefox,Chrome,IE等)
- 关于7.0去掉Chrome后,平台默认浏览器报错webview not installed的问题
- [转]Chrome浏览器的离线安装包下载地址
- 关于chrome外部协议请求,启动外部程序,itms,macappstore下载相关
- 关于有的手机浏览器下载APK时会成TXT
- 关于ajax的下载文件被浏览器阻止的问题
- chrome 浏览器中关于media player11插件的安装
- chromedriver下载地址 和其他浏览器驱动下载
- Selenium 设置浏览器下载 Firefox 和Chrome
- 关于浏览器检测文件下载
- 关于ajax的下载被浏览器阻止的问题
- 关于浏览器并发数的那些事儿——跨主机名并行下载【译】
- 求解!!关于ajax传值却自动打开浏览器下载的问题
- Chrome 浏览器 - APK 下载
- 关于IE等浏览器在下载JAR文件时的,将后缀更改为zip等问题及解决方案
- 铬合金浏览器(Goolge Chrome)即将现身 周二开放下载
- 关于IE浏览器缓存问题研究和chrome,firefox,浏览器隐私模式的体会。
- 关于chrome 浏览器 iframe里无法执行alert,comfirm 等js方法
- 关于利用浏览器下载网络视频文件的问题总结
- Google Chrome Source Code 浏览器源码(下载)