gpt4 book ai didi

java - Spring MultipartFile 参数不尊重配置的 maxFileSize

转载 作者:太空宇宙 更新时间:2023-11-04 09:44:58 24 4
gpt4 key购买 nike

我有一个文件上传 Controller 。我试图使最大文件大小可配置,但我无法弄清楚为什么未应用记录的配置( https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-multipart-file-upload-configuration )。

plugins {
id 'org.springframework.boot' version '2.1.4.RELEASE'
id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.MultipartConfigElement;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

@Controller
public class FileUploadController {

private MultipartConfigElement multipartConfigElement;

@Autowired
public FileUploadController(MultipartConfigElement multipartConfigElement) {
this.multipartConfigElement = multipartConfigElement;
}

@PostMapping("/upload")
public void upload(@RequestParam("file") MultipartFile file) throws IOException {
InputStream inputStream = new BufferedInputStream(file.getInputStream());
// TODO something with inputStream

long fileSize = file.getSize();
boolean fileSizeLimitExceeded = fileSize > multipartConfigElement.getMaxFileSize();
return;
}
}


Debug screenshot

我希望 multipartConfigElement.getMaxFileSize() 应该防止较大的文件达到此目的并自动返回 400 或其他类型的异常。

但是 maxFileSize 似乎被完全忽略了。

最佳答案

事实证明,限制确实有效,并且会自动抛出异常。

当我使用 Postman 对 Controller 运行请求时,我看到了这一点。

{
"timestamp": "2019-04-05T09:52:39.839+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1 bytes.",
"path": "/upload"
}

我没有看到这一点的原因是因为我正在使用 MockMVC 进行测试(下面的代码片段)。由于某种原因,MockMVC 似乎没有触发异常——可能是因为它没有在兼容的 Web 服务器上运行。可能与 https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#spring-mvc-test-vs-end-to-end-integration-tests 有关.

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import java.io.FileInputStream;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
@AutoConfigureMockMvc
public class FileUploadTest {

@Autowired
private MockMvc mockMvc;

@Test
public void givenAFileThatExceedsTheLimit_whenUploaded_responseWith400Error() throws Exception {

MockMultipartFile file =
new MockMultipartFile("file", new FileInputStream(TestUtils.loadLargeFile()));

this.mockMvc
.perform(MockMvcRequestBuilders.multipart("/upload").file(file)
.contentType(MediaType.MULTIPART_FORM_DATA_VALUE))
.andExpect(status().isBadRequest());
}

}

关于java - Spring MultipartFile 参数不尊重配置的 maxFileSize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55514396/

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