gpt4 book ai didi

spring - 如何在 Spring Boot 中更改 Multipart 文件的最大大小?

转载 作者:行者123 更新时间:2023-12-04 13:45:58 26 4
gpt4 key购买 nike

我尝试使用 postman 上传歌曲,但出现错误,例如字段歌曲超出其最大允许大小 1048576 字节。”

我已经尝试在 application.properties 文件中添加此配置,但它不起作用:

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB

我确切地说这适用于大小小于 1048576 字节的文件

这是我的 Controller :

@PostMapping("/songs")
public ResponseEntity<?> insertSong( @RequestParam(value = "title") String title, @RequestParam(value = "song") MultipartFile file) throws IOException {

Song song= new Song(title);

songService.insertSong(song, file);

return ResponseEntity.ok("song inserted");
}

这是我的服务:
@Service
public class SongServiceImpl implements SongService {

@Autowired
SongRepository songRepository;

@Value("${dir.songs}")
private String songsFolderPath;

@Override
public void insertSong(Song song, MultipartFile file) throws IOException
{

if(!(file.isEmpty())){
song.setSongPath(file.getOriginalFilename());
songRepository.save(song);

if (!(file.isEmpty())){
song.setSongPath(file.getOriginalFilename());
file.transferTo(new
File(songsFolderPath+song.getSong_ID()));
}
}
}

这是我的错误消息:
{"timestamp":"2018-10-10T13:27:07.090+0000","status":500,"error":"内部服务器错误","message":"已超出最大上传大小;嵌套异常是java。 lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: 字段歌曲超出其最大允许大小 1048576 字节。","path":"/music-service/songs"}

我使用微服务,我的配置在 gitLab 上,这是一个:

enter image description here

我用这样的 postman 来插入我的文件:

enter image description here

最佳答案

对于 spring-boot 版本 2.x.x,请尝试使用以下代码,包括 http。

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

引用 set-maxfilesize

关于spring - 如何在 Spring Boot 中更改 Multipart 文件的最大大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52741480/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com