diff --git a/server/src/main/java/cn/keking/service/CompressFileReader.java b/server/src/main/java/cn/keking/service/CompressFileReader.java index 0581f169..53977b2f 100644 --- a/server/src/main/java/cn/keking/service/CompressFileReader.java +++ b/server/src/main/java/cn/keking/service/CompressFileReader.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.OutputStream; import java.io.RandomAccessFile; import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -48,7 +49,6 @@ public class CompressFileReader { if (fileAttribute.isCompressFile()) { folderName = "_decompression" + folderName; } - Path folderPath = Paths.get(fileDir, folderName + packagePath); Files.createDirectories(folderPath); @@ -67,14 +67,18 @@ public class CompressFileReader { } return data.length; }, filePassword); - if (result != ExtractOperationResult.OK) { - throw new Exception("Failed to extract RAR file."); + ExtractOperationResult result1 = ExtractOperationResult.valueOf("WRONG_PASSWORD"); + if (result1.equals(result)) { + throw new Exception("Password"); + }else { + throw new Exception("Failed to extract RAR file."); + } } FileType type = FileType.typeFromUrl(filePathInsideArchive.toString()); - if (type.equals(FileType.PICTURE)) { - imgUrls.add(baseUrl + folderPath.relativize(filePathInsideArchive).toString().replace("\\", "/")); + if (type.equals(FileType.PICTURE)) { //图片缓存到集合,为了特殊符号需要进行编码 + imgUrls.add(baseUrl + URLEncoder.encode(fileName + packagePath+"/"+ folderPath.relativize(filePathInsideArchive).toString().replace("\\", "/"), "UTF-8")); } } } @@ -106,4 +110,4 @@ public class CompressFileReader { } -} +} \ No newline at end of file diff --git a/server/src/main/java/cn/keking/service/FileHandlerService.java b/server/src/main/java/cn/keking/service/FileHandlerService.java index 7ef3f568..2444bc75 100644 --- a/server/src/main/java/cn/keking/service/FileHandlerService.java +++ b/server/src/main/java/cn/keking/service/FileHandlerService.java @@ -474,11 +474,7 @@ public class FileHandlerService implements InitializingBean { boolean isCompressFile = !ObjectUtils.isEmpty(compressFileKey); if (isCompressFile) { //判断是否使用特定压缩包符号 try { - // http://127.0.0.1:8012/各类型文件1 - 副本.zip_/各类型文件/正常预览/PPT转的PDF.pdf?kkCompressfileKey=各类型文件1 - 副本.zip_ - // http://127.0.0.1:8012/preview/各类型文件1 - 副本.zip_/各类型文件/正常预览/PPT转的PDF.pdf?kkCompressfileKey=各类型文件1 - 副本.zip_ 获取路径就会错误 需要下面的方法 - String urlStrr = getSubString(compressFilePath, compressFileKey); //反代情况下添加前缀,只获取有压缩包字符的路径 - originFileName = compressFileKey + urlStrr.trim(); //拼接完整路径 - originFileName = URLDecoder.decode(originFileName, uriEncoding); //压缩包文件中文编码问题 + originFileName = URLDecoder.decode(originFileName, uriEncoding); //转义的文件名 解下出原始文件名 attribute.setSkipDownLoad(true); } catch (UnsupportedEncodingException e) { e.printStackTrace(); diff --git a/server/src/main/java/cn/keking/service/impl/CompressFilePreviewImpl.java b/server/src/main/java/cn/keking/service/impl/CompressFilePreviewImpl.java index e73fb7b2..13fc0791 100644 --- a/server/src/main/java/cn/keking/service/impl/CompressFilePreviewImpl.java +++ b/server/src/main/java/cn/keking/service/impl/CompressFilePreviewImpl.java @@ -54,15 +54,11 @@ public class CompressFilePreviewImpl implements FilePreview { try { fileTree = compressFileReader.unRar(filePath, filePassword, fileName, fileAttribute); } catch (Exception e) { - logger.error("Error processing RAR file: " + e.getMessage(), e); - Throwable[] throwableArray = ExceptionUtils.getThrowables(e); - for (Throwable throwable : throwableArray) { - if (throwable instanceof IOException || throwable instanceof EncryptedDocumentException) { - if (e.getMessage().toLowerCase().contains(Rar_PASSWORD_MSG)) { - model.addAttribute("needFilePassword", true); - return EXEL_FILE_PREVIEW_PAGE; - } - } + if (e.getMessage().toLowerCase().contains(Rar_PASSWORD_MSG)) { + model.addAttribute("needFilePassword", true); + return EXEL_FILE_PREVIEW_PAGE; + }else { + logger.error("Error processing RAR file: " + e.getMessage(), e); } } if (!ObjectUtils.isEmpty(fileTree)) { diff --git a/server/src/main/java/cn/keking/utils/RarUtils.java b/server/src/main/java/cn/keking/utils/RarUtils.java index 9362b487..795f9130 100644 --- a/server/src/main/java/cn/keking/utils/RarUtils.java +++ b/server/src/main/java/cn/keking/utils/RarUtils.java @@ -77,7 +77,7 @@ public class RarUtils { } public static String specialSymbols(String str) { //去除压缩包文件字符串中特殊符号 - Pattern p = Pattern.compile("\\s|\t|\r|\n|\\+|#|&|=|\\p{P}"); + Pattern p = Pattern.compile("\\s|\t|\r|\n|\\+|#|&|=|�|\\p{P}"); // Pattern p = Pattern.compile("\\s|\\+|#|&|=|\\p{P}"); Matcher m = p.matcher(str); return m.replaceAll(""); diff --git a/server/src/main/java/cn/keking/web/controller/FileController.java b/server/src/main/java/cn/keking/web/controller/FileController.java index 9a145d32..0ea6a914 100644 --- a/server/src/main/java/cn/keking/web/controller/FileController.java +++ b/server/src/main/java/cn/keking/web/controller/FileController.java @@ -213,6 +213,7 @@ public class FileController { String errorMsg = String.format(BASE64_DECODE_ERROR_MSG, "url"); return ReturnResponse.failure(errorMsg); } + fileUrl = fileUrl.replaceAll("http://", ""); if (KkFileUtils.isIllegalFileName(fileUrl)) { return ReturnResponse.failure("不允许访问的路径:"); } diff --git a/server/src/main/resources/web/compress.ftl b/server/src/main/resources/web/compress.ftl index 873e3dc8..b929d0c6 100644 --- a/server/src/main/resources/web/compress.ftl +++ b/server/src/main/resources/web/compress.ftl @@ -2,7 +2,7 @@
-