我正在使用我的reportService類生成包含我的報告的JasperPrint對象,然後將它發送給Servlet並生成PDF。問題是這個servlet沒有在新標籤中打開PDF(這是我想要的),實際上它甚至不會提示我下載它或任何東西。使用flex + servlet + jasper在新瀏覽器選項卡中顯示PDF的問題
Servlet的來電:從我的Servlet
try {
URL url = new URL("http://" + serverName + ":" + serverPort + path
+ "/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());
//This "jasperPrint" is my generated report from my service
out.writeObject(jasperPrint);
out.close();
connection.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
我doPost方法:
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
JasperPrint jasperPrint = null;
ObjectInputStream resultStream = null;
ServletOutputStream out = response.getOutputStream();
try {
resultStream = new ObjectInputStream(request.getInputStream());
jasperPrint = (JasperPrint) resultStream.readObject();
resultStream.close();
byte[] rel = JasperExportManager.exportReportToPdf(jasperPrint);
out.write(rel,0, rel.length);
//JasperExportManager.exportReportToPdfStream(jasperPrint, out);
response.setContentLength(rel.length);
response.setContentType("application/pdf");
response.setHeader("Content-Disposition",
"attachment; filename=\"report.pdf\"");
response.setHeader("Cache-Control", "no-cache");
System.err.println(rel.length);
} catch (JRException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
out.flush();
out.close();
}
}
我在做什麼錯?
那麼它是做什麼的?它是否在瀏覽器窗口中顯示PDF?或者它是空白的?你有錯誤嗎? – 2012-07-06 20:43:16
它什麼也沒做。我做了一些研究,發現響應總是發送給調用者(在這個特殊情況下是HttpURLConnection)。有人能證實這樣的事情嗎? – 2012-07-06 20:54:25
請問您爲什麼不在報告服務中完成所有工作?我們處理它的方式是我們從我們的服務中返回一個字節[],然後在彈性方面我們處理打開或者提示他們保存。 – 2012-07-06 21:03:01