summernote 图片上传方法重写。附前后台所有代码。

网上很多关于summernote 图片上传 重写的方法,就是因为有很多,所有有一些乱的,或者说比较复杂的。今天我写的这个,非常的简单,从js,到后台 处理的方法,都有。希望能给需要的人一些帮助

js代码。

 $('#add_summernote').summernote({  
                   lang: 'zh-CN', // default: 'en-US'  
                    focus:true,  
                 onImageUpload: function(files,editor,welEditable) {  
                     sendFile(files[0],editor,welEditable);  
                  }  
      });  


function sendFile(files,editor,welEditable){  

    data = new FormData();
    data.append("file", files);
    
    $.ajax({
        data: data,
        type: "POST",
        url: ctx+"/goods/summernoteImage",
        cache: false,
        contentType: false,
        processData: false,
         success: function (data) {
              editor.insertImage(welEditable, data);
          }, 
            error:function(){
                layer.alert('上传失败!');
                return;
            }
            
        });
}

后台代码

@RequestMapping(value="summernoteImage", headers ="Accept=application/json",produces ="application/json;charset=UTF-8")
        @ResponseBody
        public String summernoteImage(HttpServletRequest request, HttpServletResponse response, ModelMap model,MultipartFile file) throws Exception{
            String result="";
        try {
            if(file!=null){
                    String path = UploadUtil.commonUpload(1, file);
                 
                    return path;
              }
          

        } catch (Exception e) {
            result="";
            e.printStackTrace();
            
        }
         return result;
        }

图片上传方法

public class UploadUtil {
    
    
    public static String commonUpload(Integer type, MultipartFile filedata) throws IOException {
        String fileName = Tool.getRandomOrderNoOrFileName();// 获取时间戳 我是以获取时间戳来命名上传的文件的
        String suffix = filedata.getOriginalFilename().substring  
                (filedata.getOriginalFilename().lastIndexOf(".")); 
        String name = fileName +suffix;
        String name_x = fileName + "_x"+suffix;
        String imgAddr = "";
    
        String root = "";
        switch (type) {
        case 1:
            root = Tool.GetValueByKey("goodsImager");//这里我是通过system.Config.properties。配置的路径,然后根据名称来获得 图片上传的路径;  你可以,直接在这里写好路径;
            imgAddr = "/ml/goods" + "/" + name;    //最后返回的 图片路径
            break;
        }
        

        File file = new File(root, name_x);
        //
        File rootPath = new File(root);
        if (!rootPath.exists()) {
            rootPath.mkdir();
          }
        File destFile = new File(root, name);
        byte[] bytes = filedata.getBytes();
        FileOutputStream fos = new FileOutputStream(destFile); // 写入文件
        fos.write(bytes);
        fos.close();
        if("png,jpg,PNG,JPG,jpeg".contains(suffix)){
           NarrowImage.writeHighQuality(
                NarrowImage.zoomImage(destFile.getAbsolutePath()),
                file.getAbsolutePath());
        }
        return imgAddr;
    }
    
    
}


工具类
public class Tool {
     static Properties props;
        static{
            props = new Properties();
            try {
                props.load(Tool.class.getClassLoader().
                        getResourceAsStream("/resources/systemConfig.properties"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    //根据 key 获取systemConfig配置
   public static String GetValueByKey( String key) {
           String value = props.getProperty(key);
           return value;             
    }

// 获取时间戳
   public  static String getRandomOrderNoOrFileName() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

        String yid = sdf.format(new Date()) + (new Random().nextInt(100));

        return yid;
    }
}



评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值