百度富文本和springboot集成

本文介绍百度富文本编辑器的工作原理及如何进行图片上传配置。解析了初始化加载时的统一请求过程,并详细展示了通过Spring MVC实现文件上传的具体代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
    }
    
    
    

}

 

转载于:https://my.oschina.net/likaixuan0/blog/1030998

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值