关于java中的流的问题

发布于 2024-11-11 14:27:02 字数 191 浏览 5 评论 0原文

我们有以下要求。

我们必须创建一个 excel/pdf 报告,然后通过单击 java web 应用程序中的按钮来下载它。 pdf/excel 文件是使用应用程序数据动态创建的。

我们不应在服务器上创建任何物理文件。

我们该怎么做呢?是否有任何流可以让我同时读取和写入,而不必在中间关闭。

We have the below requirement.

We will have to create an excel/pdf report and then download it on click of a button in a java web application. The pdf/excel file is dynamically created using application data.

We should not create any physical file on the server.

How do we go about this? Are there any streams through which I can read and write in the same go without having to close in between.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浮华 2024-11-18 14:27:02

您可以使用基于内存的流(例如 ByteArrayInputStream 和 ByteArrayOutputStream )并使用相同的底层字节缓冲区来解决问题的同一部分中的读/写问题。

正如其他人指出的那样,您可以直接写入响应的输出流。

You could use memory-based streams (such as ByteArrayInputStream and ByteArrayOutputStream) and use the same underlying byte buffer to address the read/write in the same go part of the question.

As others have pointed out, you can just write directly to the output stream of the response.

開玄 2024-11-18 14:27:02

看看 ServletResponse.getOutputStream( )

您需要从报告 API 创建的流写入此流。不要忘记使用同一类的 setContentType() 方法设置正确的 content-type

Look at ServletResponse.getOutputStream().

You need to write to this stream from the one created by your report API. Don't forget to set the proper content-type using setContentType() method of the same class.

自此以后,行同陌路 2024-11-18 14:27:02

在这里你可以找到如何使用 jxl API 来做到这一点,它也可能对你有帮助。
如何从 Servlet 输出 Excel 文件?

Here you can find how you can do it with jxl API and it may help you also.
How do I output an Excel file from a Servlet?

冬天旳寂寞 2024-11-18 14:27:02

无论您使用什么 PDF 或 Excel API 来生成文件,您都应该查找采用 OutputStream 来写入生成的 PDF/Excel 内容的构造函数或方法。您应该只使用 response.getOutputStream() 而不是 FileOutputStream 来提供它。

例如,用于 PDF 的 iText:

response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
PdfWriter pdfWriter = PdfWriter.getInstance(document, response.getOutputStream());
// ...

以及用于 Excel 的 Apache POI:

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
WritableWorkbook workBook = Workbook.createWorkbook(response.getOutputStream());
// ...

Whatever PDF or Excel API you are using to generate the files, you should lookup the constructor or method which takes an OutputStream to write the generated PDF/Excel content to. You should just feed it with response.getOutputStream() instead of FileOutputStream.

For example, iText for PDFs:

response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
PdfWriter pdfWriter = PdfWriter.getInstance(document, response.getOutputStream());
// ...

And Apache POI for Excel:

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
WritableWorkbook workBook = Workbook.createWorkbook(response.getOutputStream());
// ...
遗弃M 2024-11-18 14:27:02

让 Servlet 将 pdf/excel 文件作为字节数组提供服务。

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    
    byte[] bytes = null; // get this from somewhere in your app
    String fileName = "filename.pdf"; // whatever you wish to name the file

    ServletOutputStream out = response.getOutputStream();
    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    response.setContentType("application/pdf");
    response.setContentLength(bytes.length);
    out.write(bytes);
    out.flush();
}

MS Excel 文件的 MIME 类型为 application/vnd.ms-excel

Have a Servlet serve the pdf/excel file as a byte array.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    
    byte[] bytes = null; // get this from somewhere in your app
    String fileName = "filename.pdf"; // whatever you wish to name the file

    ServletOutputStream out = response.getOutputStream();
    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    response.setContentType("application/pdf");
    response.setContentLength(bytes.length);
    out.write(bytes);
    out.flush();
}

MIME type for MS Excel files would be application/vnd.ms-excel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文