[英]Download PDF file from Outputstream
I am working with this method to get a file 我正在使用这种方法来获取文件
@GetMapping(value = "/test", produces = MediaType.APPLICATION_PDF_VALUE)
public String test() throws FOPException, IOException {
SisDocuments document = documentRepository.getOne(Long.valueOf("801859"));
documentService.constructDocumentById(document);
return "/connexion";
}
@Override
public void constructDocumentById(SisDocuments document) {
try {
File inputFile = new File("input.txt");
File xsltfile = new File(path + "dz/sis-fop.xsl");
FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
OutputStream out;
out = new java.io.FileOutputStream("employee.pdf");
try {
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
TransformerFactory factory = TransformerFactory.newInstance();
Source xslt = new StreamSource(xsltfile);
Transformer transformer = factory.newTransformer(xslt);
File fXmlFile = new File(path + "dz/my-file.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
Result res = new SAXResult(fop.getDefaultHandler());
DOMSource source = new DOMSource(doc);
transformer.transform(source, res);
} finally {
}
} catch (Exception e) {
System.err.println(" r " + e.getMessage());
e.printStackTrace();
}
}
this method is creating a file in my project directory and istead of creating it i want to download it 这种方法是在我的项目目录中创建一个文件,而不是创建它,我想下载它
The typical way for this is to get your file as InputStream
and write it to the OutputStream
of the Response
通常的方法是将文件作为
InputStream
并将其写入Response
的OutputStream
。
@GetMapping(value = "/test/{idDocument}", produces = MediaType.APPLICATION_PDF_VALUE)
public void test(@PathVariable("idDocument") String idDocument, HttpServletRequest request,
HttpServletResponse response) throws FOPException, IOException {
SisDocuments document = documentRepository.getOne(Long.valueOf(idDocument));
documentService.constructDocumentById(document);
try {
// // get your file as InputStream
File f = new File("employee.pdf");
InputStream is = new FileInputStream(f);
org.apache.commons.io.IOUtils.copy(is, response.getOutputStream());
// copy it to response's OutputStream
response.addHeader("Content-disposition", "attachment; filename=" + "employee.pdf");
response.setContentType("application/pdf");
response.flushBuffer();
} catch (IOException ex) {
throw new RuntimeException("IOError writing file to output stream");
}
}
Your controller method must not return String
, in fact it must returns nothing :) To stream your file, you have to write your content directly in javax.servlet.http.HttpServletResponse - outputstream
, for example: 您的控制器方法不能返回
String
,实际上它必须不返回任何内容:)要流式传输文件,您必须直接在javax.servlet.http.HttpServletResponse - outputstream
编写内容,例如:
HttpServletResponse response;
Files.copy( yourPath, response.getOutputStream() );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.