【问题标题】:REST Uploading/downloading large file with Spring boot/JPA, PostgresREST 使用 Spring boot/JPA、Postgres 上传/下载大文件
【发布时间】:2021-02-18 13:44:54
【问题描述】:

我正在尝试上传 ~700MB 文件并在 Spring Boot 中使用 Postgres 作为数据库通过 REST API 下载相同的文件。我知道 Postgres 不是要走的路,但我仍在努力解决它。

在运行时将 -Xmx2024m 添加到 VM 后,REST 上传工作。 REST 下载在调用uploadObjectEntityRepository.findById(id) 时出错:

Caused by: org.postgresql.util.PSQLException: ERROR: invalid memory alloc request size 1381847031

错误意味着数据已损坏,但上传服务以干净的方式结束。在 Postgres 日志中上传期间也没有错误。在数据库中,字段data 的类型为bytea

可能是什么问题?

缩写代码:

@Entity
@Table(name = "UploadObjectEntity")
public class UploadObjectEntity implements Serializable {
    ...
    @Lob
    @Column(name = "data", nullable = false)
    private byte[] data;
    ...
}

@PostMapping("/upload")
public ResponseEntity<UploadObject> upload(@RequestPart MultipartFile file){
    ...
    UploadObject o = customService.saveUploadObject(file);
    ...
}

@RequestMapping(value = "/download/{id}", method = RequestMethod.GET)
public void getByteArray(@PathVariable("id") Long id, HttpServletResponse response) {
    UploadObject uploadObject = customService.load(id);
    ByteSource byteSource = ByteSource.wrap(uploadObject.getData());

    InputStream stream = byteSource.openStream();

    response.setContentType(...);
    response.setContentLength(...);
    IOUtils.copy(stream, response.getOutputStream());
}

//Custom Service
public UploadObject saveUploadObject(MultipartFile file) {
    ...
    UploadObjectEntity uploadObjectEntity = new UploadObjectEntity();
    uploadObjectEntity.setData(file.getBytes());
    ...
    uploadObjectEntity = uploadObjectEntityRepository.save(uploadObjectEntity);

    return uploadObjectEntityMapper.toDto(uploadObjectEntity);
}

public UploadObject load(Long id) {
    return uploadObjectEntityRepository.findById(id).map(uploadObjectEntityMapper::toDto);
}   

【问题讨论】:

    标签: postgresql spring-boot spring-data-jpa bytea


    【解决方案1】:

    添加您的代码application.properties

    spring.http.multipart.max-file-size=1000MB
    spring.http.multipart.max-request-size=1000MB
    

    【讨论】:

      猜你喜欢
      • 2018-04-04
      • 2016-06-11
      • 2021-05-01
      • 2018-10-08
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      • 2019-04-06
      • 2015-05-23
      相关资源
      最近更新 更多