java http上传文件大小限制,春季上传文件大小限制

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.

I added these lines to my application.properties:

spring.http.multipart.max-file-size = 100MB

spring.http.multipart.max-request-size = 100MB

but it didn't help.

My code:

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 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.

Thanks in advance.

解决方案

With Spring earlier than 4.0 the right properties are

multipart.maxFileSize

multipart.maxRequestSize

From Spring 4 these were changed to

spring.http.multipart.max-file-size

spring.http.multipart.max-request-size

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值