java - 从 Restful Web 服务下载 pdf

标签 java rest pdf download

我正在编写一个休息服务,当用户在用户计算机上访问此服务时,它将下载 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/

相关文章:

java - Apache Ignite 中的持久性概述

java - 在 json java 中切换值

javascript - Parse Web 服务器中的文件系统模块

pdf - 如何全局替换PDF中的字体

java - 使用或不使用 Itext 的 PdfCopy 类连接 2 个不同的 pdf

java - 第一个选项的下拉选定值默认值

rest - 如何在 Snaplogic 的 REST POST 快照中设置 x-www-form-urlencoded?

javascript - REST 客户端实用程序

android - “{parameter}”查询后,如何使用Retrofit将这些Api URL与这些 “?”映射

java.net.ConnectException : Connection refused between two microservices via docker