0

我正在尝试通过使用 http 客户端打开连接,然后读取输入流并写入输出流。(是的,这个 url 只有我们的 intarnet 才能访问 :) )

该代码适用于

  1. 在我的 windowsxp 盒子上作为独立的 java 客户端。
  2. 在作为 war 文件的一部分在 windowsxp 上运行的 Jboss 7.1.2 上。
  3. 在我们的 linux 机器上作为独立的 java 客户端。

但是此代码无法正确读取 PDF

  1. 在 Jboss 7.1.2 上作为 war 文件的一部分在 linux 上运行。

在成功的情况下,当我从给定的 url 读取 pdf 并打印大小时,我看到我们得到了大约36289 bytes

如果在 Linux 上的 jboss 上运行任何 pdf 文档的情况 2,无论大小如何,我都只读取了970 字节,其余部分没有被读取。美妙的是,任何地方都没有例外。显然保存的 pdf 无法打开,因为 pdf 内容没有完全读取。

这是从 inputStream (类型为 )读取数据的代码

    protected void streamPdfOut(InputStream in, HttpServletResponse resp)
                throws IOException, FileNotFoundException {

            ServletOutputStream out = resp.getOutputStream();
            resp.reset();
            resp.setContentType("application/pdf");
            resp.setHeader("Content-disposition", "filename=List.pdf");
            resp.setDateHeader("Expires", 0); //prevents caching at the proxy server
            LOGGER.debug("InputStream type = "+ in);
            //LOGGER.debug(" Loaded from :"+ in.getClass().getProtectionDomain().getCodeSource());
            byte buff[] = IOUtils.toByteArray(in);
            LOGGER.debug(" IOUtils Number of bytes read count ="+ buff.length);
            out.write(buff, 0, buff.length);
            LOGGER.info("bytesReadTotal [" + buff.length + "]");
            // clean up
            out.flush();
            resp.flushBuffer();
            out.close();
            //close input stream and release any system resources.
            in.close();
        }

注意: 输入流是使用 Apache HTTPclient 打开的(我这里没有复制代码)

LOG 输出(在 JBOSS 7.1.2 上运行的 windows xp 上): DEBUG PDFServletHelperImpl:80 InputStream type = org.apache.http.conn.EofSensorInputStream@34529788 DEBUG PDFServletHelperImpl:83 IOUtils Number of bytes read count =36289 INFO PDFServletHelperImpl:85 bytesReadTotal [ 36289]

LOG 输出(在 JBOSS 7.1.2 上运行的 Linux 上):
DEBUG PDFServletHelperImpl:80 InputStream type = org.apache.http.conn.EofSensorInputStream@ab16d7 DEBUG PDFServletHelperImpl:83 IOUtils Number of bytes read count =970 INFO PDFServletHelperImpl:85 bytesReadTotal [970 ]

为了确保正在加载的类在我本地的 jboss 和 jboss 的 linux 盒子上没有不同,我打印了加载类的路径,并且在我们的 webapp 的 linux 和 windows 版本上,我看到了该类“org.apache.http.conn.EofSensorInputStream”是从war文件中的httpclient-4.1.jar加载的。

当我在 Linux 版本的 jboss 7.1.2 上运行代码时,有什么建议限制应用程序仅读取 970 字节而不是整个 pdf 流?

4

0 回答 0