1.首先在jsp中覆盖UEidtor获取路径的方法
UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
UE.Editor.prototype.getActionUrl = function(action) {
if (action == 'uploadimage') {
return WEBPATH+'/webCol/uploadImage';
} else if (action == 'uploadvideo') {
return '';
} else {
return this._bkGetActionUrl.call(this, action);
}
}
2.在springmvc配置文件中配置多文件上传
<!-- spring多文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
<property name="maxUploadSize">
<!-- 上传文件大小限制为50M,50*1024*1024 -->
<value>52428800</value>
</property>
<!-- 内存大小限制为8M,8*1024*1024 -->
<property name="maxInMemorySize">
<value>8388608</value>
</property>
<property name="resolveLazily" value="true"/>
</bean>
3.在controller中定义上传和读取文件的方法
/**
* ueditor上传图片重写
*
* @param upfile
* @param request
* @return
* @throws IOException
*/
@RequestMapping("/uploadImage")
@ResponseBody//这里upfile是config.json中图片提交的表单名称
public Map<String,String> uploadImage(@RequestParam("upfile") CommonsMultipartFile upfile,HttpServletRequest request) throws IOException{
//文件原名称
String fileName=upfile.getOriginalFilename();
//文件后缀名
String type = fileName.substring(upfile.getOriginalFilename().lastIndexOf("."));
String nowName=DateUtil.format(new Date(), "yyyyMMdd") + "_" + new Date().getTime() + type;
if(!upfile.isEmpty()){
//上传位置路径
String path0 = "D:\\static\\ueditor\\jsp\\upload\\image\\"+nowName;
//按照路径新建文件
File newFile = new File(path0);
//复制
FileCopyUtils.copy(upfile.getBytes(), newFile);
}
//返回结果信息(UEditor需要)
Map<String,String> map = new HashMap<String,String >();
//是否上传成功
map.put("state", "SUCCESS");
//现在文件名称
map.put("title", nowName);
//文件原名称
map.put("original", fileName);
//文件类型 后缀名
map.put("type", type);
//文件路径
map.put("url", "/webCol/"+nowName+"/getImage");
//文件大小(字节数)
map.put("size", upfile.getSize()+"");
return map;
}
/**
* ueditor读取文件重写
*/
@RequestMapping("{imgName}/getImage")
public void readImg(@PathVariable("imgName") String imgName, HttpServletResponse response) throws Exception {
//设置文件的返回类型
response.setContentType("image/*");
//文件路径(windows下是\\,linux下是//,都必须是绝对路径)
String imgPath="D:\\static\\ueditor\\jsp\\upload\\image\\"+imgName;
//java中用File类来表示一个文件
File image = new File(imgPath);
//测试这个文件路径是否存在(也就是这个文件是否存在)
if (!image.exists()) {
return;
}
//FileUtils.readFileToByteArray(File file)把一个文件转换成字节数组返回
response.getOutputStream().write(FileUtils.readFileToByteArray(image));
//java在使用流时,都会有一个缓冲区,按一种它认为比较高效的方法来发数据:
//把要发的数据先放到缓冲区,缓冲区放满以后再一次性发过去,而不是分开一次一次地发.
//而flush()表示强制将缓冲区中的数据发送出去,不必等到缓冲区满.
response.getOutputStream().flush();
response.getOutputStream().close();
}