Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何下载生成的PDF而不将其存储在服务器上?_Java_Spring Boot_Itext_Jhipster - Fatal编程技术网

Java 如何下载生成的PDF而不将其存储在服务器上?

Java 如何下载生成的PDF而不将其存储在服务器上?,java,spring-boot,itext,jhipster,Java,Spring Boot,Itext,Jhipster,我有一个Jhipster应用程序,它使用iText库生成PDF,该PDF按照我指定的路径保存在计算机中。 我希望在生成pdf时,会出现一个对话框来下载pdf。如果pdf保存在项目文件夹中或未保存在任何地方,我对此漠不关心 我在这个页面和互联网上看到了许多给出可能答案的帖子,但其中许多已经过时,还有一些没有为我工作过 generatePDF entity.service.ts downloadFile():可观察{ 返回这个.http.get(SERVER_API_URL+'API/downloa

我有一个Jhipster应用程序,它使用iText库生成PDF,该PDF按照我指定的路径保存在计算机中。 我希望在生成pdf时,会出现一个对话框来下载pdf。如果pdf保存在项目文件夹中或未保存在任何地方,我对此漠不关心

我在这个页面和互联网上看到了许多给出可能答案的帖子,但其中许多已经过时,还有一些没有为我工作过

generatePDF

entity.service.ts

downloadFile():可观察{
返回这个.http.get(SERVER_API_URL+'API/downloadFile');
}

使用此选项下载文件:

@GetMapping("/downloadFile")
    public ResponseEntity<Resource> downloadFile(HttpServletRequest request) {
        // Load file as Resource
        Resource resource = testService.loadFileAsResource();

        // Try to determine file's content type
        String contentType = null;
        try {
            contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
        } catch (IOException ex) {
            log.info("Could not determine file type.");
        }

        // Fallback to the default content type if type could not be determined
        if (contentType == null) {
            contentType = "application/octet-stream";
        }

        return ResponseEntity.ok().contentType(MediaType.parseMediaType(contentType)).header(
            HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"").body(resource);
    }
public Resource loadFileAsResource() {
    try {
        Path path = Paths.get("D:\\PDF\\template.pdf");
        Path filePath = path.normalize();

        Resource resource = new UrlResource(filePath.toUri());
        if (resource.exists()) {
            return resource;
        } else {
            return null;
        }
    } catch (MalformedURLException ex) {
        ex.printStackTrace();
        return null;
    }
}
参考资料:


不要写入文件,而是写入HttpServletResponse的输出流。在写作之前,请确保设置正确的内容类型。我如何才能做到这一点?这是我第一次处理文件,我迷路了。没有涉及任何文件。将HttpServletResponse添加到响应的参数中,设置其内容类型,获取其输出流,将PDF写入输出流。当然,您的方法不能返回用户:要么返回PDF文档,要么为您的用户返回JSON文档。这两个都不能退。谢谢!!如果我通过访问,但在应用程序中不起作用,并且在日志中所有内容都是正确的(访问方法很好),那么这样做很好。在post中添加.ts方法。创建资源名称测试,在resource和class SecurityConfiguration中添加此方法添加以下代码:.antMatchers(“/test/**”).permitAll()以加载访问本地主机:9060/test/downloadFile
    downloadFile() {
        this.entityService.downloadFile().subscribe();
    }
    downloadFile(): Observable<any> {
        return this.http.get(SERVER_API_URL + 'api/downloadFile');
    }
@GetMapping("/downloadFile")
    public ResponseEntity<Resource> downloadFile(HttpServletRequest request) {
        // Load file as Resource
        Resource resource = testService.loadFileAsResource();

        // Try to determine file's content type
        String contentType = null;
        try {
            contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
        } catch (IOException ex) {
            log.info("Could not determine file type.");
        }

        // Fallback to the default content type if type could not be determined
        if (contentType == null) {
            contentType = "application/octet-stream";
        }

        return ResponseEntity.ok().contentType(MediaType.parseMediaType(contentType)).header(
            HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"").body(resource);
    }
public Resource loadFileAsResource() {
    try {
        Path path = Paths.get("D:\\PDF\\template.pdf");
        Path filePath = path.normalize();

        Resource resource = new UrlResource(filePath.toUri());
        if (resource.exists()) {
            return resource;
        } else {
            return null;
        }
    } catch (MalformedURLException ex) {
        ex.printStackTrace();
        return null;
    }
}