|
|
@@ -36,8 +36,7 @@ import java.util.zip.ZipOutputStream;
|
|
|
*
|
|
|
* @author zhongzheng
|
|
|
*/
|
|
|
-public class FileUtils extends org.apache.commons.io.FileUtils
|
|
|
-{
|
|
|
+public class FileUtils extends org.apache.commons.io.FileUtils {
|
|
|
|
|
|
private static final byte[] buf = new byte[1024];
|
|
|
public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";
|
|
|
@@ -46,52 +45,36 @@ public class FileUtils extends org.apache.commons.io.FileUtils
|
|
|
* 输出指定文件的byte数组
|
|
|
*
|
|
|
* @param filePath 文件路径
|
|
|
- * @param os 输出流
|
|
|
+ * @param os 输出流
|
|
|
* @return
|
|
|
*/
|
|
|
- public static void writeBytes(String filePath, OutputStream os) throws IOException
|
|
|
- {
|
|
|
+ public static void writeBytes(String filePath, OutputStream os) throws IOException {
|
|
|
FileInputStream fis = null;
|
|
|
- try
|
|
|
- {
|
|
|
+ try {
|
|
|
File file = new File(filePath);
|
|
|
- if (!file.exists())
|
|
|
- {
|
|
|
+ if (!file.exists()) {
|
|
|
throw new FileNotFoundException(filePath);
|
|
|
}
|
|
|
fis = new FileInputStream(file);
|
|
|
byte[] b = new byte[1024];
|
|
|
int length;
|
|
|
- while ((length = fis.read(b)) > 0)
|
|
|
- {
|
|
|
+ while ((length = fis.read(b)) > 0) {
|
|
|
os.write(b, 0, length);
|
|
|
}
|
|
|
- }
|
|
|
- catch (IOException e)
|
|
|
- {
|
|
|
+ } catch (IOException e) {
|
|
|
throw e;
|
|
|
- }
|
|
|
- finally
|
|
|
- {
|
|
|
- if (os != null)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
+ } finally {
|
|
|
+ if (os != null) {
|
|
|
+ try {
|
|
|
os.close();
|
|
|
- }
|
|
|
- catch (IOException e1)
|
|
|
- {
|
|
|
+ } catch (IOException e1) {
|
|
|
e1.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
- if (fis != null)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
+ if (fis != null) {
|
|
|
+ try {
|
|
|
fis.close();
|
|
|
- }
|
|
|
- catch (IOException e1)
|
|
|
- {
|
|
|
+ } catch (IOException e1) {
|
|
|
e1.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
@@ -104,13 +87,11 @@ public class FileUtils extends org.apache.commons.io.FileUtils
|
|
|
* @param filePath 文件
|
|
|
* @return
|
|
|
*/
|
|
|
- public static boolean deleteFile(String filePath)
|
|
|
- {
|
|
|
+ public static boolean deleteFile(String filePath) {
|
|
|
boolean flag = false;
|
|
|
File file = new File(filePath);
|
|
|
// 路径为文件且不为空则进行删除
|
|
|
- if (file.isFile() && file.exists())
|
|
|
- {
|
|
|
+ if (file.isFile() && file.exists()) {
|
|
|
file.delete();
|
|
|
flag = true;
|
|
|
}
|
|
|
@@ -123,8 +104,7 @@ public class FileUtils extends org.apache.commons.io.FileUtils
|
|
|
* @param filename 文件名称
|
|
|
* @return true 正常 false 非法
|
|
|
*/
|
|
|
- public static boolean isValidFilename(String filename)
|
|
|
- {
|
|
|
+ public static boolean isValidFilename(String filename) {
|
|
|
return filename.matches(FILENAME_PATTERN);
|
|
|
}
|
|
|
|
|
|
@@ -134,17 +114,14 @@ public class FileUtils extends org.apache.commons.io.FileUtils
|
|
|
* @param resource 需要下载的文件
|
|
|
* @return true 正常 false 非法
|
|
|
*/
|
|
|
- public static boolean checkAllowDownload(String resource)
|
|
|
- {
|
|
|
+ public static boolean checkAllowDownload(String resource) {
|
|
|
// 禁止目录上跳级别
|
|
|
- if (StrUtil.contains(resource, ".."))
|
|
|
- {
|
|
|
+ if (StrUtil.contains(resource, "..")) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 检查允许下载的文件规则
|
|
|
- if (ArrayUtil.contains(MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION, FileTypeUtils.getFileType(resource)))
|
|
|
- {
|
|
|
+ if (ArrayUtil.contains(MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION, FileTypeUtils.getFileType(resource))) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
@@ -155,32 +132,24 @@ public class FileUtils extends org.apache.commons.io.FileUtils
|
|
|
/**
|
|
|
* 下载文件名重新编码
|
|
|
*
|
|
|
- * @param request 请求对象
|
|
|
+ * @param request 请求对象
|
|
|
* @param fileName 文件名
|
|
|
* @return 编码后的文件名
|
|
|
*/
|
|
|
- public static String setFileDownloadHeader(HttpServletRequest request, String fileName) throws UnsupportedEncodingException
|
|
|
- {
|
|
|
+ public static String setFileDownloadHeader(HttpServletRequest request, String fileName) throws UnsupportedEncodingException {
|
|
|
final String agent = request.getHeader("USER-AGENT");
|
|
|
String filename = fileName;
|
|
|
- if (agent.contains("MSIE"))
|
|
|
- {
|
|
|
+ if (agent.contains("MSIE")) {
|
|
|
// IE浏览器
|
|
|
filename = URLEncoder.encode(filename, "utf-8");
|
|
|
filename = filename.replace("+", " ");
|
|
|
- }
|
|
|
- else if (agent.contains("Firefox"))
|
|
|
- {
|
|
|
+ } else if (agent.contains("Firefox")) {
|
|
|
// 火狐浏览器
|
|
|
filename = new String(fileName.getBytes(), "ISO8859-1");
|
|
|
- }
|
|
|
- else if (agent.contains("Chrome"))
|
|
|
- {
|
|
|
+ } else if (agent.contains("Chrome")) {
|
|
|
// google浏览器
|
|
|
filename = URLEncoder.encode(filename, "utf-8");
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
+ } else {
|
|
|
// 其它浏览器
|
|
|
filename = URLEncoder.encode(filename, "utf-8");
|
|
|
}
|
|
|
@@ -190,12 +159,11 @@ public class FileUtils extends org.apache.commons.io.FileUtils
|
|
|
/**
|
|
|
* 下载文件名重新编码
|
|
|
*
|
|
|
- * @param response 响应对象
|
|
|
+ * @param response 响应对象
|
|
|
* @param realFileName 真实文件名
|
|
|
* @return
|
|
|
*/
|
|
|
- public static void setAttachmentResponseHeader(HttpServletResponse response, String realFileName) throws UnsupportedEncodingException
|
|
|
- {
|
|
|
+ public static void setAttachmentResponseHeader(HttpServletResponse response, String realFileName) throws UnsupportedEncodingException {
|
|
|
String percentEncodedFileName = percentEncode(realFileName);
|
|
|
|
|
|
StringBuilder contentDispositionValue = new StringBuilder();
|
|
|
@@ -215,24 +183,20 @@ public class FileUtils extends org.apache.commons.io.FileUtils
|
|
|
* @param s 需要百分号编码的字符串
|
|
|
* @return 百分号编码后的字符串
|
|
|
*/
|
|
|
- public static String percentEncode(String s) throws UnsupportedEncodingException
|
|
|
- {
|
|
|
+ public static String percentEncode(String s) throws UnsupportedEncodingException {
|
|
|
String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
|
|
|
return encode.replaceAll("\\+", "%20");
|
|
|
}
|
|
|
|
|
|
- public static String encodingZipFilename(String prefix)
|
|
|
- {
|
|
|
- String filename = prefix+"_"+ UUID.randomUUID().toString() + ".zip";
|
|
|
+ public static String encodingZipFilename(String prefix) {
|
|
|
+ String filename = prefix + "_" + UUID.randomUUID().toString() + ".zip";
|
|
|
return filename;
|
|
|
}
|
|
|
|
|
|
- public static String getZipAbsoluteFile(String filename)
|
|
|
- {
|
|
|
+ public static String getZipAbsoluteFile(String filename) {
|
|
|
String downloadPath = RuoYiConfig.getDownloadPath() + filename;
|
|
|
File desc = new File(downloadPath);
|
|
|
- if (!desc.getParentFile().exists())
|
|
|
- {
|
|
|
+ if (!desc.getParentFile().exists()) {
|
|
|
desc.getParentFile().mkdirs();
|
|
|
}
|
|
|
return downloadPath;
|
|
|
@@ -241,18 +205,19 @@ public class FileUtils extends org.apache.commons.io.FileUtils
|
|
|
public static void deleteDirectoryStream(Path path) {
|
|
|
try {
|
|
|
Files.delete(path);
|
|
|
- System.out.printf("删除文件成功:%s%n",path.toString());
|
|
|
+ System.out.printf("删除文件成功:%s%n", path.toString());
|
|
|
} catch (IOException e) {
|
|
|
System.err.printf("无法删除的路径 %s%n%s", path, e);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* File转MultipartFile
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @return org.springframework.web.multipart.MultipartFile
|
|
|
* @author change
|
|
|
* @date 2023/4/10 11:09
|
|
|
- * @param file
|
|
|
- * @return org.springframework.web.multipart.MultipartFile
|
|
|
*/
|
|
|
public static MultipartFile getMultipartFile(File file) {
|
|
|
FileItem item = new DiskFileItemFactory().createItem("file"
|
|
|
@@ -294,13 +259,13 @@ public class FileUtils extends org.apache.commons.io.FileUtils
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static void deleteFilePackage(String path){
|
|
|
+ public static void deleteFilePackage(String path) {
|
|
|
//删除本地资源
|
|
|
Path pathStr = Paths.get(path);
|
|
|
try (Stream<Path> walk = Files.walk(pathStr)) {
|
|
|
walk.sorted(Comparator.reverseOrder())
|
|
|
.forEach(FileUtils::deleteDirectoryStream);
|
|
|
- }catch (IOException e) {
|
|
|
+ } catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
throw new CustomException(e.getMessage());
|
|
|
}
|
|
|
@@ -309,8 +274,8 @@ public class FileUtils extends org.apache.commons.io.FileUtils
|
|
|
/**
|
|
|
* 压缩成ZIP 方法1
|
|
|
*
|
|
|
- * @param zipFileName 压缩文件夹路径
|
|
|
- * @param sourceFileName 要压缩的文件路径
|
|
|
+ * @param zipFileName 压缩文件夹路径
|
|
|
+ * @param sourceFileName 要压缩的文件路径
|
|
|
* @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
|
|
|
* false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
|
|
|
* @throws RuntimeException 压缩失败会抛出运行时异常
|
|
|
@@ -342,45 +307,45 @@ public class FileUtils extends org.apache.commons.io.FileUtils
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @Title: compress
|
|
|
- * @Description: TODO
|
|
|
- * @param filePaths 需要压缩的文件地址列表(绝对路径)
|
|
|
- * @param zipFilePath 需要压缩到哪个zip文件(无需创建这样一个zip,只需要指定一个全路径)
|
|
|
+ * @param filePaths 需要压缩的文件地址列表(绝对路径)
|
|
|
+ * @param zipFilePath 需要压缩到哪个zip文件(无需创建这样一个zip,只需要指定一个全路径)
|
|
|
* @param keepDirStructure 压缩后目录是否保持原目录结构
|
|
|
- * @throws IOException
|
|
|
* @return int 压缩成功的文件个数
|
|
|
+ * @throws IOException
|
|
|
+ * @Title: compress
|
|
|
+ * @Description: TODO
|
|
|
*/
|
|
|
- public static Boolean compressList(List<String> filePaths, String zipFilePath, Boolean keepDirStructure){
|
|
|
+ public static Boolean compressList(List<String> filePaths, String zipFilePath, Boolean keepDirStructure) {
|
|
|
byte[] buf = new byte[1024];
|
|
|
File zipFile = new File(zipFilePath);
|
|
|
int fileCount = 0;//记录压缩了几个文件?
|
|
|
try {
|
|
|
//zip文件不存在,则创建文件,用于压缩
|
|
|
- if(!zipFile.exists()){
|
|
|
+ if (!zipFile.exists()) {
|
|
|
zipFile.createNewFile();
|
|
|
}
|
|
|
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
|
|
|
- for(int i = 0; i < filePaths.size(); i++){
|
|
|
+ for (int i = 0; i < filePaths.size(); i++) {
|
|
|
String relativePath = filePaths.get(i);
|
|
|
- if(StringUtils.isEmpty(relativePath)){
|
|
|
+ if (StringUtils.isEmpty(relativePath)) {
|
|
|
continue;
|
|
|
}
|
|
|
File sourceFile = new File(relativePath);//绝对路径找到file
|
|
|
- if(sourceFile == null || !sourceFile.exists()){
|
|
|
+ if (sourceFile == null || !sourceFile.exists()) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
FileInputStream fis = new FileInputStream(sourceFile);
|
|
|
- if(keepDirStructure!=null && keepDirStructure){
|
|
|
+ if (keepDirStructure != null && keepDirStructure) {
|
|
|
//保持目录结构
|
|
|
zos.putNextEntry(new ZipEntry(relativePath));
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
//直接放到压缩包的根目录
|
|
|
zos.putNextEntry(new ZipEntry(sourceFile.getName()));
|
|
|
}
|
|
|
//System.out.println("压缩当前文件:"+sourceFile.getName());
|
|
|
int len;
|
|
|
- while((len = fis.read(buf)) > 0){
|
|
|
+ while ((len = fis.read(buf)) > 0) {
|
|
|
zos.write(buf, 0, len);
|
|
|
}
|
|
|
zos.closeEntry();
|
|
|
@@ -451,7 +416,7 @@ public class FileUtils extends org.apache.commons.io.FileUtils
|
|
|
* 解压
|
|
|
*
|
|
|
* @param inputStream zip压缩文件
|
|
|
- * @param descDir 指定的解压目录
|
|
|
+ * @param descDir 指定的解压目录
|
|
|
*/
|
|
|
public static void unzipWithStream(InputStream inputStream, String descDir) {
|
|
|
if (!descDir.endsWith(File.separator)) {
|
|
|
@@ -479,7 +444,7 @@ public class FileUtils extends org.apache.commons.io.FileUtils
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
- throw new CustomException("压缩包处理异常,异常信息{}"+ e);
|
|
|
+ throw new CustomException("压缩包处理异常,异常信息{}" + e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -487,7 +452,7 @@ public class FileUtils extends org.apache.commons.io.FileUtils
|
|
|
public static void writeFile(String filePath, ZipInputStream zipInputStream) {
|
|
|
File file = new File(filePath);
|
|
|
File directory = file.getParentFile();
|
|
|
- if (!directory.exists()){
|
|
|
+ if (!directory.exists()) {
|
|
|
directory.mkdirs();
|
|
|
}
|
|
|
try (OutputStream outputStream = new FileOutputStream(filePath)) {
|
|
|
@@ -506,7 +471,7 @@ public class FileUtils extends org.apache.commons.io.FileUtils
|
|
|
/**
|
|
|
* 打包压缩文件夹
|
|
|
*
|
|
|
- * @param folderPath 文件夹路径
|
|
|
+ * @param folderPath 文件夹路径
|
|
|
* @param zipFilePath 压缩后的文件路径
|
|
|
* @throws IOException IO异常
|
|
|
*/
|
|
|
@@ -533,10 +498,10 @@ public class FileUtils extends org.apache.commons.io.FileUtils
|
|
|
* 将文件夹及其中的文件递归添加到压缩流中
|
|
|
*
|
|
|
* @param parentPath 父级路径
|
|
|
- * @param folder 文件夹
|
|
|
- * @param zos Zip输出流
|
|
|
+ * @param folder 文件夹
|
|
|
+ * @param zos Zip输出流
|
|
|
* @throws FileNotFoundException 文件未找到异常
|
|
|
- * @throws IOException IO异常
|
|
|
+ * @throws IOException IO异常
|
|
|
*/
|
|
|
private static void addFolderToZip(String parentPath, File folder, ZipOutputStream zos) throws FileNotFoundException, IOException {
|
|
|
for (File file : folder.listFiles()) {
|
|
|
@@ -548,8 +513,14 @@ public class FileUtils extends org.apache.commons.io.FileUtils
|
|
|
try {
|
|
|
fis = new FileInputStream(file);
|
|
|
|
|
|
+ ZipEntry zipEntry = new ZipEntry(folder.getName() + "/" + file.getName());
|
|
|
+ String path2 = file.getParent().substring(file.getParent().lastIndexOf("\\"),file.getParent().length() - 1);
|
|
|
+ String replace = path2.replace("\\", "");
|
|
|
+ String replace1 = parentPath.replace("\\", "");
|
|
|
+ if (replace.equals(parentPath.replace("\\",""))){
|
|
|
+ zipEntry = new ZipEntry( file.getName());
|
|
|
+ }
|
|
|
// 新建Zip条目并将输入流加入到Zip包中
|
|
|
- ZipEntry zipEntry = new ZipEntry( folder.getName() + "/" + file.getName());
|
|
|
zos.putNextEntry(zipEntry);
|
|
|
|
|
|
byte[] bytes = new byte[1024];
|