哎哟,不错哦

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

java 将本地文件或网络文件与base64互相转换

一:将网络文件转为Base64

将文件转为base64

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static String fileToBase64(String url){
    int byteread = 0;
    String total = null;
    byte[] totalbyte = new byte[0];
    try {
        URL url = new URL(url);
        URLConnection conn = url.openConnection();
        InputStream inStream = conn.getInputStream();
        byte[] buffer = new byte[1204];
        while ((byteread = inStream.read(buffer)) != -1) {
            //拼接流,这样写是保证文件不会被篡改
            totalbyte = byteMerger(totalbyte,buffer,byteread);
        }
        inStream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Base64.encodeBase64String(totalbyte)
}

  将base转为文件

  

1
2
3
4
5
6
7
8
9
10
11
12
13
public static void base64ToFile(String base64, String filePath) {
        try {
            byte[] bytes = Base64.decodeBase64(base64);
            //base解密
            File videoFile = new File(filePath);
            //输入文件
            FileOutputStream fos = new FileOutputStream(videoFile);
            fos.write(bytes, 0, bytes.length);
            fos.flush();
            fos.close();
        } catch (IOException e) {
        }
    }

  

二:将本地文件转Base64

转Base64

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public static String videoToBase64(File videofilePath) {
        long size = videofilePath.length();
        byte[] imageByte = new byte[(int) size];
        FileInputStream fs = null;
        BufferedInputStream bis = null;
        try {
            fs = new FileInputStream(videofilePath);
            bis = new BufferedInputStream(fs);
            bis.read(imageByte);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fs != null) {
                try {
                    fs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Base64.encodeBase64String(imageByte);
    }

  

将Base64转文件

1
2
3
4
5
6
7
8
9
10
11
12
13
public static void base64ToFile(String base64, String filePath) {
        try {
            byte[] bytes = Base64.decodeBase64(base64);
            //base解密
            File videoFile = new File(filePath);
            //输入文件
            FileOutputStream fos = new FileOutputStream(videoFile);
            fos.write(bytes, 0, bytes.length);
            fos.flush();
            fos.close();
        } catch (IOException e) {
        }
    }

  

注意:

在将文件转Base64字符时,如果使用sun下的BASE64Encoder时会导致转换出来的Base64自动换行,原因是RFC2045中有规定Base64一行不能超过76字符,超过则添加回车换行符所以导致转换出来的Base64字符会出现换行,解决方法是使用Apache的 commons-codec.jar,Base64.encodeBase64String(byte[])得到的Base64字符不会出现换行

commons-codec   1.4版本时也会出现换行,使用1.8时不会出现换行,其他版本没有测试

 

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.8</version>
</dependency>

 

 

 

posted on   哎哟,不错哦  阅读(1983)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示