在构建项目上传图片时出现如下报错:
上传图片的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>