从另一台服务器读取放置在一台服务器上的pdf文件

时间:2015-03-11 11:47:58

标签: java

我在生产环境中有两个节点。我已将pdf文件放在一台服务器上,并希望从两台服务器上读取它。什么时候打电话'文件'方法直接pdf在浏览器中显示,但是当我打电话给“pdfFiles”时,#pdfFiles'浏览器中没有显示任何内容。

public Resolution file(){
    try {
        final HttpServletRequest request = getContext().getRequest();
        String fileName = (String) request.getParameter("file");

        File file = new File("pdf file directory ex /root/pdffiles/" + fileName);

        getContext().getResponse().setContentType("application/pdf");
        getContext().getResponse().addHeader("Content-Disposition",
                "inline; filename=" + fileName);
        FileInputStream streamIn = new FileInputStream(file);

        BufferedInputStream buf = new BufferedInputStream(streamIn);
        int readBytes = 0;
        ServletOutputStream stream = getContext().getResponse().getOutputStream();
        // read from the file; write to the ServletOutputStream
        while ((readBytes = buf.read()) != -1)
            stream.write(readBytes);

    } catch (Exception exc) {
        LOGGER.logError("reports", exc);
    }
    return null;
}

public Resolution pdfFile() {
    final HttpServletRequest request = getContext().getRequest();
    final HttpClient client = new HttpClient();
    try {
        String fileName = (String) request.getParameter("file");
        final String url = "http://" + serverNameNode1 //having pdf files
                + "/test/sm.action?reports&file=" + fileName;

        final PostMethod method = new PostMethod(url);

        try {
            client.executeMethod(method);
        } finally {
            method.releaseConnection();
        }

    } catch (final Exception e) {
        LOGGER.logError("pdfReports", "error occured2 " + e.getMessage());
    }
    return null;
}

1 个答案:

答案 0 :(得分:0)

在client.executeMethod(方法)之后的代码部分下面;'在' pdfFile()'方法,它适用于我。

buf = new BufferedInputStream(method.getResponseBodyAsStream());
            int readBytes = 0;
            stream = getContext().getResponse().getOutputStream();
            // write to the ServletOutputStream
            while ((readBytes = buf.read()) != -1)
                stream.write(readBytes);