简体   繁体   中英

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.

@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. The database will grow rapidly according to the file sizes of uploaded pdfs.

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

// 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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