在转发之前,您已经向客户端发送了一些内容时会发生此异常。
我正在使用Servlets下载文件。看一看。
public class FileDownloadServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
byte[] b = null;
ServletOutputStream sop = null;
try {
response.setContentType("application/pdf");
response.setHeader("Content-Disposition","attachment; filename=yourFileName.pdf");
sop = response.getOutputStream();
b = myService.getFileData(); /* your code */
sop.write(b);
return;
} catch (Exception e) {
/* some code*/
}
finally{
/* some code*/
}
}
}
为什么不使用流结果和输入流更干净和更好? –
我以为流结果只用于文件下载。它可以用来在页面上呈现PDF流吗? –
流结果用于打开输入流,我相信这可以使用上述结果类型 –