2
liusuyi
2026-05-12 9b5c9db9189493fbc6db48f55a7b7311b2a3253c
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
package com.ard.work.utils.file;
 
import java.io.*;
import java.util.Base64;
 
import org.springframework.web.multipart.MultipartFile;
 
public class MultipartFileUtil {
 
    /**
     * 通过InputStream获取MultipartFile
     */
    public static MultipartFile inputStreamToMultipartFile(InputStream inputStream, String fileName, String contentType) {
        try {
            byte[] content = inputStream.readAllBytes();  // Java 9+ 可用
            return new ByteArrayMultipartFile(content, "file", fileName, contentType);
        } catch (IOException e) {
            throw new RuntimeException("输入流转换失败: " + e.getMessage(), e);
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                // 忽略关闭异常
            }
        }
    }
 
    /**
     * 通过文件路径获取MultipartFile
     */
    public static MultipartFile filePathToMultipartFile(String filePath, String fileName, String contentType) {
        File file = new File(filePath);
 
        if (!fileName.contains(".") && filePath.contains(".")) {
            String extension = filePath.substring(filePath.lastIndexOf('.'));
            fileName = fileName + extension;
        }
 
        try {
            byte[] content = java.nio.file.Files.readAllBytes(file.toPath());
            return new ByteArrayMultipartFile(content, "file", fileName, contentType);
        } catch (IOException e) {
            throw new RuntimeException("文件转换失败: " + e.getMessage(), e);
        }
    }
 
    /**
     * Base64字符串转MultipartFile
     */
    public static MultipartFile base64ToMultipartFile(String base64Str, String fileName, String contentType) {
        if (base64Str == null || base64Str.isEmpty()) {
            throw new IllegalArgumentException("Base64字符串不能为空");
        }
 
        String pureBase64Str = base64Str.contains(",") ? base64Str.split(",")[1] : base64Str;
 
        try {
            byte[] fileBytes = Base64.getDecoder().decode(pureBase64Str);
            return new ByteArrayMultipartFile(fileBytes, "file", fileName, contentType);
        } catch (IllegalArgumentException e) {
            throw new RuntimeException("Base64字符串格式非法,解码失败", e);
        }
    }
 
    /**
     * 自定义 MultipartFile 实现(不依赖 MockMultipartFile)
     */
    private static class ByteArrayMultipartFile implements MultipartFile {
        private final byte[] content;
        private final String name;
        private final String originalFilename;
        private final String contentType;
 
        public ByteArrayMultipartFile(byte[] content, String name, String originalFilename, String contentType) {
            this.content = content;
            this.name = name;
            this.originalFilename = originalFilename;
            this.contentType = contentType;
        }
 
        @Override
        public String getName() {
            return name;
        }
 
        @Override
        public String getOriginalFilename() {
            return originalFilename;
        }
 
        @Override
        public String getContentType() {
            return contentType;
        }
 
        @Override
        public boolean isEmpty() {
            return content == null || content.length == 0;
        }
 
        @Override
        public long getSize() {
            return content.length;
        }
 
        @Override
        public byte[] getBytes() throws IOException {
            return content;
        }
 
        @Override
        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(content);
        }
 
        @Override
        public void transferTo(File dest) throws IOException, IllegalStateException {
            try (FileOutputStream fos = new FileOutputStream(dest)) {
                fos.write(content);
            }
        }
    }
}