工作小记

转载:Base64Utils实现base64码转换为文件流

2020-03-29  本文已影响0人  叫子非鱼啊

有时候,我们会遇到将文件转化为base64编码后的字符串,传递到服务器上,然后让服务器处理操作。我写了这个utils包,实现了文件和base64的相互转化。

懒汉模式
1、通过双判断的方式,这种方式之所以是双判断,就是在a和b同时调用了这个方法,并都堵在了锁上,如果a线完事,b肯定就进去了, 如果不加上判断,b又实例化了一次。
2、通过直接给实例化方法加锁的方式,这种方式比较简单粗暴,但是效率教双判断低。

实现代码
package com.yellowcong.voice.utils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;

/**
 * 创建日期:2018年1月14日<br/>
 * 创建时间:下午6:50:53<br/>
 * 创建者 :yellowcong<br/>
 * 机能概要:用于Base64解码和编码
 */
public class Base64Utils {

    private static Base64Utils utils = null;

    private Base64Utils(){

    }

    /**
     * 创建日期:2018年1月14日<br/>
     * 创建时间:下午7:23:30<br/>
     * 创建用户:yellowcong<br/>
     * 机能概要:单利 ,懒汉模式
     * @return
     */
    public static Base64Utils getInstance(){
        if(utils == null){
            synchronized (Base64Utils.class) {
                if(utils == null ){
                    utils = new Base64Utils();
                }
            }
        }
        return utils;
    }
    /**
     * 创建日期:2018年1月14日<br/>
     * 创建时间:下午7:47:12<br/>
     * 创建用户:yellowcong<br/>
     * 机能概要:获取文件的大小
     * @param inFile 文件
     * @return 文件的大小
     */
    public int getFileSize(File inFile){
        InputStream in = null;

        try {
            in = new FileInputStream(inFile);
            //文件长度
            int len = in.available();
            return len;
        }catch (Exception e) {
            // TODO: handle exception
        }finally{
            try {
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return -1;
    }

    /**
     * 创建日期:2018年1月14日<br/>
     * 创建时间:下午6:57:53<br/>
     * 创建用户:yellowcong<br/>
     * 机能概要:将文件转化为base64
     * @return
     * @throws Exception 
     */
    public String file2Base64(File inFile){

        //将文件转化为字节码
        byte [] bytes = copyFile2Byte(inFile);
        if(bytes == null){
            return null;
        }

        //base64,将字节码转化为base64的字符串
        String result = Base64.getEncoder().encodeToString(bytes);
        return result;
    }

    /**
     * 
     * 创建日期:2018年1月14日<br/>
     * 创建时间:下午7:09:02<br/>
     * 创建用户:yellowcong<br/>
     * 机能概要:将文件转化为字节码
     * @param inFile 
     * @return
     */
    private byte [] copyFile2Byte(File inFile){
        InputStream in = null;

        try {
            in = new FileInputStream(inFile);
            //文件长度
            int len = in.available();

            //定义数组
            byte [] bytes = new byte[len];

            //读取到数组里面
            in.read(bytes);
            return bytes;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally{
            try {
                if(in != null){
                    in.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 创建日期:2018年1月14日<br/>
     * 创建时间:下午6:54:02<br/>
     * 创建用户:yellowcong<br/>
     * 机能概要:将字符串转化为文件
     * @param strBase64 base64 编码的文件
     * @param outFile 输出的目标文件地址
     * @return 
     * @throws IOException
     */
    public boolean base64ToFile(String strBase64,File outFile){
        try {
            // 解码,然后将字节转换为文件
            byte[] bytes = Base64.getDecoder().decode(strBase64); // 将字符串转换为byte数组
            return copyByte2File(bytes,outFile);
        } catch (Exception ioe) {
            ioe.printStackTrace();
            return false;
        }
    }
    /**
     * 创建日期:2018年1月14日<br/>
     * 创建时间:下午7:01:59<br/>
     * 创建用户:yellowcong<br/>
     * 机能概要:将字节码转化为文件
     * @param bytes
     * @param file
     */
    private boolean copyByte2File(byte [] bytes,File file){
        FileOutputStream  out = null;
        try {
            //转化为输入流
            ByteArrayInputStream in = new ByteArrayInputStream(bytes);

            //写出文件
            byte[] buffer = new byte[1024];

            out = new FileOutputStream(file);

            //写文件
            int len = 0;
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len); // 文件写操作
            }
            return true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                if(out != null){
                    out.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return false;
    }
}
测试代码
package yellowcong.yuyin;

import java.io.File;

import org.junit.Test;

import com.yellowcong.baidu.utils.Base64Utils;

/**
 * 创建日期:2018年1月14日<br/>
 * 创建时间:下午7:10:28<br/>
 * 创建者    :yellowcong<br/>
 * 机能概要:
 */
public class TestBase64Utils {

    @Test
    public void testJpg2Base64(){
        Base64Utils utils = Base64Utils.getInstance();
        String str = utils.file2Base64(new File("D:/demo_identificate.png"));
        System.out.println(str);

    }
    @Test
    public void testBase642File(){

        Base64Utils utils = Base64Utils.getInstance();
        String str = utils.file2Base64(new File("D:/demo_identificate.png"));

        utils.base64ToFile(str, new File("D://xx.jpg"));

    }
}

版权声明:本文为CSDN博主「狂飙的yellowcong」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yelllowcong/article/details/79058482

上一篇 下一篇

猜你喜欢

热点阅读