关于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(); } }}
阅读全文
0 0
- 关于MySQL字段BLOB二进制下载,浏览器可另存为(chrome除外)
- MySql 二进制相关(Blob)
- 关于 向Mysql 插入 Blob字段 转
- C、C++写二进制数据到mysql的Blob字段
- C语言写二进制数据到mysql的Blob字段
- C、C++写二进制数据到mysql的Blob字段
- python往mysql的blob字段写入二进制数据
- C语言写二进制数据到mysql的Blob字段
- Navicat for MySQL 查看BLOB字段内容和c++读写MySQL blob二进制数据
- SQLITE3 读写二进制字段blob
- mysql操作BLOB字段
- mysql blob字段导入
- 各大浏览器实现当前网页下载功能(当前加载时自动弹出另存为窗口,支持Firefox,Chrome,IE等)
- C、C++写二进制数据到mysql的Blob字段/插入images
- C++ 读取图像(二进制)保存到mysql blob
- C#读取Mysql blob字段
- C#读取Mysql blob字段
- Java 操作MySql Blob 字段
- 大富豪3.4端IOS11修复教程
- unicode Entities for Symbols and Greek Letters
- Android MQTT使用详解
- ORA-12514: TNS:listener does not currently know of service requested in connect descri
- 利用镜面反射让游戏闪耀起来
- 关于MySQL字段BLOB二进制下载,浏览器可另存为(chrome除外)
- Problem L 求m到n之和 C语言函数
- 开放封闭原则
- 火焰图
- java-super关键字
- 22、TensorFlow 实现双隐层分类器
- 简单的Fresco加载图片
- JAVA中IO流
- 早安1128