spring boot上传文件并自动解压

spring boot上传文件并自动解压

Scroll Down

业务需要把上传文件解压到指定目录

依赖

zip4j功能比较强大,支持加密、解密压缩,支持文件的添加、删除等

        <dependency>
            <groupId>net.lingala.zip4j</groupId>
            <artifactId>zip4j</artifactId>
            <version>1.3.2</version>
        </dependency>

控制器

判断文件后缀是不是.zip 进行服务调用

@PostMapping("/uploadWallpaper")
    @ResponseBody
    public Response uploadFile(@RequestParam("file") MultipartFile file,Wallpaper wallpaper){
        //后缀.zip
        if(FileUtils.getPrefix(file.getOriginalFilename()).toLowerCase().equals(Wallpaper.File_PREFIX)){
            return wallpaperService.saveWallpaper(file,wallpaper);
        }
        return Response.Err("不是.zip结尾的压缩包");
    }

服务

主要业务实现,把元数据存入数据库.保存文件(zip)后进行解压.

public Response saveWallpaper(MultipartFile file, Wallpaper wallpaper){
        //设置id(行数+1)
        wallpaper.setId(WallpaperSize()+1);
        wallpaper.setUrl("127.0.0.1:8081/wallpaper/"+wallpaper.getId()+"/index.html");
        wallpaper.setAudit(false);
        //添加到数据库
        wallpaperMapper.insert(wallpaper);
        String path=FileUtils.getApplicationPath()+"wallpaper"+File.separator+wallpaper.getId()+"."+Wallpaper.File_PREFIX;
        //保存到服务器
        FileUtils.saveFile(file,path);
        String destPath=FileUtils.getApplicationPath()+"wallpaper"+File.separator+wallpaper.getId()+File.separator;
        //解压
            if(FileUtils.unPackZip(new File(path),"",destPath)){
            return Response.Ok("上传成功",wallpaper.getUrl());
        }
        return Response.Err("上传失败");
    }

文件工具类

文件保存,文件解压的工具类

public class FileUtils {
    public static String getPrefix(String filename){
        return filename.substring(filename.lastIndexOf(".")+1);
    }
    public static boolean saveFile(MultipartFile file,String path){
        File desFile = new File(path);
        if(!desFile.getParentFile().exists()){
            desFile.mkdirs();
        }
        try {
            file.transferTo(desFile);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    public static String getApplicationPath(){
        //获取classpath
        ApplicationHome h = new ApplicationHome(WallpaperApplication.class);
        File jarF = h.getSource();
        return jarF.getParentFile()+ File.separator;
    }
    /**
     * zip文件解压
     *
     * @param destPath 解压文件路径
     * @param zipFile  压缩文件
     * @param password 解压密码(如果有)
     */
    public static boolean unPackZip(File zipFile, String password, String destPath) {
        try {
            ZipFile zip = new ZipFile(zipFile);
            zip.extractAll(destPath);
            // 如果解压需要密码
            if (zip.isEncrypted()) {
                zip.setPassword(password);
            }
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
        return true;
    }
}