当前位置: 首页 > 知识库问答 >
问题:

如何在spring应用程序中从web service下载图像/pdf文件

公羊光明
2023-03-14

@getmapping(value=“/image”,produces=“application/pdf”)@responsebody public String downloadFile(@pathvariable(“path”)String path)抛出IOException{ClassPathResource downloadLink=new ClassPathResource(“/assets/”+path);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType("application/pdf"));
    headers.add("Access-Control-Allow-Origin", "*");
    headers.add("Access-Control-Allow-Methods", "GET, POST, PUT");
    headers.add("Access-Control-Allow-Headers", "Content-Type");
    headers.add("Content-Disposition", "filename=" + path);
    headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
    headers.add("Pragma", "no-cache");
    headers.add("Expires", "0");

    headers.setContentLength(downloadLink.contentLength());
    Base64.getDecoder().decode(path);
    return "/admin/image";
}

共有1个答案

顾俊茂
2023-03-14

我检查了你的代码,你的代码几乎是正确的,但你需要修改一些代码,你可以使用这个示例代码下载文件:你可以参考这个代码

@RestController
@RequestMapping("/download")
public class DownloadFileRestController {
    @Autowired
    ServletContext context;

    @RequestMapping(value = "/pdf/{fileName:.+}", method = RequestMethod.GET, produces = "application/pdf")
    public ResponseEntity<InputStreamResource> download(@PathVariable("fileName") String fileName) throws IOException {
        System.out.println("Calling Download:- " + fileName);
        ClassPathResource pdfFile = new ClassPathResource("downloads/" + fileName);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/pdf"));
        headers.add("Access-Control-Allow-Origin", "*");
        headers.add("Access-Control-Allow-Methods", "GET, POST, PUT");
        headers.add("Access-Control-Allow-Headers", "Content-Type");
        headers.add("Content-Disposition", "filename=" + fileName);
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");

        headers.setContentLength(pdfFile.contentLength());
        ResponseEntity<InputStreamResource> response = new ResponseEntity<InputStreamResource>(new InputStreamResource(pdfFile.getInputStream()), headers, HttpStatus.OK);
        return response;

    }
}
 类似资料:
  • 这是我的选项2选项 但它给出了一个错误的说法 java.io.FileNotFoundException:C:\users\businesssupport\documents\netBeansProjects\InvoiceGenerator\resources\hemaslogo.jpg(系统找不到指定的路径)

  • 我在我的Android应用程序中使用滑翔进行图像加载,以避免任何崩溃,我正在加载带有应用程序上下文的图像。这会对应用程序和内存的性能产生什么影响?

  • 我正在处理一个Spring Boot应用程序,其中我使用该应用程序公开SOAP WebService。我在Spring boot应用程序中使用Apache CFX framework for SOAP impl。我正在使用基于注释的方法。 我在一个bean中的Spring Boot配置文件中设置应用程序上下文时遇到了问题。下面是我的代码。 配置文件如下所示。 现在我有了bean SOAPproce

  • 问题内容: 我正在尝试使浏览器下载从ajax响应接收到的pdf文件。 受jquery ajax下载pdf文件启发,我模拟了如下单击/下载事件: 不幸的是,这仅适用于Chrome,不适用于Firefox + IE。当我尝试在最后两个浏览器中触发它时,没有任何反应。 由于从CMS继承,脚本和标记被放置在iframe中,但是我不确定这是否有影响。 关于如何针对所有现代浏览器进行优化的想法? 问题答案:

  • 问题内容: 我需要从服务器上的PDF文件中提取所有图像。我不想要PDF页面,只想要原始尺寸和分辨率的图像。 如何使用Perl,PHP或任何其他基于UNIX的应用程序(我将使用PHP的exec函数调用它)来做到这一点? 问题答案: pdfimages就是这样做的。它是poppler- utils和xpdf-utils软件包的一部分。 从联机帮助页: Pdfimages将可移植文档格式(PDF)文件中

  • 我尝试将Spring Boot应用程序连接到JFrog Artifictory以检索文件。任何人都知道如何做到这一点,或者我们需要做什么。 谢谢。