SpringBoot 模拟附件上传提交,提示ClassNotFoundException: org.springframework.mock.web.MockMultipartFile
今天在编写模拟Hadoop 文件上传功能,提示如下错误信息=ClassNotFoundException: org.springframework.mock.web.MockMultipartFile模拟功能说明:采用spring-test.jar 包中的MockMultipartFile 类,模拟文件上传功能,但是在执行相关业务逻辑代码,提示类找不到。后来检查maven 依赖是我发现s...
·
今天在编写模拟Hadoop 文件上传功能,提示如下错误信息=ClassNotFoundException: org.springframework.mock.web.MockMultipartFile
模拟功能说明:采用spring-test.jar 包中的MockMultipartFile 类,模拟文件上传功能,但是在执行相关业务逻辑代码,提示类找不到。后来检查maven 依赖是我发现spring-test.jar 生效模式是test(测试模式),速度移除spring-test 的模式限制。
maven 配置文件如下:
报错依赖:
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.3.RELEASE</version>
<scope>test</scope>
</dependency>
解决后的依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
测试功能代码:
@ApiOperation(httpMethod = "POST", value = "新建文件")
@RequestMapping(value = "/createFile", method = { RequestMethod.POST }, produces = "application/json;charset=UTF-8")
@ResponseBody
public JreportResponse createFile(
@RequestBody @ApiParam(name = "JSON对象", value = "json格式对象", required = true) JSONObject entity) {
FileInputStream inputStream = null;
MultipartFile file = null;
try {
inputStream = new FileInputStream("C:\\data\\words.txt");
file = new MockMultipartFile("test.txt", inputStream);
} catch (Exception e) {
// TODO Auto-generated catch block
logger.error(e.getMessage());
}finally{
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error(e.getMessage());
}
}
service.createFile(entity.getString("path"),file);
return JreportResponse.ok();
}

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)