如何在java

时间:2016-10-04 11:37:39

标签: java spring-mvc

我正在从屏幕上传zip文件夹,我正在使用MultipartFile将其发送到控制器。我正在尝试提取上传的文件夹并将该解压缩的文件夹保存在某个特定位置..我试过但我没有得到....谁能建议我? 这是我的代码

 public  String test(
                @RequestParam("datafile") MultipartFile file
    { 

    String source =file.getOriginalFilename();

    //source variable will containthe value as "zip_Folder.zip";
            String destination = "D:\\destination";

            try {
                 ZipFile zipFile = new ZipFile(source);
                 zipFile.extractAll(destination);

            } catch (ZipException e) {
                e.printStackTrace();
            }
    }

2 个答案:

答案 0 :(得分:5)

必需的zip4jApache Commons-IO依赖项:

@PostMapping("/upload")
public String add(@RequestParam("file") MultipartFile file) throws IOException {

    /**
     * save file to temp
     */
    File zip = File.createTempFile(UUID.randomUUID().toString(), "temp");
    FileOutputStream o = new FileOutputStream(zip);
    IOUtils.copy(file.getInputStream(), o);
    o.close();

    /**
     * unizp file from temp by zip4j
     */
    String destination = "D:\\destination";
    try {
         ZipFile zipFile = new ZipFile(zip);
         zipFile.extractAll(destination);
    } catch (ZipException e) {
        e.printStackTrace();
    } finally {
        /**
         * delete temp file
         */
        zip.delete();
    }

    return "redirect:/";
}

除此之外,最好的方法是在属性文件中放置“D:\ destination”等常量并按@Value注入

@Value("${destination.dir}")
private String destination;

答案 1 :(得分:0)

尝试使用

java.util.zip.ZipEntry; java.util.zip.ZipInputStream;

为我工作

例如

https://www.mkyong.com/java/how-to-decompress-files-from-a-zip-file/

相关问题