【发布时间】:2013-05-07 05:54:57
【问题描述】:
这是我的代码,数组中的 fileByte,其中包含 PDF 的内容。 但是,该文件已损坏,我无法从中读取。这里有什么问题?谢谢!
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
response.addHeader("Content-Disposition", "attachment;filename=test.pdf");
ServletOutputStream outputStream = response.getOutputStream();
response.setContentType("application/pdf");
response.setContentLength(fileByte.length);
outputStream.write(fileByte);
outputStream.flush();
outputStream.close();
【问题讨论】:
-
这里没有错。你确定
fileByte是正确的吗?尝试将其写入文件。 -
我创建了一个文件,然后将文件转换为 byteArray 以便发送。你能告诉我如何直接做吗?我使用 iText pdf 渲染器来创建一个 pdf 文件。如果我用渲染器保存文件,我可以毫无问题地打开它。
-
只写
FileOutputStream fos = new FileOutputStream(new File(filename)); fos.write(fileByte); fos.close();。检查文件,我想它会和你下载的一样。
标签: java servlets outputstream