SpringBoot 模拟附件上传提交,提示ClassNotFoundException: org.springframework.mock.web.MockMultipartFile

你猜 阅读:2536 2021-03-31 16:55:54 评论:0

今天在编写模拟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(); 
	}

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号