我正在使用这种方法来获取文件
@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();
}
}
此方法是在我的项目目录中创建一个文件,而不是创建一个我要下载的文件
答案 0 :(得分:0)
典型的方法是将文件获取为InputStream
并将其写入OutputStream
的{{1}}
Response
答案 1 :(得分:0)
您的控制器方法不得返回String
,实际上,它必须不返回任何内容:)
要流式传输文件,您必须直接在javax.servlet.http.HttpServletResponse - outputstream
中编写内容,例如:
HttpServletResponse response;
Files.copy( yourPath, response.getOutputStream() );