我在我的应用程序中使用Spring Boot,我想将一些文件上传到我的数据库中。 我用了一个教程来实现这一点,它运行正常。我的问题是我不知道如何设置要上传的最大文件大小。默认值为1MB,但这对我来说还不够。
我将这些行添加到我的application.properties:
spring.http.multipart.max-file-size = 100MB
spring.http.multipart.max-request-size = 100MB
但它没有帮助。
我的代码:
FileService.java
@Service
public class FileService {
@Autowired
FileRepository fileRepository;
public Response uploadFile(MultipartHttpServletRequest request) throws IOException {
Response response = new Response();
List fileList = new ArrayList();
Iterator<String> itr = request.getFileNames();
while (itr.hasNext()) {
String uploadedFile = itr.next();
MultipartFile file = request.getFile(uploadedFile);
String mimeType = file.getContentType();
String filename = file.getOriginalFilename();
byte[] bytes = file.getBytes();
File newFile = new File(filename, bytes, mimeType);
File savedFile = fileRepository.saveAndFlush(newFile);
savedFile.setFile(null);
fileList.add(savedFile);
}
response.setReport(fileList);
return response;
}
}
FileController.java
@RestController
@RequestMapping("/file")
public class FileController {
@Autowired
FileService fileService;
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public Response uploadFile(MultipartHttpServletRequest request) throws IOException{
return fileService.uploadFile(request);
}
}
这段代码很好,它完美无缺,我无法设置最大文件大小。
提前致谢。
答案 0 :(得分:7)
使用早于4.0的Spring,正确的属性是
<强> multipart.maxFileSize 强>
<强> multipart.maxRequestSize 强>
从春季4开始,这些改为
<强> spring.http.multipart.max文件大小强>
<强> spring.http.multipart.max请求尺寸强>
答案 1 :(得分:7)
对我来说(在Spring Boot 2.0.0中):
spring.servlet.multipart.max-file-size=-1
答案 2 :(得分:1)
此配置对我有用:
spring.servlet.multipart.max-file-size = 10MB
spring.servlet.multipart.max-request-size = 10MB