ueditor 图片的上传、回显 config.json在linux下的读取

ueditor用在springboot项目中图片的上传路径和回显路径设置
在这个有几个重要的点
  1. 上传路径的配置
  2. 图片路径访问前缀
  3. springboot常量配置
  4. springboot加载静态资源的方法

    这里我是把上传的图片保存到了本地路径下,没有放在项目文件下

    添加ueditor包和js这些准备工作略过。。。

在application.properties里添加常量

spring.articlepic=/articleupload

设置路径为自定义路径

@Value("${spring.articlepic}")
      private String articleupload;

@RequestMapping(value="/admin/ueditor",method={RequestMethod.POST,RequestMethod.GET})
    public void ueditorConfig(HttpServletRequest request,HttpServletResponse response){
        response.setContentType("application/json");
        //String rootPath = request.getSession().getServletContext().getRealPath("/");
        String rootPath =articleupload;
        try{
            String exec = new ActionEnter(request,rootPath).exec();
            PrintWriter writer = response.getWriter();
            writer.write(exec);
            writer.flush();
            writer.close();
        } catch (IOException e){
            e.printStackTrace();
        }
    }

WebConfig.java设置虚拟路径指向本地存储路径

  @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");//项目下里静态资源路径用classpath
        registry.addResourceHandler("/articlepath/**").addResourceLocations("file:/articleupload/");//我传到本地其它路径 用file
        super.addResourceHandlers(registry);
    }

这里的articlepath就指向本地的/articleupload路径下

最后,在config.json里添加路径前缀和图片保存路径

 /* 上传图片配置项 */
    "imageActionName": "uploadimage", /* 执行上传图片的action名称 */
    "imageFieldName": "upfile", /* 提交的图片表单名称 */
    "imageMaxSize": 2048000, /* 上传大小限制,单位B */
    "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 上传图片格式显示 */
    "imageCompressEnable": true, /* 是否压缩图片,默认是true */
    "imageCompressBorder": 1600, /* 图片压缩最长边限制 */
    "imageInsertAlign": "none", /* 插入的图片浮动方式 */
    "imageUrlPrefix": "http://localhost:8080/articlepath", /* 图片访问路径前缀 */
    "imagePathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
                                /* {filename} 会替换成原文件名,配置这项需要注意中文乱码问题 */
                                /* {rand:6} 会替换成随机数,后面的数字是随机数的位数 */
                                /* {time} 会替换成时间戳 */
                                /* {yyyy} 会替换成四位年份 */
                                /* {yy} 会替换成两位年份 */
                                /* {mm} 会替换成两位月份 */
                                /* {dd} 会替换成两位日期 */
                                /* {hh} 会替换成两位小时 */
                                /* {ii} 会替换成两位分钟 */
                                /* {ss} 会替换成两位秒 */
                                /* 非法字符 \ : * ? " < > | */
                                /* 具请体看线上文档: fex.baidu.com/ueditor/#use-format_upload_filename */

“imageUrlPrefix”: “http://localhost:8080/articlepath” 这里端口后的articlepath就是刚才所注册的虚拟路径,这样就OK了,图片的上传和回显都没问题了
这里写图片描述
######windows下本机测试没问题 接下来再看打包到Linux下有问题没

######现在上传Jar包到linux下测试,然后发现图片没有上传成功
经过调试输出后发现,在Linux下根本没有读取到config.json文件
这应该是读取文件路径的问题,在windows下直接可以通过路径读取到.json文件,
但是在linux下它在Jar包里,这个时候再通过上面的方法进行就不行了。。。
######这里我改动了一下ueditor包的源码
在ConfigManager.java里进行改动
添加一个读取config.json内容的函数,返回json文件的字符串内容

  private String getConfigContent(){
        InputStream in = this.getClass().getResourceAsStream("/config.json");
        String result = null;
        try {
              StringBuffer sb = new StringBuffer();
              byte[] b = new byte[1024];
              int length=0;
              while(-1!=(length=in.read(b))){
                 sb.append(new String(b,0,length,"utf-8"));
              }
              result = sb.toString().replaceAll("/\\*(.|[\\r\\n])*?\\*/","");
           } catch (IOException e) {
              e.printStackTrace();
           }finally {
              try {
                 in.close();
              } catch (IOException e) {
                 e.printStackTrace();
              }
           }
        return result;
    }
然后在initEnv函数修改一下,直接得到config.json文件的内容,不用再通过它原来的根据路径去获取的方法
    private void initEnv () throws FileNotFoundException, IOException {

        File file = new File( this.originalPath );

        if ( !file.isAbsolute() ) {
            file = new File( file.getAbsolutePath() );
        }

        this.parentPath = file.getParent();

        String configContent = this.getConfigContent();
        System.out.println("json内容:"+configContent);
        try{
            JSONObject jsonConfig = new JSONObject( configContent );
            this.jsonConfig = jsonConfig;
        } catch ( Exception e ) {
            this.jsonConfig = null;
        }

    }

这样在linux下就能读取到config.json文件了,就能实现图片的上传

这里是已经把jar包上传到服务器上了,所以要让图片回显,还得把config.json文件里图片上传项的 “imageUrlPrefix”的路径l改成服务器的地址
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值