[英]File is getting downloaded twice in Java Jersey API
我正在使用以下代碼從html下載PDF包含我的工作正常,但是文件被下載了兩次,我試圖修改渲染器行為,但仍然沒有任何東西。
public Response downloadResumePdf(@PathParam("userId") String userId) throws IOException, DocumentException {
String homePath = System.getProperty("user.home");
String filePath = homePath + "/Downloads/Resume" + LocalDateTime.now().toLocalDate() + ".pdf";
org.xhtmlrenderer.pdf.ITextRenderer renderer = new ITextRenderer();
String yourXhtmlContentAsString = "<h1>hi </h1>";
renderer.setDocumentFromString(yourXhtmlContentAsString);
renderer.layout();
java.io.FileOutputStream fos = new java.io.FileOutputStream(filePath);
renderer.createPDF(fos);
fos.close();
File file = new File(filePath);
return Response
.ok((Object) file)
.header("Content-Disposition", "attachment; filename=\"Resume" + LocalDateTime.now().toLocalDate() + ".pdf\"")
.build();
問題
在您的代碼中,您正在生成一個文件,然后將其提供給您的API。 該文件是使用new java.io.FileOutputStream(filePath)
創建的,名為Downloads
文件夾中的Resume2019-01-16.pdf
。
由於您是在本地運行API,因此當您轉到端點時,瀏覽器會將您提供的文件Downloads
到“ Downloads
文件夾中。 由於Resume2019-01-16.pdf
已經存在,瀏覽器將其命名為Resume2019-01-16 (1).pdf
。
因此,似乎正在下載兩個文件,但是其中一個是由您的代碼生成的,而另一個實際上是在下載的。
固定
更改您正在提供的文件的文件夾,只有實際下載的文件才會出現在您的Downloads
例如:
String filePath = homePath + "/Documents/Resume" + LocalDateTime.now().toLocalDate() + ".pdf";
或者,使用某種方法將文件存儲在內存中,而不是創建物理文件並提供服務。
正如Mark的答案所述,重復的原因是因為您在創建並寫入FileOutputStream
時正在創建“臨時”文件。
解決方案:您無需創建臨時文件即可處理下載。 不用創建FileOutputStream
,只需使用StreamingOutput
並將StreamingOutput
的OutputStream
傳遞給ITextRenderer#createPDF(OutputStream)
方法。
@GET
@Produces("application/pdf")
public Response getResumePdf(@PathParam("userId") String userId) {
StreamingOutput entity = new StreamingOutput() {
@Override
public void write(OutputStream output) {
try {
ITextRenderer renderer = new ITextRenderer();
String yourXhtmlContentAsString = "<h1>hi </h1>";
renderer.setDocumentFromString(yourXhtmlContentAsString);
renderer.layout();
renderer.createPDF(output);
output.flush();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
return Response.ok(entity)
.header(...)
.build();
}
您使用StreamingOutput代替使用File。
String homePath = System.getProperty("user.home");
String filePath = homePath + "/Downloads/Resume" + LocalDateTime.now().toLocalDate() + ".pdf";
org.xhtmlrenderer.pdf.ITextRenderer renderer = new ITextRenderer();
String yourXhtmlContentAsString = "<h1>hi </h1>";
renderer.setDocumentFromString(yourXhtmlContentAsString);
renderer.layout();
java.io.FileOutputStream fos = new java.io.FileOutputStream(filePath);
renderer.createPDF(fos);
//fos.close();
final File file = new File(filePath);
StreamingOutput fileStream = new StreamingOutput()
{
@Override
public void write(java.io.OutputStream output) throws IOException, WebApplicationException
{
try
{
byte[] data = Files.readAllBytes(file.toPath());
output.write(data);
output.flush();
}
catch (Exception e)
{
throw new WebApplicationException("File Not Found. !!");
}
}
};
return Response
.ok( fileStream, MediaType.APPLICATION_OCTET_STREAM)
.header("Content-Disposition", "attachment; filename=\"Resume" + LocalDateTime.now().toLocalDate() + ".pdf\"")
.build();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.