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
package com.ard.common.core.utils.file;
 
import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
 
/**
 * 本地文件转 MultipartFile,用于 Feign 文件上传
 */
public class FileMultipartFile implements MultipartFile {
    private final File file;
    private final String name;
    private final String originalFilename;
    private final String contentType;
 
    public FileMultipartFile(File file, String originalFilename, String contentType) {
        this.file = file;
        this.name = "file";
        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 file == null || file.length() == 0;
    }
 
    @Override
    public long getSize() {
        return file == null ? 0 : file.length();
    }
 
    @Override
    public byte[] getBytes() throws IOException {
        return java.nio.file.Files.readAllBytes(file.toPath());
    }
 
    @Override
    public InputStream getInputStream() throws IOException {
        return new FileInputStream(file);
    }
 
    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        java.nio.file.Files.copy(file.toPath(), dest.toPath());
    }
}