【问题标题】:Invoking a servlet from a java class从 java 类调用 servlet
【发布时间】:2012-07-05 07:41:06
【问题描述】:

我已经制作(或至少尝试制作)一个将 JasperPrint 对象转换为 PDF 和 还会在新选项卡中打开此 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());

我做错了什么?

【问题讨论】:

  • 那么当你从 java 调用它时会发生什么?你有什么错误吗?您可以发布堆栈跟踪吗?
  • 不,我没有收到任何错误。这真的很奇怪,但我找到了解决方案并在下面的答案中分享了它。无论如何感谢您的回答

标签: java servlets jasper-reports pdf-generation


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2012-11-12
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    相关资源
    最近更新 更多