Spring上传文件大小限制

Sir*_*ann 4 size upload spring file max

我正在为我的应用程序使用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;
}
}
Run Code Online (Sandbox Code Playgroud)

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);
     }
}
Run Code Online (Sandbox Code Playgroud)

这段代码很好,它工作得很好,我只是无法设置最大文件大小.

提前致谢.

Sir*_*ann 9

对于早于4.0的Spring,正确的属性是

multipart.maxFileSize

multipart.maxRequestSize

从春季开始,这些都改为了

spring.http.multipart.max文件大小

spring.http.multipart.max请求尺寸


小智 8

对我来说(在Spring Boot 2.0.0中):

spring.servlet.multipart.max-file-size=-1
Run Code Online (Sandbox Code Playgroud)

  • 负值意味着该限制完全被禁用。您可以这样做,但在这种情况下,您必须控制前端的传入流量。负值为 DDoS 敞开大门。 (2认同)

小智 8

这个配置对我有用:

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
Run Code Online (Sandbox Code Playgroud)

参考文档:调整文件上传限制