Java读取MultipartFile Zip文件并将每个文件转换为MultipartFile

	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

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
好的,以下是Java代码示例,使用Spring框架的MultipartFile类实现multipartfile上传zip文件结构跟原文件一模一样: ```java import org.springframework.web.multipart.MultipartFile; import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class ZipFileUploadService { //上传zip文件并解压到指定目录 public void uploadAndExtractZip(MultipartFile zipFile, String extractDir) throws IOException { try (ZipInputStream zis = new ZipInputStream(zipFile.getInputStream())) { ZipEntry zipEntry; while ((zipEntry = zis.getNextEntry()) != null) { String filePath = extractDir + File.separator + zipEntry.getName(); if (!zipEntry.isDirectory()) { //创建文件File parent = new File(new File(filePath).getParent()); if (!parent.exists()) { parent.mkdirs(); } //写文件 try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) { byte[] buffer = new byte[1024]; int len; while ((len = zis.read(buffer)) != -1) { bos.write(buffer, 0, len); } bos.flush(); } } else { //创建文件File folder = new File(filePath); if (!folder.exists()) { folder.mkdirs(); } } zis.closeEntry(); } } } } ``` 解释一下这段代码的实现过程: 1. 使用Spring框架的MultipartFile类获取上传的zip文件的输入流。 2. 创建ZipInputStream对象,通过getNextEntry()方法获取zip文件中的每个文件。 3. 判断当前文件是否为目录,若是则创建目录。 4. 若不是目录,则创建文件夹并将文件写入到指定路径。 5. 关闭ZipInputStream流。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会写爬虫的程序员B

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值