1、文件上传入参格式
MultipartFile:是一个接口,当你需要处理文件上传时,你可以将请求中的文件作为 MultipartFile 类型的参数来接收
File:是一个类,表示文件或路径的抽象路径名
Byte[]:是一个二进制数组,用于存储实际文件的原始数据
InputStream:是一个抽象类,用于从数据源读取原始字节数据
2、文件下载出参格式
一般出参类型为void,直接set到了HttpServletResponse
注意
MockMultipartFile是测试用的,生产环境一般不用
3、类型转换
转MultipartFile
//File转MultipartFile
public MultipartFile fileToMultipartFile(File file, String contentType) {
if (Objects.isNull(file)) {
return null;
}
//需要新建一个实现类SimpleMultipartFiless
//contentType 文件类型 比如"application/pdf"
return new SimpleMultipartFiless(file.getName(), file, file.getName(), contentType);
}
//byte[]转MultipartFile
@SneakyThrows
public MultipartFile byteToMultipartFile(byte[] bytes, String fileName, String contentType, String fileRealName) {
if (null == bytes || bytes.length == 0) {
return null;
}
FileItemFactory factory = new DiskFileItemFactory();
//第一个参数fileName一般是一串唯一字符串的文件名
//第二个参数contentType 文件类型 比如"application/pdf"
//第三个参数true:如果文件不大,尝试在内存中存储它,而不是在磁盘上
//第四个参数fileName一般是汉字文件名
FileItem item = factory.createItem(fileName, contentType, true, fileRealName);
InputStream stream = new ByteArrayInputStream(bytes);
try {
Streams.copy(stream, item.getOutputStream(), true);
} catch (Exception e) {
}
return new CommonsMultipartFile(item);
}
转File
//MultipartFile转File
@SneakyThrows
public File multipartFileToFile(MultipartFile multipartFile) {
//MultipartFile转File
//需要在当前服务器创建一个临时路径,建一个空file,使用后再删掉。删掉时机自行判断
//因为转换逻辑是把MultipartFile的流塞进临时的空file
File dir = new File("/Users/duke/file/multipartFileToFile");
if (!dir.exists()) {
dir.mkdirs();
}
String path = dir + "/" + multipartFile.getOriginalFilename();
File file = new File(path);
FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), file);
//JVM退出时删除
file.deleteOnExit();
return file;
}
//byte[]转File
@SneakyThrows
public File byteToFile(byte[] bytes, String fileName) {
File dir = new File("/Users/duke/file/byteToFile");
if (!dir.exists()) {
dir.mkdirs();
}
File file2 = new File(dir, fileName);
//如果使用这句,普通语句,需要手动关流
//BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file2));
//bos.write(bytes);
//0、以下使用try-with-resources
//1、当 try-with-resources 语句结束时(无论是正常结束还是由于异常而结束),它都会自动调用所有在括号中声明的资源的 close 方法
//2、在 try-with-resources 语句中,你可以声明一个或多个资源,它们都必须实现了 AutoCloseable 接口
//3、BufferedOutputStream 和 FileOutputStream 都实现了这个接口
//4、由于 BufferedOutputStream 已经包装了 FileOutputStream,并且 BufferedOutputStream 的 close 方法会调用其内部 FileOutputStream 的 close 方法,所以你只需要在 try-with-resources 语句中声明 BufferedOutputStream 即可
//声明变量 BufferedOutputStream bos
//声明变量并实例化 BufferedOutputStream bos = new(xxx);
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file2))) {
bos.write(bytes);
}
return file2;
}
//InputStream转File(不需要生成本地文件)
@SneakyThrows
public File inputStreamToFile(InputStream inputStream, String fileName, String suffix) {
//suffix 文件后缀,比如".pdf"
File tempFile = File.createTempFile(fileName, suffix);
try (FileOutputStream out = new FileOutputStream(tempFile)) {
IOUtils.copy(inputStream, out);
}
return tempFile;
}
转byte[]
//File转byte[]
@SneakyThrows
public byte[] fileToByte(File file) {
//URI 统一资源标识符,即可以表示任何类型的资源
URI uri = file.toURI();
//Path 专门用于文件系统路径
Path path = Paths.get(uri);
//Files.readAllBytes(Path path)这个方法读取由path对象指定的文件的全部内容
return Files.readAllBytes(Paths.get(file.toURI()));
}
//MultipartFile转byte[]
@SneakyThrows
public byte[] multipartFileToByte(MultipartFile multipartFile) {
//getBytes()内部实现了从上传的文件中提取字节数据的逻辑。
//调用这个方法时,Spring框架会根据上传的文件内容,将其读取为字节数组(byte array)并返回
//在处理大文件时可能会消耗大量内存,因为它会一次性将整个文件内容加载到内存中。如果上传的文件非常大,可能会导致内存溢出(OutOfMemoryError)错误
return multipartFile.getBytes();
}
//InputStream转byte[]
@SneakyThrows
public byte[] inputStreamToByte(InputStream inputStream) {
return IOUtils.toByteArray(inputStream);
}
转流
//File转流
@SneakyThrows
public InputStream fileToInputStream(File file) {
return new FileInputStream(file);
}
//MultipartFile转流
@SneakyThrows
public InputStream multipartFileToInputStream(MultipartFile file) {
return file.getInputStream();
}
//byte[]转流
@SneakyThrows
public InputStream bytesToInputStream(byte[] bytes) {
return new ByteArrayInputStream(bytes);
}
使用的类
import cn.hutool.core.util.ObjectUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.util.Streams;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import java.io.*;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
转MultipartFile时使用到的 自定义的实体类
package com..xx.adapter.jwtAuthAop.listen;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class SimpleMultipartFiless implements MultipartFile {
private final String name;
private final String originalFilename;
private final String contentType;
private final File file;
public SimpleMultipartFiless(String name, File file, String originalFilename, String contentType) {
this.name = name;
this.file = 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.length()==0;
}
@Override
public long getSize() {
return file.length();
}
@Override
public byte[] getBytes() throws IOException {
return Files.readAllBytes(Paths.get(file.toURI()));
}
@Override
public InputStream getInputStream() throws IOException {
return new FileInputStream(file);
}
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
if (dest.exists() && !dest.delete()) {
throw new IOException("Failed to delete existing file: " + dest.getAbsolutePath());
}
if (!file.renameTo(dest)) {
try (InputStream in = new FileInputStream(file)) {
// 使用Files.copy进行文件复制
Files.copy(in, dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
// 复制完成,现在可以尝试删除原文件
if (!file.delete()) {
throw new IOException("Failed to delete original file: " + file.getAbsolutePath());
}
}
}
}
}