记录springmvc+ueditor1.4.3上传附件问题

百度富文本编辑器地址:http://ueditor.baidu.com/website/index.html

1.UEditor上传目录

[img]http://dl2.iteye.com/upload/attachment/0111/2524/d6675fa1-f844-33a7-a606-c3570fd57374.jpg[/img]

2.在ueditor.config.js中配置serverUrl: "/resources/plugin/ueditor"

3.后台配置访问路径

@RequestMapping("resources/plugin/ueditor")
public void baiduEdit(HttpServletRequest request, HttpServletResponse response) {
try {
request.setCharacterEncoding("utf-8");
response.setHeader("Content-Type", "text/html");

String rootPath = request.getServletContext().getRealPath("/");
logger.debug("================>{}", rootPath);
//针对配置百度上传附件读取配置文件
ActionMap.mapping.put("config", ActionMap.CONFIG);
//上传文件
ActionMap.mapping.put("uploadfile", ActionMap.UPLOAD_FILE);
ActionEnter actionEnter = new ActionEnter(request, rootPath);
response.getWriter().write(actionEnter.exec());
} catch (Exception e) {
e.printStackTrace();
}
}

配置成功后,可以启动项目, 执行url
http://localhost:8080/longchoudai/resources/plugin/ueditor?action=config
可以看到你配在config.json中的Json数据

4.上传附件时候修改原百度BinaryUploader.java 在目录com.baidu.ueditor.upload下面

public class BinaryUploader {
private static Logger logger = LogManager.getLogger();

/**
* 保存文件
* @author lance
* 2015年8月27日 下午2:26:49
*/
public static final State save(HttpServletRequest request, Map<String, Object> conf) {
boolean isAjaxUpload = request.getHeader("X_Requested_With") != null;
if (!ServletFileUpload.isMultipartContent(request)) {
return new BaseState(false, 5);
}
ServletFileUpload upload = new ServletFileUpload(
new DiskFileItemFactory());
if (isAjaxUpload) {
upload.setHeaderEncoding("UTF-8");
}
try {
String savePath = request.getServletContext().getRealPath("/")+"/attached/file/"+JodaTimeUtils.dateToString(new Date(), null)+"/";
String saveUrl = request.getContextPath() + "/attached/file/";

//上传附件目录
Path path = Paths.get(savePath);
if(!Files.isDirectory(path)){
try {
Files.createDirectories(path);
} catch (IOException e) {
logger.error("百度上传附件创建上传文件夹错误: {}", e.getMessage());
}
}
//重新命名文件名字
String newFileName = "", fileExt = "", fileName = "";
CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(request.getSession().getServletContext());
//检查form中是否有enctype="multipart/form-data"
if(multipartResolver.isMultipart(request)) {
//将request变成多部分request
MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
//获取multiRequest 中所有的文件名
Iterator<String>it=multiRequest.getFileNames();
//遍历文件
while(it.hasNext()) {
MultipartFile file=multiRequest.getFile(it.next().toString());
if(file != null){
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
fileName = file.getOriginalFilename();
fileExt = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
//新文件名称
newFileName = savePath + df.format(new Date()) + "_" + new Random().nextInt(1000) + fileExt;
saveUrl = saveUrl + df.format(new Date()) + "_" + new Random().nextInt(1000) + fileExt;
if (!validType(fileExt, (String[])conf.get("allowFiles"))) {
return new BaseState(false, 8);
}
try {
//上传文件
file.transferTo(new File(newFileName));
} catch (IllegalStateException | IOException e) {
logger.error("百度上传附件保存文件错误: {}", e.getMessage());
}
}
}
}

State storageState = new BaseState(Boolean.TRUE);
if (storageState.isSuccess()) {
storageState.putInfo("url", PathFormat.format(saveUrl));
storageState.putInfo("type", fileExt);
storageState.putInfo("original", fileName);
}
return storageState;
} catch (Exception e) {
logger.error("百度上传附件文件错误: {}", e.getMessage());
}
return new BaseState(false, 4);
}

private static boolean validType(String type, String[] allowTypes) {
List<String> list = Arrays.asList(allowTypes);
return list.contains(type);
}
}



[img]http://dl2.iteye.com/upload/attachment/0111/2532/a1fe45b1-f284-3167-91d8-ec15562c0fd3.jpg[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值