最近在使用wangEditor做上传文件的功能
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiI0gTMx81dsQWZ4lmZf1GLlpXazVmcvwFciV2dsQXYtJ3bm9CX9s2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xCMy81dvRWYoNHLwEzX5xCMx8FesU2cfdGLwMzX0xiRGZkRGZ0Xy9GbvNGLpZTY1EmMZVDUSFTU4VFRR9Fd4VGdsYTMfVmepNHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-cmbw5SNwMDM5EmMyUjY5cjMhZzYyYzX5ADOwcTM3AzLcdDMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.png)
但是有一点非常的让人难受,我在上传文件的时候每个方面代码都是没有问题的
比如前台:
var editor = new wangEditor('#txtDiv');
editor.customConfig.uploadImgServer = serviceUrl+'/Shopping/filecontroller/uploadfile';
editor.customConfig.uploadImgFileName = 'myFileName';
editor.customConfig.showLinkImg = false;
editor.create();
这样我上传定义了上传文件的接口路径,后台我使用的是SpringMVC这个框架,接口我是这样写的:
@RequestMapping(value = "/uploadfile")
public void uploadFile(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
log.info("-------------------开始调用上传文件uploadfile接口-------------------");
String path = this.getClass().getClassLoader().getResource("/").getPath();
int index = path.indexOf("Shopping");
path = path.substring(0, index + "Shopping".length()) + "/WebContent/resources/upload/";
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload sfu = new ServletFileUpload(factory);
sfu.setHeaderEncoding("UTF-8"); // 处理中文问题
sfu.setSizeMax(1024 * 1024); // 限制文件大小
String fileName="";
try {
List<FileItem> fileItems=sfu.parseRequest(request);
for(FileItem item:fileItems){
fileName=UUID.randomUUID().toString()+item.getFieldName().substring(item.getFieldName().lastIndexOf('.'), item.getFieldName().length());
item.write(new File(path+"//"+fileName));
}
} catch (Exception e) {
e.printStackTrace();
}
//获取图片url地址
String imgUrl = "resources/upload/"+fileName;
response.setContentType("text/text;charset=utf-8");
PrintWriter out = response.getWriter();
out.print(imgUrl);
out.flush();
out.close();
log.info("-------------------结束调用上传文件uploadfile接口-------------------");
}
看样子每个方面都很好,但是有点问题是在上传文件的时候后台获取不到文件,我打断点看了看发现的是: