java - 如何在 servlet 中处理多个文档打开请求

标签 java servlets tomcat6

我正在使用 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/

相关文章:

Java Swing Gridbag 面板不会 colspan

java - 在测试 servlet 时在我的浏览器输出中获取字符串

java - 只要 ServerSocket 空闲,Linux 上的 Tomcat6 就会使用 100% 的 CPU

tomcat - 使用来自 Docker 的 tomcat 图像时具有自定义 catalina.sh

java - RAD - Websphere 应用程序服务器无法启动

java - Keycloak Java 适配器 - 检索角色列表

Java: 无法找到或加载主类 Main

java - facebook webhook 验证失败

java - 使用 Java 以编程方式重写 URL

jquery - 设置基于 jquery 移动网络应用程序的 xmpp 聊天服务器