admin
2024-02-21 2272213fea7ab9ea6250eefad55bb113ce2b1837
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.ruoyi.utils;
 
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.uuid.Seq;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FilenameUtils;
 
import javax.xml.bind.DatatypeConverter;
import java.io.*;
import java.net.URL;
 
public class Base64Util {
 
    /**
     * @Description: 文件转为base64字符串。filePath:文件路径
     * @Param: [filePath]
     * @return: java.lang.String
     * @Date: 2020/12/25
     */
    public static String fileToBase64(String filePath) throws IOException {
        File file = new File(filePath);
        FileInputStream inputFile = null;
        byte[] buffer = null;
        try {
            inputFile = new FileInputStream(file);
            buffer = new byte[(int) file.length()];
            inputFile.read(buffer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != inputFile) {
                inputFile.close();
            }
        }
        byte[] bs = Base64.encodeBase64(buffer);
        return new String(bs);
    }
 
    /**
     * base64字符串转文件
     * @param base64Str
     * @param targetFilePath
     * @return
     */
    public static void base64ToFile(String base64Str, String targetFilePath) {
        byte[] buffer = Base64.decodeBase64(base64Str);
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(targetFilePath);
            out.write(buffer);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
 
    /**
     * 将图片转化为base64
     * @param src
     * @return
     * @throws Exception
     */
    public static String imgToBase64(String src) throws Exception {
        URL url = new URL(src);
        InputStream in = null;
        byte[] data = null;
        try {
            in = url.openStream();//远程文件
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        Base64 encoder = new Base64();
        return encoder.encodeToString(data);
    }
 
    /**
     * Base64转换为图片服务
     * targetPath  输出视频文件路径,不需要文件名
     * */
    public static void base64ToImg(String base64,String targetPath){
        File file = null;
        FileOutputStream fops = null;
        base64 = base64.replace("data:image/jpeg;base64,","");
        byte[] buff = DatatypeConverter.parseBase64Binary(base64);
        try {
            fops = new FileOutputStream(new File(targetPath));
            fops.write(buff);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("--------------------------------"+"图片转换完成"+"--------------------------------");
    }
 
    /**
     * Base64转换为图片存在服务器中
     * */
    public static String base64ToJpg(String base64,String name){
        //文件路径与存储路径
        String fileName = StringUtils.format("{}/{}_{}.jpg", DateUtils.datePath(), name, Seq.getId(Seq.uploadSeqType));
        String filePath = RuoYiConfig.getUploadPath() + "/" + fileName;
        int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
        String currentDir = StringUtils.substring(RuoYiConfig.getUploadPath(), dirLastIndex);
        String url = Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
 
        // 参数avatar接收base64字符串
        // 1.截取掉base64开头的data:image/png;base64,
        base64 = base64.replace("data:image/jpeg;base64,","");
        // 2.base64转换为bytes类型
        byte[] buff = DatatypeConverter.parseBase64Binary(base64);
        // 3.创建一张图片,将bytes类型的数据写入图片中
        FileOutputStream fops = null;
        try {
            File desc = new File(filePath);
            if (!desc.exists()) {
                if (!desc.getParentFile().exists()) {
                    desc.getParentFile().mkdirs();
                }
            }
            fops = new FileOutputStream(desc);
            fops.write(buff);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return url;
    }
 
 
 
}