Java Spring启动-数据JPA-从DB下载大型Lob文件
我编写了一个spring boot web/rest应用程序来处理遗留系统(DB表),该应用程序执行以下操作:Java Spring启动-数据JPA-从DB下载大型Lob文件,java,hibernate,rest,spring-boot,spring-data-jpa,Java,Hibernate,Rest,Spring Boot,Spring Data Jpa,我编写了一个spring boot web/rest应用程序来处理遗留系统(DB表),该应用程序执行以下操作: 用户通过REST WS创建账单请求 我的rest应用程序存储对旧数据库表的请求 另一个系统(X)(我无法控制)读取记录并生成zip文件 然后,X系统将文件存储在我们的表中(zip文件和文件名) 用户现在可以使用我的应用程序下载该文件 这里的问题是一些账单文件很大(1-2 GB),我得到: java.lang.OutOfMemoryError: Java heap space 当我试图
java.lang.OutOfMemoryError: Java heap space
当我试图选择文件内容时,在Hibernate中会发生这种情况
现在,我如何让用户下载一个大文件而不出现上述错误?请在下面查看POJO类别和回购协议
实体类
@Entity
public class EbillStatus implements Serializable {
private static final long serialVersionUID = 1L;
@Id @GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy="uuid")
@Column(name="EBILL_STATUS_ID")
private String id;
@Column(name="CORPORATE_EBILL_ID", insertable=false, updatable=false)
private String corporateEbillId;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="COMPLETION_DATETIME")
private Date completionDatetime;
@Column(name="FILENAME", insertable=false, updatable=false)
private String filename;
@Lob
private byte[] fileContent;
@Column(name="USER_ID")
private String username;
@Column(name="STATUS_ID")
private Integer status;
@Column(name="BILL_COUNT", insertable=false, updatable=false)
private Integer billCount;
private Integer accountCount;
}
存储库:
public interface EbillStatusRepository extends JpaRepository<EbillStatus, String> {
EbillStatus findByCorporateEbill(CorporateEbill corporateEbill);
@Query(value="select s FROM EbillStatus s WHERE s.corporateEbillId=?1")
EbillStatus findFile(String corporateEbillId);
}
提供给控制器的Pojo类
public class EbillFileModel {
private String fileName;
private byte[] fileContent;
public EbillFileModel(String fileName, byte[] fileContent) {
super();
this.fileName = fileName;
this.fileContent = fileContent;
}
public EbillFileModel() {
super();
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public byte[] getFileContent() {
return fileContent;
}
public void setFileContent(byte[] fileContent) {
this.fileContent = fileContent;
}
}
下载文件的控制器方法
@GetMapping(value = "/{username}/order/{eBillId}/download", produces = "application/zip")
public ResponseEntity<ByteArrayResource> downloadEbillFile(@PathVariable String username, @PathVariable String eBillId)
throws IOException, NotFoundException {
EbillFileModel file = ebillservice.getEbillFile(username, eBillId);
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CACHE_CONTROL, CACHE_CONTROL_VAL);
headers.add(HttpHeaders.PRAGMA,PRAGMA_VAL);
headers.setContentDispositionFormData(ATTACHMENT, file.getFileName());
headers.add(HttpHeaders.EXPIRES, ZERO);
return ResponseEntity
.ok()
.headers(headers)
.contentType(APPLICATION_ZIP)
.body(new ByteArrayResource(file.getFileContent()));
}
@GetMapping(value=“/{username}/order/{eBillId}/download”,products=“application/zip”)
public ResponseEntity下载ebillfile(@PathVariable字符串username,@PathVariable字符串eBillId)
抛出IOException,NotFoundException{
EbillFileModel file=ebillservice.getEbillFile(用户名,eBillId);
HttpHeaders=新的HttpHeaders();
headers.add(HttpHeaders.CACHE\u CONTROL,CACHE\u CONTROL\u VAL);
headers.add(HttpHeaders.PRAGMA,PRAGMA_VAL);
headers.setContentDispositionFormData(附件,file.getFileName());
headers.add(HttpHeaders.EXPIRES,零);
返回响应性
.ok()
.标题(标题)
.contentType(应用程序\u ZIP)
.body(新的ByteArrayResource(file.getFileContent());
}
摘要和注释:
- 我不能改变系统的设计和工作方式,我只是在上面写了一个Web/REST应用程序
- 我使用的是SpringBoot1.5.1.RELEASE和Java8
- 打包是部署在JBoss EAP 7上的WAR文件
- 下载存储在DB中的大型文件的最佳方式是什么
@GetMapping(value = "/{username}/order/{eBillId}/download", produces = "application/zip")
public ResponseEntity<ByteArrayResource> downloadEbillFile(@PathVariable String username, @PathVariable String eBillId)
throws IOException, NotFoundException {
EbillFileModel file = ebillservice.getEbillFile(username, eBillId);
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CACHE_CONTROL, CACHE_CONTROL_VAL);
headers.add(HttpHeaders.PRAGMA,PRAGMA_VAL);
headers.setContentDispositionFormData(ATTACHMENT, file.getFileName());
headers.add(HttpHeaders.EXPIRES, ZERO);
return ResponseEntity
.ok()
.headers(headers)
.contentType(APPLICATION_ZIP)
.body(new ByteArrayResource(file.getFileContent()));
}