public static Map<String,MultipartFile> excelFileUpload(MultipartFile zipFile,String filePath) throws IOException {
Map<String, MultipartFile> multipartFileHashMap = new HashMap<>();
if (zipFile!=null) {
//解压压缩文件并上传文件
byte[] buffer = new byte[1024];
int bufferSize = 1024;
File tempFile = null;
ByteArrayOutputStream byteOut = null;
ZipInputStream zipIn = null;
try {
java.util.zip.ZipEntry zipEntry = null;
zipIn = new ZipInputStream(zipFile.getInputStream(), Charset.forName("GBK"));
byte[] bytes = new byte[1024];
while ((zipEntry = zipIn.getNextEntry()) != null) {
byteOut = new ByteArrayOutputStream();
IOUtils.copy(zipIn, byteOut);
InputStream inputStream = new ByteArrayInputStream(byteOut.toByteArray());
String fileName = zipEntry.getName();
MultipartFile multipartFile = FileUtils.getMultipartFile(inputStream, fileName);
multipartFileHashMap.put(fileName, multipartFile );
}
} catch (Exception e){
e.printStackTrace();
} finally{
if (byteOut != null) {
byteOut.close();
}
if (zipIn != null) {
zipIn.close();
}
}
}
return multipartFileHashMap;
}
public class FileUtils {
/**
* 获取封装得MultipartFile
*
* @param inputStream inputStream
* @return MultipartFile
*/
public static MultipartFile getMultipartFile(InputStream inputStream, String fileName) {
FileItem fileItem = createFileItem(inputStream, fileName);
//CommonsMultipartFile是feign对multipartFile的封装,但是要FileItem类对象
return new CommonsMultipartFile(fileItem);
}
/**
* FileItem类对象创建
*
* @param inputStream inputStream
* @param fileName fileName
* @return FileItem
*/
public static FileItem createFileItem(InputStream inputStream, String fileName) {
FileItemFactory factory = new DiskFileItemFactory(16, null);
String textFieldName = "file";
FileItem item = factory.createItem(textFieldName, MediaType.MULTIPART_FORM_DATA_VALUE, true, fileName);
int bytesRead = 0;
byte[] buffer = new byte[10 * 1024 * 1024];
OutputStream os = null;
//使用输出流输出输入流的字节
try {
os = item.getOutputStream();
while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
inputStream.close();
} catch (IOException e) {
throw new IllegalArgumentException("文件上传失败");
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
}
}
}
return item;
}
}
请结合另一篇中的博客中的方法使用:https://blog.csdn.net/qq_36360308/article/details/125894500?spm=1001.2014.3001.5501ss