从java类调用servlet

时间:2012-07-04 20:41:16

标签: java servlets jasper-reports pdf-generation

我已经(或者至少试图制作)一个将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());

我做错了什么?

1 个答案:

答案 0 :(得分:0)

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

相关问题