postgresql - 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);
}
解决方案
添加您的代码application.properties
spring.http.multipart.max-file-size=1000MB
spring.http.multipart.max-request-size=1000MB
推荐阅读
- flutter - 如何检查颤振吐司是否已经在屏幕上?
- python - 使用来自另一个数据帧的列更新数据帧中的多个列
- javascript - 收到错误“machineDefinition is undefined”我已经按照步骤操作了吗?
- oracle - RTF 模板中的 XML 标记分组以通过嵌套表计算总计,但不提供格式化输出。嵌套表中的额外行
- mongodb - 为什么在 mongodb 中没有更新 system.profile 集合?
- flutter - Flutter - Json 序列化不起作用
- c# - 无法调用带参数的 webmethod
- snowflake-cloud-data-platform - 注册性能:使用存储过程的数据转换
- javascript - 链表。对代码中的平等问题感到困惑
- jquery - 通过AJAX将序列化的数据传递给MVC中的控制器