url转base64,base64转file

1.url转base64

/**
 * url转base64
 * @param url 只支持本地文件地址
 * @return
 */
public static String fileToBase64(String url){
    File file = new File(url);
    byte[] bytes=null;
    String base64String=new String();
    FileInputStream fileInputStream= null;
    try {
        fileInputStream = new FileInputStream(file);
        int size=fileInputStream.available();
        bytes=new byte[size];
        fileInputStream.read(bytes);
        fileInputStream.close();

        BASE64Encoder encoder=new BASE64Encoder();
        base64String=encoder.encode(bytes);
    } catch (FileNotFoundException e) {
        throw new RuntimeException("pdf文件找不到!");
    } catch (IOException e) {
        throw new RuntimeException("pdf文件转base64异常!");
    }
    return base64String;
}

2.网络地址转为base64

/**
 * 将网络地址转为base64
 * @param url 只支持网络地址
 * @return
 */
public static String encodeImageToBase64(URL url) throws Exception {
    //将图片文件转化为字节数组字符串,并对其进行Base64编码处理
    System.out.println("文件的路径为:" + url.toString());
    //打开链接
    HttpURLConnection conn = null;
    try {
        conn = (HttpURLConnection) url.openConnection();
        //设置请求方式为"GET"
        conn.setRequestMethod("GET");
        //超时响应时间为5秒
        conn.setConnectTimeout(5 * 1000);
        //通过输入流获取文件数据
        InputStream inStream = conn.getInputStream();
        //得到文件的二进制数据,以二进制封装得到数据,具有通用性
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        //创建一个Buffer字符串
        byte[] buffer = new byte[1024];
        //每次读取的字符串长度,如果为-1,代表全部读取完毕
        int len = 0;
        //使用一个输入流从buffer里把数据读取出来
        while ((len = inStream.read(buffer)) != -1) {
            //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
            outStream.write(buffer, 0, len);
        }
        //关闭输入流
        inStream.close();
        byte[] data = outStream.toByteArray();
        //对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        String base64 = encoder.encode(data);
        System.out.println("文件[{}]编码成base64字符串:[{}]"+url.toString()+base64);
        return base64;//返回Base64编码过的字节数组字符串
    } catch (IOException e) {
        e.printStackTrace();
        throw new Exception("文件上传失败,请联系客服!");
    }
}

3. base64解码转file

注意:byte[] bytes = Base64.getDecoder().decode(base64);

byte[] bytes = Base64.getMimeDecoder().decode(base64);

//BASE64解码成File文件
public static void base64ToFile(String destPath,String base64, String fileName) {
    File file = new File(destPath);

    BufferedOutputStream bos = null;
    java.io.FileOutputStream fos = null;
    try {
        //创建文件目录
        if (!file.exists() && !file.isDirectory()) {
            file.createNewFile();
        }
        byte[] bytes = Base64.getMimeDecoder().decode(base64);
        fos = new java.io.FileOutputStream(file);
        bos = new BufferedOutputStream(fos);
        bos.write(bytes);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

4.base64解码失败

base64编码格式的编码方法和解码方法,实现是按照rfc4648和rfc2045协议来定义。

由实践得出:

jdk7的编码结果包含换行;
jdk8的编码结果不包含换行;
jdk8无法解码包含换行的编码结果;
jdk8的编码结果使用jdk7进行解码,没有任何问题,不再演示。

解决办法:
1. 使用apache common包中的org.apache.commons.codec.binary.Base64类进行编码和解码; 
2. 编码之后或解码之前去除换行符; 
3. 编码和解码使用相同的jdk版本;

4.采用以上几种编码,解码格式

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值