gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:04:42 25 4
gpt4 key购买 nike

我在生产环境中有两个节点。我已将 pdf 文件放置在一台服务器上,并想从两台服务器上读取它。当我直接调用“file”方法时,pdf会显示在浏览器中,但是当我调用“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;
}

最佳答案

在“client.executeMethod(method);”之后包含以下部分代码在 '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);

关于java - 从一台服务器读取放置在另一台服务器上的 pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28985842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com