码迷,mamicode.com
首页 > 其他好文 > 详细

base64和图片的相互转换

时间:2020-02-25 17:27:14      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:encode   enc   malformed   NPU   safe   capture   json   for   groovy   

package czc.superzig.modular.utils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.sun.jna.NativeLong;

import cn.hutool.core.codec.Base64Decoder;
import czc.superzig.common.operatingtable.base.entity.Result;
import czc.superzig.modular.system.operatingtable.entity.CameraBlock;
import czc.superzig.modular.utils.camera.DemoCapture;
import groovyjarjarantlr.collections.List;

public class Base64 {
    private static final String separator = "/";
    private final static ExecutorService executor = Executors.newCachedThreadPool();//启用多线程

    //获取base64字符串
    public static String encodeBase64(String filaName,boolean isSafe) {
        if(StringUtils.isBlank(filaName)){
            throw new NullPointerException();
        }
        InputStream in = null;
        byte[] data = null;
        String encodedText=null;
        //读取图片字节数组
        try {
            in = new BufferedInputStream(new FileInputStream(filaName));
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //对字节数组Base64编码
        
        if(isSafe){
            java.util.Base64.Encoder encoder = java.util.Base64.getUrlEncoder();
            encodedText = encoder.encodeToString(data);
        }else{
            BASE64Encoder encoder = new BASE64Encoder();
            encodedText=encoder.encode(data);
            encodedText=encodedText.replaceAll("[\\s*\t\n\r]", "");
        }
        return encodedText;
    }
 
    //解析base64
    public static String decodeBase64(String base64,String filePath,String suffix,boolean isSafe){
        if(StringUtils.isBlank(base64)||StringUtils.isBlank(filePath)||StringUtils.isBlank(suffix)){
            throw new NullPointerException();
        }
        OutputStream out=null;
        String fileName=null;
        try {
            byte[] b=new byte[2048];
            if(isSafe){
                java.util.Base64.Decoder decoder = java.util.Base64.getUrlDecoder();
                b = decoder.decode(base64);
            }else{
                BASE64Decoder decoder = new BASE64Decoder();
                b = decoder.decodeBuffer(base64.substring(base64.indexOf(",") + 1));
            }
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            File file=new File(filePath);
            if(!file.exists()){
                file.mkdirs();
            }
            fileName=filePath+System.currentTimeMillis()+"."+suffix;
            out = new BufferedOutputStream(new FileOutputStream(fileName));
            out.write(b);
            out.flush();
        }catch (Exception e) {

        }finally {
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }



        return fileName;
    }
}

 

base64和图片的相互转换

标签:encode   enc   malformed   NPU   safe   capture   json   for   groovy   

原文地址:https://www.cnblogs.com/xiatc/p/12362337.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!