简体   繁体   English

如何使用 java spring 从 mysql 检索和显示 pdf

[英]how to retrive and display pdf from mysql using java spring

I can insert the PDF file to MySQL according to below code, I had been strucked in retrieving PDF and displaying PDF as I uploaded with java spring boot.我可以根据下面的代码将 PDF 文件插入到 MySQL,当我用 java spring 引导上传时,我在检索 PDF 并显示 PDF 时感到震惊。

@Entity
@Table(name = "permission")
public class DBFile {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String id;
private String filename;
private String file Type;
private String name;
private Date submission Date= new java.util.Date();
@Lob
private byte[] data;  
public DB File (String filename, String file Type, byte[] data, String name) {
    super();
    this.id = ID;
    this.filename = filename;
    this.file Type = file Type;
    this.data = data;
    this.name = name;
    

} }

1st I must say that uploading a blob to the database is not recommended when you are planning to upload more files. 1st 我必须说,当您计划上传更多文件时,不建议将 blob 上传到数据库。 The database will grow rapidly according to the file sizes of uploaded pdfs.数据库将根据上传的 pdf 的文件大小快速增长。

I suggest you to upload it to a server and save the file location at the database table.我建议您将其上传到服务器并将文件位置保存在数据库表中。

but according to this approach, you have to write a simple java spring boot service to send pdf但是按照这种做法,你得写一个简单的 java spring 引导服务发送 pdf

// you can query the file using JPA or whatever    
byte[] fileFromTable = entity.getData();
        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType("applicatioin/pdf"))
                .header(HttpHeaders.CONTENT_DISPOSITION,
                        "attachment; filename=" + entity.getName() + CommonConstants.DOUBLE_QUOTE)
                .body(new ByteArrayResource(fileFromTable));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM