Springmvc中Ueditor改变图片存储路径

Springmvc中Ueditor改变图片存储路径

目的:改变Ueditor存图片的路径,我在本地测试的存在了D:\server\ueditor下面

下载Ueditor还有放在那里就不说了,怎么喜欢怎么来

我是重写的类来改变的存储路径
Controller代码如下
/***
* 测试页面
* @param request
* @param response
* @return
*/
@RequestMapping(value="/page", method = RequestMethod.GET)
public String page(HttpServletRequest request, HttpServletResponse response){
return “/travels/test”;
}

/***
 * ueditor.config.js需要指向的处理
 * @param request
 * @param response
 * @return
 * @throws FileNotFoundException
 */
@RequestMapping("/ueconfig")
@ResponseBody
public String configTest(HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException {
    String json = "";
    response.setContentType("application/json");
    // 获取项目在磁盘的绝对路径
    //String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
    //这里我没有用上面那个path,我获取的是项目路径,可以自行打印看效果,为了拼路径
    String path = context.getRealPath("/");
    System.out.println("path1-----------"+path);
  
    try {
        // 将josn文件读到Stirng,找到项目目录下的config存放的路径
        json = IOUtils.toString(new FileInputStream(new File(path + "/member/js/ueditor/jsp/config.json")), Charsets.UTF_8.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return json;
}

/***
 * 处理图片上传
 * @param upfile
 * @param request
 * @return
 * @throws IOException
 */
@RequestMapping("uploadImageData")
@ResponseBody
public Map<String,String> uploadImage(@RequestParam("upfile") MultipartFile upfile, HttpServletRequest request) throws IOException {
    //文件原名称
    String fileName = upfile.getOriginalFilename();
    // 保存文件的新名字
    String nowName = UUID.randomUUID()+fileName.substring(upfile.getOriginalFilename().lastIndexOf("."));
    String uploadPath = "";
    if(!upfile.isEmpty()){
    	String path = "D:/server/ueditor/jsp/upload/image/";
        String time = new SimpleDateFormat(PATH_FORMAT).format(new Date());
        System.out.println("time---"+time);
        
        path = path+time;
        File f = new File(path);
        if(!f.exists()){
            // 第一次上传文件新建文件夹
            f.mkdirs();
        }
        uploadPath = path+"/"+nowName;
        //按照路径新建文件
        File newFile = new File(uploadPath);
        if(!newFile.exists()){
            newFile.createNewFile();
        }
        //复制
        FileCopyUtils.copy(upfile.getBytes(), newFile);
    }
    
    String time = new SimpleDateFormat(PATH_FORMAT).format(new Date());
    //返回结果信息(UEditor官方要求这个json格式)
    Map<String,String> map = new HashMap<String,String >();
    //是否上传成功
    map.put("state", "SUCCESS");
    //现在文件名称
    map.put("title", nowName);
    //文件原名称
    map.put("original", fileName);
    //文件类型 .+后缀名
    map.put("type", fileName.substring(upfile.getOriginalFilename().lastIndexOf(".")));
    //文件路径
    System.out.println("uploadPath---"+uploadPath);
    // map.put("url", uploadPath);    // 浏览器不能直接访问项目外目录的图片等文件,需要做虚拟路径映射
    map.put("url", "/PathImage/"+time+"/"+nowName);  // 这个路径的 /PathImage/ 是在配置类里指定的映射到本地的绝对路径,我是在tomcat里配置的映射
    //文件大小(字节数)
    map.put("size", upfile.getSize()+"");
    return map;
}

然后开始前台
测试页面

提交

ueditor.config.js里
window.UEDITOR_CONFIG = {

    //为编辑器实例添加一个路径,这个不能被注释
    UEDITOR_HOME_URL: URL

    // 服务器统一请求接口路径
    , serverUrl:  "http://localhost:8080/项目名称/ue/ueconfig"

tomcat里的server.xml里加上映射

运行起来
图片回显

本地测试成功之后,服务器同理,如果有问题,欢迎留言,最近刚弄好这个,印象比较深刻一些## 标题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值