java 将输出流转换为文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17487709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:09:10  来源:igfitidea点击:

Convert Outputstream to file

javahtmljsppdfvraptor

提问by gmlyranetwork

Well i'm stucked with a problem,

好吧,我遇到了一个问题,

I need to create a PDF with a html source and i did this way:

我需要使用 html 源创建一个 PDF,我是这样做的:

File pdf = new File("/home/wrk/relatorio.pdf");
OutputStream out = new FileOutputStream(pdf);
InputStream input = new ByteArrayInputStream(build.toString().getBytes());//Build is a StringBuilder obj
Tidy tidy = new Tidy();
Document doc = tidy.parseDOM(input, null);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc, null);
renderer.layout();
renderer.createPDF(out);
out.flush();
out.close();

well i'm using JSP so i need to download this file to the user not write in the server...

好吧,我正在使用 JSP,所以我需要将此文件下载给用户,而不是写入服务器...

How do I transform this Outputstream output to a file in the java without write this file in hard drive ?

如何将此 Outputstream 输出转换为 java 中的文件而不将此文件写入硬盘驱动器?

采纳答案by g0dkar

If you're using VRaptor 3.3.0+ you can use the ByteArrayDownloadclass. Starting with your code, you can use this:

如果您使用的是 VRaptor 3.3.0+,则可以使用ByteArrayDownload该类。从你的代码开始,你可以使用这个:

@Path("/download-relatorio")
public Download download() {
    // Everything will be stored into this OutputStream
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    InputStream input = new ByteArrayInputStream(build.toString().getBytes());
    Tidy tidy = new Tidy();
    Document doc = tidy.parseDOM(input, null);
    ITextRenderer renderer = new ITextRenderer();
    renderer.setDocument(doc, null);
    renderer.layout();
    renderer.createPDF(out);
    out.flush();
    out.close();

    // Now that you have finished, return a new ByteArrayDownload()
    // The 2nd and 3rd parameters are the Content-Type and File Name
    // (which will be shown to the end-user)
    return new ByteArrayDownload(out.toByteArray(), "application/pdf", "Relatorio.pdf");
}

回答by Michael Lang

A Fileobject does not actually hold the data but delegates all operations to the file system (see this discussion). You could, however, create a temporary file using File.createTempFile. Also look herefor a possible alternative without using a Fileobject.

一个File对象不实际保存数据,但代表所有操作的文件系统(见讨论)。但是,您可以使用File.createTempFile. 还可以在此处查找不使用File对象的可能替代方案。

回答by Rotom92

use temporary files.

使用临时文件。

File temp = File.createTempFile(prefix ,suffix);

prefix -- The prefix string defines the files name; must be at least three characters long.

prefix -- 前缀字符串定义文件名;长度必须至少为三个字符。

suffix -- The suffix string defines the file's extension; if null the suffix ".tmp" will be used.

suffix -- 后缀字符串定义文件的扩展名;如果为空,将使用后缀“.tmp”。