流的方式下载附件
<!--
@author :daisy
@date : 2011-12-04
@note : 从数据库中读取BLOB图片显示
-->
<%@page import="com.cwai.dao.DBManager"%>
<%@ page contentType=" text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
out.clear();
out = pageContext.pushBody();
//读取blob字段的图片内容,将其显示
String xh = request.getParameter("xh");
String wjm = request.getParameter("wjm");
wjm = java.net.URLEncoder.encode(wjm, "UTF-8");
try {
ServletOutputStream sos = null;
String sql = "SELECT WJNR WJNR FROM FJ_SWYB WHERE XH = '" + xh+"'";
System.out.println(sql);
byte[] blob_array = DBManager.getBlob(sql, null);
try {
//修改前
//response.setContentType("multipart/form-data");
//修改后
response.setContentType("multipart/form-data");
String kzm = wjm.substring(wjm.lastIndexOf(".")+1);
if("PDF".equals(kzm.toUpperCase()) ){
response.setContentType("application/pdf");
}
String downFileName = new String(wjm.getBytes("GB2312"),"iso8859-1");
//注意:attachment \inline 两者的区别
response.setHeader("Content-Disposition", "attachment;filename=\""+downFileName+"\"");
sos = response.getOutputStream();
System.out.println("blob_array-lenth:----" + blob_array.length);
sos.write(blob_array);
sos.flush();
sos.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sos != null) sos.close();
}
return;
} catch (Exception ex) {
ex.printStackTrace();
}
response.sendError(404);
%>
本文介绍了一种使用流的方式从数据库中读取并下载BLOB类型的附件,如图片或PDF文件的方法。通过设置响应头和内容类型,可以实现不同文件格式的正确下载。
pdf文件无法打开的解决方案&spm=1001.2101.3001.5002&articleId=102130275&d=1&t=3&u=f59ed92cabe14e78b2a42d620b593434)
4200

被折叠的 条评论
为什么被折叠?



