当前位置 博文首页 > 一个搬砖的农民工的博客:如何上传文件并解压

    一个搬砖的农民工的博客:如何上传文件并解压

    作者:[db:作者] 时间:2021-07-29 18:43

    上传单个zip文件到后端并解压后保存

    1.Controller

    
    
    /**
     * 保存目录
     */
    @Value("${file.upload}")
    private String catalog;
    
    @RestController
    @RequestMapping("/upload")
    public List<File> upload(MultipartFile file) throws IOException {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
             // 在 uploadPath 文件夹中通过日期对上传的文件归类保存
         // 比如:/2021/03/11
            String format = sdf.format(new Date());
    //  new File("").getCanonicalPath()获取本地项目路径,加上保存目录路径
            String path = new File("").getCanonicalPath()+catalog;
    // 路径加上日期作为一个目录 
    File folder = new File(path + format);
    // 不是一个目录的时候
            if (!folder.isDirectory()) {
                //建立一个子目录
                folder.mkdirs();
            }
    //  文件保存路径和保存的文件名
            File filePath= new File(folder,file.getOriginalFilename());
            try {
                // 文件保存
                file.transferTo(filePath);
            } catch (IOException e) {
                log.error("压缩包保存失败", e);
            }
    
            //解压
            List<File> _list = new ArrayList<>() ;
            try {
                ZipFile _zipFile = new ZipFile(filePath , "GBK") ;
                for(Enumeration entries = _zipFile.getEntries(); entries.hasMoreElements() ; ){
                    ZipEntry entry = (ZipEntry)entries.nextElement() ;
                    File _file = new File(folder.getPath() + File.separator + entry.getName()) ;
                    if( entry.isDirectory() ){
                        _file.mkdirs() ;
                    }else{
                        File _parent = _file.getParentFile() ;
                        if( !_parent.exists() ){
                            _parent.mkdirs() ;
                        }
                        InputStream _in = _zipFile.getInputStream((ZipArchiveEntry) entry);
                        OutputStream _out = new FileOutputStream(_file) ;
                        int len = 0 ;
                        byte[] _byte = new byte[1024];
                        while( (len = _in.read(_byte)) > 0){
                            _out.write(_byte, 0, len);
                        }
                        _in.close();
                        _out.flush();
                        _out.close();
                        _list.add(_file) ;
                    }
                }
            } catch (IOException e) {
                log.error("解压失败", e);
            }
            // 删除压缩包
            filePath.delete();
            // 返回文件保存路径
            return _list;
        }
    }
    
    
    
    

    2.yml配置

    
    spring:
      servlet:
        multipart:  #设置上传文件大小
          max-file-size: 800MB
          max-request-size: 800MB
    file: 
      upload: \\enroll-data-deal\\enroll-data-deal-application\\src\\main\\resources\\upload\\
    
    
    
    
    

    其实保存文件就两步(file.getOriginalFilename()文件名,获取文件保存路径,file转移到transferTo路径path)这种方式是上传到本地文件夹

    File path = new File("D:\\Web\\"+ file.getOriginalFilename());

    System.out.println(path); // D:\Web\一分一段导入模板.xls

    file.transferTo(path);

    cs