gpt4 book ai didi

java - 从 Restful Web 服务下载 pdf

转载 作者:行者123 更新时间:2023-12-02 02:51:20 27 4
gpt4 key购买 nike

我正在编写一个休息服务,当用户在用户计算机上访问此服务时,它将下载 pdf。下面是我的 Controller 类的代码。

当我尝试在浏览器中访问以下网址时,它正在下载 .JSON 文件而不是 PDF 文件。

URL:- http://localhost:8080/test-service/downloadPDF?userId=abcd&requestingLocation=6&orderId=06006030

我尝试使用以下站点中提到的相同代码。这似乎是直接的代码,但不确定为什么它不起作用。 http://www.java2novice.com/restful-web-services/jax-rs-download-file/ https://www.javatpoint.com/jax-rs-file-download-example

以下是 Controller 中的方法。

    @GET
@RequestMapping(value = "/downloadPDF")
@Produces("application/pdf")
public @ResponseBody Response downloadDocument(@RequestParam("userId") String userId,@RequestParam("requestingLocation") String requestinglocation,@RequestParam("orderId") String orderId) {
String path = "C:\\opt\\autobol\\logs\\autobol\\test2.pdf";
File file = new File(path);
ResponseBuilder response = Response.ok((Object) file);
response.header("Content-Disposition","attachment; filename=\"javatpoint_pdf.pdf\"");
return response.build();

}

它应该下载pdf文件

以下是下载文件的内容(文件名downloadPDF.json):

{  
"entity":"C:\\opt\\autobol\\logs\\autobol\\test2.pdf",
"status":200,
"metadata":{
"Content-Disposition":[
"attachment; filename=\"javatpoint_pdf.pdf\""
]
},
"annotations":null,
"entityClass":"java.io.File",
"genericType":null,
"length":-1,
"language":null,
"location":null,
"lastModified":null,
"date":null,
"closed":false,
"cookies":{

},
"statusInfo":"OK",
"stringHeaders":{
"Content-Disposition":[
"attachment; filename=\"javatpoint_pdf.pdf\""
]
},
"links":[

],
"entityTag":null,
"allowedMethods":[

],
"mediaType":null,
"headers":{
"Content-Disposition":[
"attachment; filename=\"javatpoint_pdf.pdf\""
]
}
}

在下载PDF.json文件中

最佳答案

试试这个

            HttpResponse response = HttpContext.Current.Response;
response.ClearContent();
response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(strPath));
response.TransmitFile(strPath); //File
response.Flush();
response.End();
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(strPath));
Response.WriteFile(strPath);
Response.End();

关于java - 从 Restful Web 服务下载 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57107151/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com