java将页面传的base64图片写入找指定路径下

java将页面传的base64图片写入找指定路径下

    public  String GetImg(String img){
    
    


        /**
         * 1.获取到后面到数据
         */
        String data = img;
        String base64Data =  data.split(",")[1];
        /**
         * 2.解码成字节数组
         */
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] bytes = decoder.decode(base64Data);
        /**
         * 3.字节流转文件
         */
        FileOutputStream fos = null;
        int uuid = Util.getUUID();
        String filePath = "D:\\"+uuid+".jpg";
        try {
    
    
            fos = new FileOutputStream(filePath);
            fos.write(bytes);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            if (fos != null){
    
    
                try {
    
    
                    fos.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
        return filePath;
    }

猜你喜欢

转载自blog.csdn.net/weixin_43851064/article/details/109639924