用cropper.js裁剪图片并上传到服务器,解析base64转存图片到本地

今天要写上传图片功能,研究了一下cropper。将图片上传服务器并保存到本地

html




    基于cropper.js的图片裁剪
    
    

    
    




然后是控制器

 @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public String update(String imgBase64,String fileFileName) throws UnsupportedEncodingException {

        imgBase64 = URLDecoder.decode(imgBase64,"UTF-8");
        System.out.println(imgBase64+","+fileFileName);

        imgBase64 = imgBase64.substring(22);

        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgBase64);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }

            OutputStream out = new FileOutputStream("D:/image/headPortrait/photo.jpg");
            out.write(b);
            out.flush();
            out.close();

        } catch (Exception e) {
            return "false";
        }

        return "hi";
    }

下面是我的demo
demo

你可能感兴趣的:(图片裁剪)