相关疑难解决方法(0)

711
推荐指数
20
解决办法
100万
查看次数

从弹簧控制器下载文件

我有一个要求,我需要从网站上下载PDF.PDF需要在代码中生成,我认为这将是freemarker和像iText这样的PDF生成框架的组合.有更好的方法吗?

但是,我的主要问题是如何允许用户通过Spring Controller下载文件?

java spring controller file download

354
推荐指数
9
解决办法
41万
查看次数

在新的浏览器选项卡中打开ResponseEntity PDF

我遇到了一个有用的PDF生成代码,用于在Spring MVC应用程序中向客户端显示该文件(" 使用Spring MVC返回生成的PDF "):

@RequestMapping(value = "/form/pdf", produces = "application/pdf")
public ResponseEntity<byte[]> showPdf(DomainModel domain, ModelMap model) {
    createPdf(domain, model);

    Path path = Paths.get(PATH_FILE);
    byte[] pdfContents = null;
    try {
        pdfContents = Files.readAllBytes(path);
    } catch (IOException e) {
        e.printStackTrace();
    }

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType("application/pdf"));
    String filename = NAME_PDF;
    headers.setContentDispositionFormData(filename, filename);
    headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
    ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(
            pdfContents, headers, HttpStatus.OK);
    return response;
}
Run Code Online (Sandbox Code Playgroud)

我添加了一个声明,表明该方法返回一个PDF文件(" 春3.0的Java REST回报PDF文档 "): produces = "application/pdf".

我的问题是,当执行上面的代码时,它会立即要求客户端保存PDF文件.我希望首先在浏览器中查看PDF文件,以便客户端可以决定是否保存.

我发现 …

java pdf spring spring-mvc

14
推荐指数
1
解决办法
3万
查看次数

无法下载文件:无法解析为 URL,因为 Spring 中不存在该文件

我想使用以下 Angular 6 代码实现文件下载:

休息API:

private static final Logger LOG = LoggerFactory.getLogger(DownloadsController.class);

private static final String EXTERNAL_FILE_PATH = "/Users/test/Documents/blacklist_api.pdf";

@GetMapping("export")
public ResponseEntity<InputStreamResource> export() throws IOException {
    ClassPathResource pdfFile = new ClassPathResource(EXTERNAL_FILE_PATH);

    HttpHeaders headers = new HttpHeaders();
    headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
    headers.add("Pragma", "no-cache");
    headers.add("Expires", "0");

    return ResponseEntity.ok().headers(headers).contentLength(pdfFile.contentLength())
            .contentType(MediaType.parseMediaType("application/pdf"))
            .body(new InputStreamResource(pdfFile.getInputStream()));
}
Run Code Online (Sandbox Code Playgroud)

服务:

import {Component, OnInit} from '@angular/core';
import {DownloadService} from "../service/download.service";
import {ActivatedRoute, Router} from "@angular/router";
import {flatMap} from "rxjs/internal/operators";
import {of} from "rxjs/index";
import { map } from 'rxjs/operators';

@Component({ …
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot angular angular6

5
推荐指数
1
解决办法
2万
查看次数

下载的PDF看起来是空的,尽管它包含一些数据

我正在尝试使用JavaScript实现PDF文件下载功能.
作为对POST请求的响应,我得到一个PDF文件,在Chrome DevTools控制台中看起来像(oResult数据容器,片段):

"%PDF-1.4↵% ↵40obj↵<>stream↵x

现在我正在尝试初始化下载过程:

let blob = new Blob([oResult], {type: "application/pdf"});

let link = document.createElement('a');

link.href = window.URL.createObjectURL(blob);
link.download = "tstPDF";

link.click();
Run Code Online (Sandbox Code Playgroud)

结果,点击一个按钮我得到tstPDF.pdf,它包含正确的页数,但PDF本身是空的,没有显示内容,尽管它是6 KB.

当我测试生成PDF的Java服务器端模块时,一切正常,它InputStream通过发送ServletOutputStream.因此,我认为这个问题是地方上的客户端,或许有东西MIME,BLOB,encoding,或类似的.

您知道为什么生成的PDF不显示任何数据吗?

javascript jquery post blob itext

2
推荐指数
1
解决办法
3221
查看次数