java Base64Utils 工具栏类分享

程序员零花钱码上赚 

 

 以下是java Base64Util工具类,期望能帮助到有需要的程序员

public class Base64Utils {
    // 加密
    @SuppressWarnings("restriction")
    public static String getBase64(String str) {
        byte[] b = null;
        String s = null;
        try {
            b = str.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (b != null) {
            s = new BASE64Encoder().encode(b);
        }
        return s;
    }

    // 解密
    @SuppressWarnings("restriction")
    public static String getFromBase64(String s) {
        byte[] b = null;
        String result = null;
        if (s != null) {
            BASE64Decoder decoder = new BASE64Decoder();
            try {
                b = decoder.decodeBuffer(s);
                result = new String(b, "utf-8");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static byte[] addBytes(byte[] data1, byte[] data2) {
        byte[] data3 = new byte[data1.length + data2.length];
        System.arraycopy(data1, 0, data3, 0, data1.length);
        System.arraycopy(data2, 0, data3, data1.length, data2.length);
        return data3;
    }
    /**
     * byte转InputStream
     * @param buf
     * @return
     */
    public static InputStream byte2Input(byte[] buf) {
        return new ByteArrayInputStream(buf);
    }

    /**
     * pdf合并
     * @param inputs
     * @param filepth
     */
    public static void hbpdf(List<InputStream> inputs, String filepth) {
        try {
            PDFMergerUtility PDFmerger = new PDFMergerUtility();
            PDFmerger.addSources(inputs);
            PDFmerger.setDestinationFileName(filepth);
            PDFmerger.mergeDocuments();
            System.out.println("Documents merged");
        } catch (Exception e) {

        }
    }

    /**
     * file转byte[]
     * @param filePath
     * @return
     */
    public static byte[] File2byte(String filePath)
    {
        byte[] buffer = null;
        try
        {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1)
            {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return buffer;
    }

    public static void main(String[] args) {
        //String sa = "U0MwMDAwMDnnrKwx6L2m";
        //System.out.println(getFromBase64(sa));
        //hbpdf();

        String str = "的健康角度考虑";
        System.out.println(getBase64(str));
        String str2 = "55qE5YGl5bq36KeS5bqm6ICD6JmR";
        System.out.println(getFromBase64(str2));
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值