日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java上传rar文件_java实现上传zip/rar压缩文件,自动解压

發布時間:2023/12/20 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java上传rar文件_java实现上传zip/rar压缩文件,自动解压 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在pom中添加解壓jar依賴

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.2.RELEASE

com.hf

uncompress

0.0.1-SNAPSHOT

uncompress

上傳壓縮文件(rar或者zip格式),解壓

1.8

org.springframework.boot

spring-boot-starter-web

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-starter-thymeleaf

net.lingala.zip4j

zip4j

1.3.2

com.github.junrar

junrar

0.7

org.springframework.boot

spring-boot-maven-plugin

解壓zip/rar的工具類

package com.hf.uncompress.utils;

import com.github.junrar.Archive;

import com.github.junrar.rarfile.FileHeader;

import lombok.extern.slf4j.Slf4j;

import net.lingala.zip4j.core.ZipFile;

import java.io.File;

import java.io.FileOutputStream;

/**

* @Description: 解壓rar/zip工具類

* @Date: 2019/1/22

* @Auther:

*/

@Slf4j

public class UnPackeUtil {

/**

* zip文件解壓

*

* @param destPath 解壓文件路徑

* @param zipFile 壓縮文件

* @param password 解壓密碼(如果有)

*/

public static void unPackZip(File zipFile, String password, String destPath) {

try {

ZipFile zip = new ZipFile(zipFile);

/*zip4j默認用GBK編碼去解壓,這里設置編碼為GBK的*/

zip.setFileNameCharset("GBK");

log.info("begin unpack zip file....");

zip.extractAll(destPath);

// 如果解壓需要密碼

if (zip.isEncrypted()) {

zip.setPassword(password);

}

} catch (Exception e) {

log.error("unPack zip file to " + destPath + " fail ....", e.getMessage(), e);

}

}

/**

* rar文件解壓(不支持有密碼的壓縮包)

*

* @param rarFile rar壓縮包

* @param destPath 解壓保存路徑

*/

public static void unPackRar(File rarFile, String destPath) {

try (Archive archive = new Archive(rarFile)) {

if (null != archive) {

FileHeader fileHeader = archive.nextFileHeader();

File file = null;

while (null != fileHeader) {

// 防止文件名中文亂碼問題的處理

String fileName = fileHeader.getFileNameW().isEmpty() ? fileHeader.getFileNameString() : fileHeader.getFileNameW();

if (fileHeader.isDirectory()) {

//是文件夾

file = new File(destPath + File.separator + fileName);

file.mkdirs();

} else {

//不是文件夾

file = new File(destPath + File.separator + fileName.trim());

if (!file.exists()) {

if (!file.getParentFile().exists()) {

// 相對路徑可能多級,可能需要創建父目錄.

file.getParentFile().mkdirs();

}

file.createNewFile();

}

FileOutputStream os = new FileOutputStream(file);

archive.extractFile(fileHeader, os);

os.close();

}

fileHeader = archive.nextFileHeader();

}

}

} catch (Exception e) {

log.error("unpack rar file fail....", e.getMessage(), e);

}

}

}

頁面HTML

Title

上傳壓縮包:

解壓路徑:

解壓密碼(為空可不傳):

controller代碼:

package com.hf.uncompress.controller;

import com.hf.uncompress.Result.AjaxList;

import com.hf.uncompress.service.FileUploadService;

import com.hf.uncompress.vo.PackParam;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

/**

* @Description:

* @Date: 2019/1/22

* @Auther:

*/

@Controller

@RequestMapping("/user")

@Slf4j

public class FileUploadController {

@Autowired

private FileUploadService fileUploadService;

@GetMapping("/redirect")

public String redirectHtml() {

return "work";

}

@PostMapping("/upload/zip")

@ResponseBody

public String uploadZip(MultipartFile zipFile, @RequestBody PackParam packParam) {

AjaxListajaxList = fileUploadService.handlerUpload(zipFile, packParam);

return ajaxList.getData();

}

}

service實現類代碼

package com.hf.uncompress.service.impl;

