重写BinaryUploader,升级common-io 2.8
package com.baidu.ueditor.upload;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.baidu.ueditor.PathFormat;
import com.baidu.ueditor.define.AppInfo;
import com.baidu.ueditor.define.BaseState;
import com.baidu.ueditor.define.FileType;
import com.baidu.ueditor.define.State;
public class BinaryUploader
{
public static final State save(HttpServletRequest request, Map<String, Object> conf) {
try {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile multipartFile = multipartRequest.getFile(conf.get("fieldName").toString());
String savePath = (String) conf.get("savePath");
String originFileName = multipartFile.getOriginalFilename();
String suffix = FileType.getSuffixByFilename(originFileName);
originFileName = originFileName.substring(0,originFileName.length() - suffix.length());
savePath = savePath + suffix;
long maxSize = ((Long) conf.get("maxSize")).longValue();
if (!validType(suffix, (String[]) conf.get("allowFiles"))) {
return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE);
}
savePath = PathFormat.parse(savePath, originFileName);
String physicalPath = (String)conf.get("physicsPath");
if (physicalPath != null && !"".equals(physicalPath)) {
physicalPath = physicalPath + savePath;
} else {
physicalPath = conf.get("rootPath") + savePath;
}
File targetFile = new File(physicalPath);
if(!targetFile.getParentFile().exists()){
targetFile.getParentFile().mkdirs();
}
//State storageState = StorageManager.saveFileByInputStream(multipartFile.getInputStream(),savePath, maxSize);
State storageState = StorageManager.saveFileByInputStream(multipartFile.getInputStream(),physicalPath, maxSize);
if (storageState.isSuccess()) {
storageState.putInfo("url", PathFormat.format(savePath));
storageState.putInfo("type", suffix);
storageState.putInfo("original", originFileName + suffix);
}
return storageState;
}catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
return new BaseState(false, AppInfo.IO_ERROR);
}
private static boolean validType(String type, String[] allowTypes) {
List<String> list = Arrays.asList(allowTypes);
return list.contains(type);
}
}
//下面和这个问题无关,只是记录
package com.baidu.ueditor.spring;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 编辑器业务响应
* @author wall
* @date 2020-11-30
*/
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baidu.ueditor.ActionEnter;
@Controller
@RequestMapping("/ueditor/ueditor.do")
public class UeditorController {
/**
* 读取配置
* @author wall
* @date 2020-11-30
*/
@RequestMapping(value = "action=*")
@ResponseBody
public void ueditorAction(HttpServletRequest request,HttpServletResponse response) {
try {
request.setCharacterEncoding( "utf-8" );
response.setHeader("Content-Type" , "text/html");
String rootPath =request.getServletContext().getRealPath( "/" );
ActionEnter ae = new ActionEnter(request,rootPath);
PrintWriter out = response.getWriter();
out.write(ae.exec());
} catch (IOException e) {
e.printStackTrace();
}
}
}