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());
|
}
|
}
|