java.lang.IllegalStateException: getOutputStream() 已经为此响应调用
嗨,当我尝试通过我的 java 代码打开一个 pdf 文件时,我遇到了异常
这是我的Java代码。我正在使用 IText 创建 pdf
@RequestMapping(value = "/pdf", method = RequestMethod.POST)
public String pdf(ActivitiesForm activitiesForm, Model model,
HttpSession session,HttpServletResponse response) throws InstanceNotFoundException, IOException,
DocumentException {
String PATH = "/home/space/workspace/trainning/Trainning/Performance.pdf";
// step 1
Document document = new Document();
// step 2
FileOutputStream fos = new FileOutputStream(PATH);
PdfWriter.getInstance(document,fos);
// step 3
document.open();
// step 4
PdfPTable table;
table = new PdfPTable(3);
table.setWidthPercentage(100);
PdfPCell cell;
cell = new PdfPCell(new Phrase("Roll No"));
cell.setColspan(1);
// cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Name"));
cell.setColspan(1);
// cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Performance"));
cell.setColspan(1);
// cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
Integer count = rollService.getRollCount();
Integer countpts = creditptsService.getPtsCount();
if (countpts != null) {
for (Integer i = 1; i <= count; i++) {
Roll roll = rollService.getRollNoById(Long.parseLong(i
.toString()));
List<Creditpts> creditpts = creditptsService.getRollById(roll
.getId());
Integer performance = 0;
if (creditpts == null) {
cell = new PdfPCell(new Phrase("" + roll.getRollno()));
cell.setColspan(1);
// cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Phrase("" + roll.getStudentName()));
cell.setColspan(1);
// cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Phrase("0"));
cell.setColspan(1);
// cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
} else {
Iterator<Creditpts> creIterator = creditpts.iterator();
while (creIterator.hasNext()) {
Creditpts creditpt = creIterator.next();
performance += creditpt.getCrdpts();
}
cell = new PdfPCell(new Phrase("" + roll.getRollno()));
cell.setColspan(1);
// cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Phrase("" + roll.getStudentName()));
cell.setColspan(1);
// cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Phrase("" + performance));
cell.setColspan(1);
// cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
}
}
}
document.add(table);
document.close();
fos.close();
FileInputStream baos = new FileInputStream(PATH);
// set some response headers
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setContentType("application/pdf");
response.setContentLength((int) new File(PATH).length());
OutputStream os = response.getOutputStream();
byte buffer[] = new byte[8192];
int bytesRead, i;
while ((bytesRead = baos.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.flush();
os.close();
}
我已经粘贴了我的 pdf 创建和打开代码我在这里做错了什么我错过了一些东西......
这是完整的堆栈跟踪:
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:633)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:214)
at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:105)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
at org.apache.jasper.runtime.JspWriterImpl.write(JspWriterImpl.java:336)
at org.apache.jasper.runtime.JspWriterImpl.write(JspWriterImpl.java:353)
at org.apache.jsp.jsp.activity_jsp._jspService(activity_jsp.java:402)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:593)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:530)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:229)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1265)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1016)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:965)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:859)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:883)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:792)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:298)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)