在Spring Boot中测试文件上传导致fileuploadeexception(未设置多部分边界)



我试图将文件上传到我的Spring Boot应用程序并直接将它们写入目标(而不是首先在临时文件中)。我的应用程序代码可以工作,但我无法使单元测试工作。我的控制器是这样的:

@PostMapping("/upload")
@ResponseBody
public String handleFileUpload(final HttpServletRequest request) throws IOException {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
throw new ResponseStatusException(BAD_REQUEST, "Input was not of type multipart");
}
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator fileIterator = upload.getItemIterator(request);
while (fileIterator.hasNext()) {
FileItemStream item = fileIterator.next();
if (!item.isFormField()) {
// Save the file
try {
return myFileStorageService.store(item.openStream());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
throw new ResponseStatusException(BAD_REQUEST, "Input did not contain a file");
}

这段代码工作得很好,但我的测试不是:

@MockBean
private MyFileStorageService myFileStorageService;
@Autowired
private MockMvc mockMvc;
@Test
void shouldUploadFile() throws Exception {
final InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("testfile.txt");
final MockMultipartFile testFile = new MockMultipartFile("file", "testfile.txt", null, inputStream);
doReturn("success!").when(myFileStorageService).store(testFile);
mockMvc.perform(multipart("/upload").file(testFile))
.andExpect(status().isOk())
.andExpect(content().string("success!"));
verify(myFileStorageService).store(testFile);
}

这会导致以下异常:

org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.init(FileItemIteratorImpl.java:189)
at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.getMultiPartStream(FileItemIteratorImpl.java:205)
at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.findNextItem(FileItemIteratorImpl.java:224)
at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.<init>(FileItemIteratorImpl.java:142)
at org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:252)
at org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload.getItemIterator(ServletFileUpload.java:134)
at com.lolmewn.FileUploadController.handleFileUpload(FileUploadController.java:128)
...

在我的配置中,我已经配置了以下内容:

spring:
servlet:
multipart:
enabled: false
max-file-size: -1
max-request-size: -1

我希望Spring会为我生成多部分边界,就像浏览器或邮差一样,这不是情况吗?我看到了许多类似的问题,其中大多数显式地将其内容类型设置为主要错误,但据我所知,我没有在任何地方设置内容类型,因此我希望Spring为我生成内容类型。

如果您使用的是默认应用程序。属性,然后在类的顶部添加@SpringBootTest注释,这将实例化它。如果使用应用程序测试之类的东西。属性需要包含@ActiveProfiles(test)
如果您使用配置类来表示它

@EnableConfigurationProperties(value = YourConfig.class)

编辑:改变

final MockMultipartFile testFile = new MockMultipartFile("file", "testfile.txt", null, inputStream);

final MockMultipartFile testFile = new MockMultipartFile("file", "testfile.txt",  
MediaType.MULTIPART_FORM_DATA_VALUE, inputStream); 

相关内容

  • 没有找到相关文章

最新更新