import com.hf.uncompress.Result.AjaxList;

import com.hf.uncompress.enums.FileTypeEnum;

import com.hf.uncompress.service.FileUploadService;

import com.hf.uncompress.utils.UnPackeUtil;

import com.hf.uncompress.vo.PackParam;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;

import java.io.IOException;

/**

* @Description:

* @Date: 2019/1/22

* @Auther:

*/

@Service

@Slf4j

public class FileUploadServiceImpl implements FileUploadService {

@Override

public AjaxListhandlerUpload(MultipartFile zipFile, PackParam packParam) {

if (null == zipFile) {

return AjaxList.createFail("請上傳壓縮文件!");

}

boolean isZipPack = true;

String fileContentType = zipFile.getContentType();

//將壓縮包保存在指定路徑

String packFilePath = packParam.getDestPath() + File.separator + zipFile.getName();

if (FileTypeEnum.FILE_TYPE_ZIP.type.equals(fileContentType)) {

//zip解壓縮處理

packFilePath += FileTypeEnum.FILE_TYPE_ZIP.fileStufix;

} else if (FileTypeEnum.FILE_TYPE_RAR.type.equals(fileContentType)) {

//rar解壓縮處理

packFilePath += FileTypeEnum.FILE_TYPE_RAR.fileStufix;

isZipPack = false;

} else {

return AjaxList.createFail("上傳的壓縮包格式不正確,僅支持rar和zip壓縮文件!");

}

File file = new File(packFilePath);

try {

zipFile.transferTo(file);

} catch (IOException e) {

log.error("zip file save to " + packParam.getDestPath() + " error", e.getMessage(), e);

return AjaxList.createFail("保存壓縮文件到:" + packParam.getDestPath() + " 失敗!");

}

if (isZipPack) {

//zip壓縮包

UnPackeUtil.unPackZip(file, packParam.getPassword(), packParam.getDestPath());

} else {

//rar壓縮包

UnPackeUtil.unPackRar(file, packParam.getDestPath());

}

return AjaxList.createSuccess("解壓成功");

}

}

使用到的枚舉類:

package com.hf.uncompress.enums;

import lombok.AllArgsConstructor;

import lombok.NoArgsConstructor;

/**

* @Description: 壓縮文件類型

* @Date: 2019/1/22

* @Auther:

*/

@AllArgsConstructor

@NoArgsConstructor

public enum FileTypeEnum {

FILE_TYPE_ZIP("application/zip", ".zip"),

FILE_TYPE_RAR("application/octet-stream", ".rar");

public String type;

public String fileStufix;

public static String getFileStufix(String type) {

for (FileTypeEnum orderTypeEnum : FileTypeEnum.values()) {

if (orderTypeEnum.type.equals(type)) {

return orderTypeEnum.fileStufix;

}

}

return null;

}

}

同一返回值定義:

package com.hf.uncompress.Result;

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

/**

* @Description: 返回值處理

* @Date: 2019/1/22

* @Auther:

*/

@AllArgsConstructor

@NoArgsConstructor

@Data

public class AjaxList{

private boolean isSuccess;

private T data;

public static AjaxListcreateSuccess(T data) {

return new AjaxList(true, data);

}

public static AjaxListcreateFail(T data) {

return new AjaxList(false, data);

}

}

前端上傳封裝的vo

package com.hf.uncompress.vo;

import lombok.Data;

/**

* @Description: 上傳壓縮的參數

* @Date: 2019/1/23

* @Auther:

*/

@Data

public class PackParam {

/**

* 解壓密碼

*/

private String password;

/**

* 解壓文件存儲地址

*/

private String destPath;

}

在application.properties中定義其上傳的閥域

#設置上傳單個文件的大小限制

spring.servlet.multipart.max-file-size=500MB

# 上傳文件總的最大值

spring.servlet.multipart.max-request-size=500MB

spring.thymeleaf.cache=false

總結

以上是生活随笔為你收集整理的java上传rar文件_java实现上传zip/rar压缩文件,自动解压的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。