2

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"
                        />

このコードで何を変更する必要があるかを提案してください。

4

4 に答える 4

1

このエラーが発生する理由はいくつかあります。

  • サーバーが通信を試みている間にネットワークがドロップします。
  • ユーザーがリクエストをキャンセルします。
  • データベースがダウンしているか、接続を拒否しています。

あなたがする必要があるのは、この順序で閉じることです:

finally {
      if(bos != null)
         bos.close();
      if(bis != null)
         bis.close();
      if(bais != null)
         bais.close();
}

お役に立てれば!

于 2013-10-28T20:21:37.270 に答える
1

ここでの回答を見ると、クロージングの順序が問題を引き起こしている可能性があるようです。

このコードを変更...

finally {
     if (bais != null)
         bais.close();

         if (bis != null)
             bis.close();

         if (bos != null)
             bos.close();
 }

に...

finally {
         bais.close();
         bos.close();
         bis.close();
 }
于 2013-10-28T11:29:45.463 に答える
1

byte[] docContentBytes 内にすべてのファイルコンテンツが既にメモリ内にロードされているのに、なぜバッファリングが必要なのですか?

ただし、Tomcat とブラウザ/クライアント間の通信エラーが原因で例外が発生した場合は、バッファリングを削除して content-length を設定してみてください。

それ以外の場合、Tomcat と SQL サーバーの間の通信エラーが原因で例外が発生した場合、問題は次のいずれかにあります。

  • dbクラスの実装
  • Documents実装
  • Microsoft SQL サーバーのインストールでの設定/チューニング

ただし、これはこのサーブレットをコーディングする方法です。

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException
{
    Database db = Database.getInstance();

    String docCode = request.getParameter("docCode");
    if(docCode == null || docCode.isEmpty()) throw new IllegalArgumentException("docCode is null");

    Documents document = db.getDocument(docCode);
    if(document == null) throw new IllegalStateException("invalid docCode: " + docCode);

    byte[] docContentBytes = document.getDocContentBytes();
    if(docContentBytes == null) throw new IllegalStateException("document " + docCode + " has no content");

    String contentType = document.getDocMimeType();
    if(contentType == null) contentType = "application/octet-stream";

    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");
    response.setContentType(contentType);
    response.setContentLength(docContentBytes.length);

    response.getOutputStream().write(docContentBytes);
}
于 2013-11-03T11:10:22.970 に答える