我正在使用 servlet,它用于打开文档,如 doc、txt、pdf、ppt 等。
我的代码片段如下。
Documents document = db.getDocument(docCode);
String contentType = document.getDocMimeType();
byte[] docContentBytes = document.getDocContentBytes();
ServletOutputStream out = response.getOutputStream ();
response.setHeader("X-UA-Compatible", "IE=8");
response.setHeader("Content-disposition", "attachment;filename=\"Document\"");
response.setHeader("Pragma","private");
response.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
response.setHeader("Content-Transfer-Encoding","binary");
if(contentType!=null){
response.setContentType(contentType);
}else{
response.setContentType("application/pdf");
}
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
ByteArrayInputStream bais = null;
if(docContentBytes != null) {
try{
bais = new ByteArrayInputStream(docContentBytes);
bis = new BufferedInputStream(bais);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
// Simple read/write loop.
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
}catch(final MalformedURLException e) {
System.out.println ( "MalformedURLException." );
throw e;
} catch(final IOException e) {
System.out.println ( "IOException." );
throw e;
} finally {
if (bais != null)
bais.close();
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}
现在,当我尝试打开多个文档时,一段时间后我会从 tomcat 服务器收到损坏的管道错误。
我的数据源实现如下。
<Resource name="jdbc/TEST_DS"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://hostName;databaseName=TEST"
username="test"
password="testPwd"
maxPoolSize="50"
removeAbandoned="true"
removeAbandonedTimeout="1000"
logAbandoned="true"
/>
任何人都可以建议我需要在这段代码中修改什么吗?
最佳答案
从答案来看here看起来可能是关闭命令造成了这个问题。
更改此代码...
finally {
if (bais != null)
bais.close();
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
致...
finally {
bais.close();
bos.close();
bis.close();
}
关于java - 如何在 servlet 中处理多个文档打开请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19310750/