CSDN没有高手吗?Tomcat406,通过servlet获取数据库中的Blob,返回给客户端出错!如果解决了400分全是你的!
对于图片,TXT等IE可以显示的文件下载没有任何问题。
对于像Word,PDF等弹出保存窗口的文件则会出现如上的错误,但是通过FlashGet又可以下载。
不知道是什么原因。
错误信息:
2003-12-30 00:14:48 Connection reset by peer: socket write error
2003-12-30 00:14:50 Software caused connection abort: socket write error
部分代码:
response.setHeader("Cache-Control","no-store"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader("Expires",0);
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = "select t.* " +
"from table_name " +
"where guid = '" + guid+ "' " +
"order by step desc";
final int BUFFER_SIZE = 8024;
try {
conn = Conn.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if(rs.next()) {
Blob b = rs.getBlob("documentcontent");
if(b != null) {
String filename = rs.getString("documentfilename");
String contentType = (String)(MimeTypes.get(StringUtils.substringAfterLast(filename, ".").toLowerCase()));
if(contentType == null) {
contentType = "application/octet-stream";
}
response.setHeader("Content-Type", contentType);
response.setHeader("Content-Disposition", "inline,filename=" + filename);
response.setHeader("Content-Length", String.valueOf(b.length()));
response.setHeader("Last-Modified", new java.util.Date().toString());
BufferedInputStream bis = new BufferedInputStream(b.getBinaryStream());
ServletOutputStream sos = response.getOutputStream();
byte buf[] = new byte[BUFFER_SIZE];
int c = 0;
while((c = bis.read(buf)) != -1) {
sos.write(buf, 0, c);
sos.flush();
}
bis.close();
sos.flush();
sos.close();
}
}
rs.close();
log.debug("下载文件结束");
}
catch(SQLException ex1) {
log.error("下载文件时错误。SQL="+sql, ex1);
throw new RuntimeException();
}
catch(IOException ex2) {
log.error("下载文件时错误。", ex2);
throw new RuntimeException();
}
finally {
if(stmt != null) { try { stmt.close(); } catch(Exception e){} }
if(conn != null) { try { conn.close(); } catch(Exception e){} }
}