1.先说一下百度富文本的原理,理解了原理,接下来就好搞 了,初始化加载百度富文本编辑器的时候,会发送一个统一请求,然后读取config.josn后返回里面的数据。
2.上传就是根据config.json里面配置的各种url来上传了,
/**
* ClassName:UeditorController Function: TODO ADD FUNCTION. Reason: TODO ADD
* REASON. Date: 2017年2月25日 上午11:12:20
*
* @author likaixuan
* @version V1.0
* @since JDK 1.7
* @see
*/
@RequestMapping("/ueditor")
@Controller
public class UeditorController {
@Value("${upload.image}")
private String savePath;
@Value("${hikpedia.image.url}")
private String preUrl;
@RequestMapping(value="/upload",method=RequestMethod.GET)
@ResponseBody
public void config(HttpServletRequest request,HttpServletResponse response) throws IOException {
String actionName = request.getParameter("action");
if ("config".equals(actionName)) {
InputStream in = new FileInputStream(UeditorController.class.getResource("/").getPath() + "config.json");
OutputStream out = response.getOutputStream();
IOUtils.copy(in, out);
}
}
@RequestMapping(value="/upload",method=RequestMethod.POST)
@ResponseBody
public Map<String,String> upload(HttpServletRequest request, HttpServletResponse response,MultipartFile upfile) throws UnsupportedEncodingException{
Map<String, String> result = new HashMap<String, String>();
String url = FileUploadUtil.singleFileUpload(upfile, savePath, preUrl);
String state = "SUCCESS";
// 返回json的对象
result.put("url", url);
result.put("size", String.valueOf(upfile.getSize()));
result.put("type", upfile.getContentType());
result.put("title", upfile.getOriginalFilename());
result.put("original", upfile.getName());
result.put("name", upfile.getOriginalFilename());
result.put("state", state);
return result;
}
}
import java.io.File;
import java.io.IOException;
import org.springframework.web.multipart.MultipartFile;
/**
* ClassName:FileUploadUtil Function: TODO ADD FUNCTION. Reason: TODO ADD
* REASON. Date: 2017年3月2日 下午7:37:12
*
* @author likaixuan
* @version V1.0
* @since JDK 1.7
* @see
*/
public class FileUploadUtil {
private static final String SEPARATOR = "/";
/**
* singleFileUpload:(单个文件上传)
* TODO(返回文件上传后所在路径)
*
* @return fileurl
* @since JDK 1.7
*/
public static String singleFileUpload(MultipartFile file,String savePath,String prePath) {
String filePath = "";// 文件上传后,可访问的路径
try {
//判断savePath路径最后是否带/,如果不带追加
String lastStr = savePath.substring(savePath.length()-2);
if(lastStr.contains("/")){
filePath = savePath + DateUtil.getNowYearMoth() + SEPARATOR
+ file.getOriginalFilename();
}else{
savePath+=SEPARATOR;
filePath = savePath +SEPARATOR+ DateUtil.getNowYearMoth() + SEPARATOR
+ file.getOriginalFilename();
}
// 前端传入mulFileSource
// 创建压缩前源文件
File fileSourcePath = new File(savePath + DateUtil.getNowYearMoth());
File fileSource = new File(fileSourcePath, file.getOriginalFilename());
if (!fileSourcePath.exists()) {
fileSourcePath.mkdirs();
}
file.transferTo(fileSource);
} catch (IllegalStateException e) {
throw new RuntimeException();
} catch (IOException e) {
throw new RuntimeException();
}
/*return savePath+ DateUtil.getNowYearMoth() + SEPARATOR
+ file.getOriginalFilename();*/
return prePath+SEPARATOR+DateUtil.getNowYearMoth()+SEPARATOR+file.getOriginalFilename();
}
}