Java把Base64编码格式的图片下载到本地指定文件夹下




import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Decoder;

import java.io.*;
import java.util.Collections;
import java.util.List;
import java.util.Properties;


public class KafkaConsumer extends ShutdownableThread {
    private static final Logger LOG = LoggerFactory.getLogger(KafkaConsumer.class);

    public static void main(String[] args) {
        // 图片base64字符串
        String img = "";
        
        Properties properties = getProperties();
        String filePath = properties.getProperty("file.path");
        filePath = filePath.replace("\\", "\\\\");
        String xm = "张三";
        String idCard = "360111";
        try {
            boolean f = downloadImg(img, filePath, xm, idCard);
            if (f) {
                LOG.info("XM:" + xm + "图片下载成功");
            }
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
            LOG.info("XM:" + xm + "图片下载失败");
        }

    }

    /**
     * base64转图片
     *
     * @param base64str base64码
     * @param savePath  图片路径
     * @param xm        人员姓名
     * @param idCard    身份证明号码
     * @return boolean 判断是否成功下载到本地
     */
    private static boolean downloadImg(String base64str, String savePath, String xm, String idCard) throws IOException {
        //对字节数组字符串进行Base64解码并生成图片
        if (base64str == null) {
            return false;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        //Base64解码
        byte[] b = decoder.decodeBuffer(base64str);
        for (int i = 0; i < b.length; ++i) {
            //调整异常数据
            if (b[i] < 0) {
                b[i] += 256;
            }
        }

        // 判断路径是否不存在,不存在就创建文件夹
        File fileDir = new File(savePath);

        if (!fileDir.exists() && !fileDir.isDirectory()) {
            fileDir.mkdirs();
        }

        // 生成一个空文件,自定义图片的名字
        File file = new File(savePath + "\\" + idCard + "_" + xm + ".jpg");

        if (!file.exists()) {
            file.createNewFile();
        }

        //生成jpg图片
        OutputStream out = new FileOutputStream(file.getPath());
        out.write(b);
        out.flush();
        out.close();
        return true;
    }

    // 注意,这个是根据配置文件拿值,这个配置文件同项目打包后的jar包在同一目录
    private static Properties getProperties() {
        Properties properties = new Properties();
        // 获取项目根目录的绝对路径
        String rootPath = System.getProperty("user.dir") + File.separator;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(new File(rootPath + "application.properties"));
            properties = new Properties();
            properties.load(fileInputStream);
        } catch (FileNotFoundException e) {
            LOG.error(e.getMessage(), e);
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    LOG.error(e.getMessage(), e);
                }
            }
        }

        return properties;
    }

}

jar包、配置文件和生成图片的目录        D:\\test

Java把Base64编码格式的图片下载到本地指定文件夹下_第1张图片

application.properties

 

 把项目打成jar包

Java把Base64编码格式的图片下载到本地指定文件夹下_第2张图片

 Java把Base64编码格式的图片下载到本地指定文件夹下_第3张图片

本文参考:java把base64位编码转为File文件并存到本地_ekkcole的博客-CSDN博客_base64转file java

你可能感兴趣的:(#,java基础知识,java)