java实现上传文件后解压缩,工具类

只上传工具类代码了,具体的业务,根据自己实际需求调整

上传工具类

package com.yxy.youxiang.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

public class FtpUtils {
	//上传文件,路径
    public static String uploadApkFileToLocal(MultipartFile multipartFile,String basePath) {
            if (multipartFile==null){
                throw new YXException("文件不能为空");
            }
            try {
            //创建目标文件路径 System.getProperty("user.dir")为获取当前项目的路径,File.separator为windows中的‘\’,linux中的‘/’
            //如果文件目录不存在,就执行创建
            File targetFile = new File(basePath);
            if (!targetFile.isDirectory()) {
            targetFile.mkdirs();
            }
            //拼接目标文件
            String fileName = multipartFile.getOriginalFilename();
            File finalFile = new File(basePath +fileName);
            String finalFileUrl = basePath +fileName;
            //获取本地文件输入流
            InputStream stream = multipartFile.getInputStream();
            //声明服务器要存储的目标文件输出流
            FileOutputStream fos = new FileOutputStream(finalFile);
            //写入目标文件
            byte[] buffer = new byte[1024 * 1024];
            int byteRead = 0;
            while ((byteRead = stream.read(buffer)) != -1) {
            fos.write(buffer, 0, byteRead);
            //把缓冲区的内容强制的写出,避免关闭流时丢失数据
            fos.flush();
            }
            //关闭流
            fos.close();
            stream.close();
            return finalFileUrl;
            } catch (Exception e) {
            throw new YXException("上传文件失败");
            }
            }
}

压缩文具:(根据文件路径,和指定解压到目的路径)

package com.yxy.youxiang.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * 文件压缩工具类
 */
public class ZipUtil {

    /**
     * 解压文件到指定目录
     */
    public static void unZipFiles(String zipPath, String descDir) throws IOException {
        try{
            File zipFile = new File(zipPath);
            System.err.println(zipFile.getName());
            if(!zipFile.exists()){
                throw new IOException("需解压文件不存在.");
            }
            File pathFile = new File(descDir);
            if (!pathFile.exists()) {
                pathFile.mkdirs();
            }
            ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));
            for (Enumeration entries = zip.entries(); entries.hasMoreElements();) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                String zipEntryName = entry.getName();
                System.err.println(zipEntryName);
                InputStream in = zip.getInputStream(entry);
                String outPath = (descDir + File.separator + zipEntryName).replaceAll("\\*", "/");
                System.err.println(outPath);
                // 判断路径是否存在,不存在则创建文件路径
                File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
                if (!file.exists()) {
                    file.mkdirs();
                }
                // 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
                if (new File(outPath).isDirectory()) {
                    continue;
                }
                // 输出文件路径信息
                OutputStream out = new FileOutputStream(outPath);
                byte[] buf1 = new byte[1024];
                int len;
                while ((len = in.read(buf1)) > 0) {
                    out.write(buf1, 0, len);
                }
                in.close();
                out.close();
            }
        }catch(Exception e){
            throw new IOException(e);
        }
    }

    // 删除文件或文件夹以及文件夹下所有文件
    public static void delDir(String dirPath) throws IOException {
        long start = System.currentTimeMillis();
        try{
            File dirFile = new File(dirPath);
            if (!dirFile.exists()) {
                return;
            }
            if (dirFile.isFile()) {
                dirFile.delete();
                return;
            }
            File[] files = dirFile.listFiles();
            if(files==null){
                return;
            }
            for (int i = 0; i < files.length; i++) {
                delDir(files[i].toString());
            }
            dirFile.delete();
        }catch(Exception e){
            throw new IOException("删除文件异常.");
        }
    }
}

开发中遇到的需求,记下来,顺便分享给大家,得到帮助了,帮忙给点点阅读量~

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值