我已经制作(或至少尝试制作)一个将JasperPrint对象转换为PDF的servlet,并且还会在新选项卡中打开此 PDF。但似乎我的代码调用程序没有调用我的 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();
Servlet Code:
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());
我做错了什么?
经过更多的研究,我找到了解决方案。这就是我解决问题的方式:从 Java 应用程序调用 Servlet