java上传zip文件

文件上传大家都是知道怎么写的,前几天用到了上传zip文件的方法,在这里记录一下,希望可以帮助到大家!

  /****
     * 上传解析zip
     * @param file
     */
    @RequestMapping("uploadZip")
    public void uploadZip(MultipartFile file) {
        String zipFileName;
        String teachId;
        String fileName;
        try {
            ZipInputStream zipInputStream = new ZipInputStream(file.getInputStream(), Charset.forName("GBK"));
            BufferedInputStream bs = new BufferedInputStream(zipInputStream);
            ZipEntry zipEntry;
            byte[] bytes = null;
            while ((zipEntry = zipInputStream.getNextEntry()) != null) {
                zipFileName = zipEntry.getName();
                // 只读取docx的文件
                String docx = zipFileName.substring(zipFileName.length() - 4, zipFileName.length());
                if (zipEntry.getSize() > 0 && docx != null && "docx".equals(docx)) {
                    bytes = new byte[(int) zipEntry.getSize()];
                    bs.read(bytes, 0, (int) zipEntry.getSize()); 
                    MultipartFile multipartFile = new MockMultipartFile(fileName, new ByteArrayInputStream(bytes));
                    
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

很简单,没有很多代码,拿到MultipartFile 就可以做我们的业务逻辑了

pom文件引入

<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>5.0.7.RELEASE</version>
		</dependency>

好了,到这里,上传zip文件的代码就没有了,希望可以帮助到大家!如果有用或者错误的话,希望大家多多评论,关注。

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值