我已经(或者至少试图制作)一个将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代码:
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());
我做错了什么?