Spring上传文件大小限制 [英] Spring upload file size limit

查看:93
本文介绍了Spring上传文件大小限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序使用 Spring Boot,我想将一些文件上传到我的数据库中.我使用了一个教程来实现这一点,并且效果很好.我的问题是我不知道如何设置要上传的最大文件大小.默认值为 1MB,但这对我来说还不够.

I'm using Spring Boot for my application, and I want to upload some files into my database. I used a tutorial to achive this, and it works fine. My problem is that I don't know how to set max file size to upload. The default is 1MB but that's just not enough for me.

我将这些行添加到我的 application.properties:

I added these lines to my 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);
     }
}

这段代码很好,运行良好,我只是无法设置最大文件大小.

This code is just fine, it works perfectly, I just can't set max file size.

推荐答案

Spring 4.0 之前的正确属性是

With Spring earlier than 4.0 the right properties are

multipart.maxFileSize

multipart.maxRequestSize

从 Spring 4 开始,这些更改为

From Spring 4 these were changed to

spring.http.multipart.max-file-size

spring.http.multipart.max-request-size

这篇关于Spring上传文件大小限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