首页 > 解决方案 > REST Uploading/downloading large file with Spring boot/JPA, Postgres

问题描述

I'm trying to upload ~700MB file and download the same through REST API in Spring boot with Postgres as database. I know Postgres is not the way to go but still, I'm trying to solve it as it is.

REST upload works after I added -Xmx2024m to VM while running. REST download gives error while calling uploadObjectEntityRepository.findById(id):

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

Error would mean the data is corupt, but the upload service ended clean. There were no errors during upload in the Postgres log also. In the database, field data is of type bytea.

What could be the problem?

Abbreviated code:

@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);
}   

标签: postgresqlspring-bootspring-data-jpabytea

解决方案


添加您的代码application.properties

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

推荐阅读