0

我已经制作(或至少尝试制作)一个将 JasperPrint 对象转换为 PDF 并在新选项卡中打开此 PDF 的 servlet。但似乎我的代码调用程序没有调用我的 servlet,也没有抛出任何异常。

当我直接从浏览器调用 URL 时,它确实调用了 servlet,但在我的 java 类中不会发生同样的情况。

调用者代码:

URL url = new URL("http://localhost:8080/app/reportgenerator");
HttpURLConnection connection =  (HttpURLConnection)url.openConnection(); 

connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setDefaultUseCaches (false);
connection.setRequestProperty("Content-Type", "application/octet-stream");

ObjectOutputStream out = new ObjectOutputStream(connection.getOutputStream());
JasperPrint jasperPrint = new JasperPrint();
out.writeObject(jasperPrint);           
out.close();

小服务程序代码:

response.setContentType("application/pdf");
response.setHeader("Content-Disposition","attachment; filename=\"report.pdf\"");

JasperPrint jasperPrint = null;

try {
    ObjectInputStream resultStream = new ObjectInputStream(request.getInputStream());
    jasperPrint = (JasperPrint) resultStream.readObject();
    resultStream.close();

    JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());

我究竟做错了什么?

4

1 回答 1

0

经过更多的研究,我找到了解决方案。这就是我解决问题的方法:Calling a Servlet from a Java application

于 2012-07-05T12:13:05.117 回答