从java类调用servlet
我已经制作(或者至少尝试制作)了一个servlet,用于将JasperPrint对象转换为PDF和 还将在新选项卡中打开此PDF。但我的代码调用程序似乎没有调用我的servlet,也没有抛出任何异常 当我直接从浏览器调用URL时,它确实调用servlet,但在我的java类中没有发生同样的情况 调用程序代码:从java类调用servlet,java,servlets,jasper-reports,pdf-generation,Java,Servlets,Jasper Reports,Pdf Generation,我已经制作(或者至少尝试制作)了一个servlet,用于将JasperPrint对象转换为PDF和 还将在新选项卡中打开此PDF。但我的代码调用程序似乎没有调用我的servlet,也没有抛出任何异常 当我直接从浏览器调用URL时,它确实调用servlet,但在我的java类中没有发生同样的情况 调用程序代码: URL url = new URL("http://localhost:8080/app/reportgenerator"); HttpURLConnection connection =
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());
我做错了什么?经过大量研究,我找到了解决办法。这就是我解决问题的方法:那么从java调用时会发生什么?你有什么错误吗?你能发布堆栈跟踪吗?不,我没有收到任何错误。这真的很奇怪,但我找到了解决方案,并在下面的答案中分享了它。谢谢你的回答,不管怎样可能是重复的