SSM框架:SpringMVC上传图片出现报错解决Expected MultipartHttpServletRequest: is a MultipartResolver configured?

在构建项目上传图片时出现如下报错:
在这里插入图片描述
上传图片的controller层:

package controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import util.Result;
import util.ResultUtil;
/**
 *@description  
 *@author    Darren_Hu
 *@date  	2019年1月16日---上午11:09:27
 */
@Controller
public class UtilController {

	//上传图片并返回图片路径(json格式),接收的图片名和wangEditor定义的图片名保持一致MultipartFile  
	@RequestMapping(value = "uploadImg",method = RequestMethod.POST)
	public @ResponseBody Object uploadImg(@RequestParam("myFileName")MultipartFile myFileName,HttpSession session,HttpServletRequest request) throws IllegalStateException, IOException{
		String realName = "";
		String realPath = "";
		if (myFileName != null) {
			String fileName = myFileName.getOriginalFilename();
			String fileNameExtension = fileName.substring(fileName.indexOf("."), fileName.length());
			// 生成实际存储的真实文件名

			realName = UUID.randomUUID().toString() + fileNameExtension;

			// "/upload"是你自己定义的上传目录
			
//			String realPath = session.getServletContext().getRealPath("/upload");
			realPath = session.getServletContext().getRealPath("/upload");
//			realPath = "E:\\work\\Java\\DMS\\WebContent\\upload";
			File uploadFile = new File(realPath, realName);
			myFileName.transferTo(uploadFile);
		}
//		String [] str = {request.getContextPath() + "/upload/" + realName};
//		String [] strPath = {realPath + "\\" + realName};
//		List<String> stringB = Arrays.asList(strPath);
//		List<Map<String, Object>> resultMap = new ArrayList<Map<String,Object>>();
		Map<String, Object> resultMap = new HashMap<String, Object>();
//		String resultStr =  ResultUtil.success(strPath).toString();
		resultMap.put("url", request.getContextPath() + "/upload/" + realName); //【注意】这个配置本地路径前端无法加载
		System.out.println(resultMap);
		return resultMap;
	}
	
}

使用postman测试:
在这里插入图片描述
测试fail了,后台出现报错了。

解决办法:
在springmvc.xml配置文件中加上如下配置即可。

<!-- 设置上传文件最大值   1M=1*1024*1024(B)=1048576 bytes -->  
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
	    <property name="maxUploadSize" value="10485760" />  
	</bean>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值