最近在使用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接口-------------------");
}
看樣子每個方面都很好,但是有點問題是在上傳檔案的時候背景擷取不到檔案,我打斷點看了看發現的是: