测试用户
2023-04-13 43393f2bb11cbf9e6af40077bbc5284660e8a754
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
package com.common.core.utils;
 
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
 
import java.io.*;
 
/**
 * @Author holfeng
 * @Date 15:21 10/02/2022
 * @Version 1.0
 **/
public class BASE64DecodedMultipartFile implements MultipartFile {
 
    private byte[] fileContent;
    private String header;
 
    public BASE64DecodedMultipartFile(byte[] fileContent, String header) {
        this.fileContent = fileContent;
        this.header = header.split(";")[0];
    }
 
    @Override
    public String getName() {
        return header.split("/")[1];
    }
 
    @Override
    public String getOriginalFilename() {
        return header.split("/")[1];
    }
 
    @Override
    public String getContentType() {
        return header.split(":")[1];
    }
 
    @Override
    public boolean isEmpty() {
        return fileContent == null || fileContent.length == 0;
    }
 
    @Override
    public long getSize() {
        return fileContent.length;
    }
 
    @Override
    public byte[] getBytes() throws IOException {
        return fileContent;
    }
 
    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(fileContent);
    }
 
    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        FileOutputStream stream=null;
        try {
            stream = new FileOutputStream(dest);
            stream.write(fileContent);
        }catch (Exception e){
            throw e;
        }finally {
            if(stream!=null)stream.close();
        }
    }
 
 
    public static MultipartFile base64ToMultipart(String base64) throws IOException {
        String[] baseStrs = base64.split(",");
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] b = new byte[0];
        b = decoder.decodeBuffer(baseStrs[1]);
        for (int i = 0; i < b.length; ++i) {
            if (b[i] < 0) {
                b[i] += 256;
            }
        }
        return new BASE64DecodedMultipartFile(b, baseStrs[0]);
    }
 
 
}