我正在使用 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 服务器收到损坏的管道错误。
我的 DataSource 实现如下。
<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"
/>
任何人都可以建议我在这段代码中需要修改什么?