diff --git a/office-plugin/src/main/java/org/artofsolving/jodconverter/cli/Convert.java b/office-plugin/src/main/java/org/artofsolving/jodconverter/cli/Convert.java index 882942eb..33118111 100644 --- a/office-plugin/src/main/java/org/artofsolving/jodconverter/cli/Convert.java +++ b/office-plugin/src/main/java/org/artofsolving/jodconverter/cli/Convert.java @@ -14,6 +14,7 @@ package org.artofsolving.jodconverter.cli; import java.io.File; import java.io.IOException; +import java.nio.charset.Charset; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; @@ -86,7 +87,7 @@ public class Convert { DocumentFormatRegistry registry; if (commandLine.hasOption(OPTION_REGISTRY.getOpt())) { File registryFile = new File(commandLine.getOptionValue(OPTION_REGISTRY.getOpt())); - registry = new JsonDocumentFormatRegistry(FileUtils.readFileToString(registryFile)); + registry = new JsonDocumentFormatRegistry(FileUtils.readFileToString(registryFile, Charset.defaultCharset())); } else { registry = new DefaultDocumentFormatRegistry(); } @@ -122,5 +123,5 @@ public class Convert { officeManager.stop(); } } - + } diff --git a/office-plugin/src/main/java/org/artofsolving/jodconverter/document/JsonDocumentFormatRegistry.java b/office-plugin/src/main/java/org/artofsolving/jodconverter/document/JsonDocumentFormatRegistry.java index 1989dec9..086dee01 100644 --- a/office-plugin/src/main/java/org/artofsolving/jodconverter/document/JsonDocumentFormatRegistry.java +++ b/office-plugin/src/main/java/org/artofsolving/jodconverter/document/JsonDocumentFormatRegistry.java @@ -14,6 +14,7 @@ package org.artofsolving.jodconverter.document; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.Charset; import java.util.HashMap; import java.util.Map; @@ -25,7 +26,7 @@ import org.json.JSONObject; public class JsonDocumentFormatRegistry extends SimpleDocumentFormatRegistry { public JsonDocumentFormatRegistry(InputStream input) throws JSONException, IOException { - readJsonArray(IOUtils.toString(input)); + readJsonArray(IOUtils.toString(input, Charset.defaultCharset())); } public JsonDocumentFormatRegistry(String source) throws JSONException { diff --git a/office-plugin/src/main/java/org/artofsolving/jodconverter/model/FileProperties.java b/office-plugin/src/main/java/org/artofsolving/jodconverter/model/FileProperties.java index 7722dc0b..1f03cf27 100644 --- a/office-plugin/src/main/java/org/artofsolving/jodconverter/model/FileProperties.java +++ b/office-plugin/src/main/java/org/artofsolving/jodconverter/model/FileProperties.java @@ -15,7 +15,7 @@ public class FileProperties { } public Map toMap() { - Map map = new HashMap(); + Map map = new HashMap<>(); if (filePassword != null) { map.put("Password", filePassword); } diff --git a/office-plugin/src/main/java/org/artofsolving/jodconverter/office/OfficeProcess.java b/office-plugin/src/main/java/org/artofsolving/jodconverter/office/OfficeProcess.java index 3abf9c2c..105681d8 100644 --- a/office-plugin/src/main/java/org/artofsolving/jodconverter/office/OfficeProcess.java +++ b/office-plugin/src/main/java/org/artofsolving/jodconverter/office/OfficeProcess.java @@ -16,6 +16,7 @@ import static org.artofsolving.jodconverter.process.ProcessManager.PID_NOT_FOUND import static org.artofsolving.jodconverter.process.ProcessManager.PID_UNKNOWN; import java.io.File; import java.io.IOException; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -134,11 +135,11 @@ class OfficeProcess { logger.fine("no %OFFICE_HOME%/basis-link found; assuming it's OOo 2.x and we don't need to append URE and Basic paths"); return; } - String basisLinkText = FileUtils.readFileToString(basisLink).trim(); + String basisLinkText = FileUtils.readFileToString(basisLink, Charset.defaultCharset()).trim(); File basisHome = new File(officeHome, basisLinkText); File basisProgram = new File(basisHome, "program"); File ureLink = new File(basisHome, "ure-link"); - String ureLinkText = FileUtils.readFileToString(ureLink).trim(); + String ureLinkText = FileUtils.readFileToString(ureLink, Charset.defaultCharset()).trim(); File ureHome = new File(basisHome, ureLinkText); File ureBin = new File(ureHome, "bin"); Map environment = processBuilder.environment(); @@ -163,9 +164,9 @@ class OfficeProcess { } private class ExitCodeRetryable extends Retryable { - + private int exitCode; - + protected void attempt() throws TemporaryException, Exception { try { exitCode = process.exitValue(); @@ -173,7 +174,7 @@ class OfficeProcess { throw new TemporaryException(illegalThreadStateException); } } - + public int getExitCode() { return exitCode; } diff --git a/office-plugin/src/main/java/org/artofsolving/jodconverter/process/LinuxProcessManager.java b/office-plugin/src/main/java/org/artofsolving/jodconverter/process/LinuxProcessManager.java index 40d64e68..ec08db80 100644 --- a/office-plugin/src/main/java/org/artofsolving/jodconverter/process/LinuxProcessManager.java +++ b/office-plugin/src/main/java/org/artofsolving/jodconverter/process/LinuxProcessManager.java @@ -13,6 +13,7 @@ package org.artofsolving.jodconverter.process; import java.io.IOException; +import java.nio.charset.Charset; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -25,11 +26,10 @@ import org.apache.commons.io.IOUtils; *

* Should Work on Solaris too, except that the command line string * returned by ps there is limited to 80 characters and this affects - * {@link #findPid(String)}. */ public class LinuxProcessManager implements ProcessManager { - private static final Pattern PS_OUTPUT_LINE = Pattern.compile("^\\s*(\\d+)\\s+(.*)$"); + private static final Pattern PS_OUTPUT_LINE = Pattern.compile("^\\s*(\\d+)\\s+(.*)$"); private String[] runAsArgs; @@ -74,9 +74,7 @@ public class LinuxProcessManager implements ProcessManager { command = args; } Process process = new ProcessBuilder(command).start(); - @SuppressWarnings("unchecked") - List lines = IOUtils.readLines(process.getInputStream()); - return lines; + return IOUtils.readLines(process.getInputStream(), Charset.defaultCharset()); } } diff --git a/server/pom.xml b/server/pom.xml index 65fa796a..7fcd907e 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -183,7 +183,7 @@ aspose-cad 19.9 system - ${basedir}/lib/aspose-cad-19.9.jar + ${pom.basedir}/lib/aspose-cad-19.9.jar @@ -191,7 +191,7 @@ cpdetector 1.04 system - ${basedir}/lib/cpdetector-1.04.jar + ${pom.basedir}/lib/cpdetector-1.04.jar @@ -260,14 +260,14 @@ jai_core 1.1.3 system - ${basedir}/lib/jai_core-1.1.3.jar + ${pom.basedir}/lib/jai_core-1.1.3.jar javax.media jai_codec 1.1.3 system - ${basedir}/lib/jai_codec-1.1.3.jar + ${pom.basedir}/lib/jai_codec-1.1.3.jar diff --git a/server/src/main/java/cn/keking/service/CompressFileReader.java b/server/src/main/java/cn/keking/service/CompressFileReader.java index e73751a5..9ccc20d4 100644 --- a/server/src/main/java/cn/keking/service/CompressFileReader.java +++ b/server/src/main/java/cn/keking/service/CompressFileReader.java @@ -13,11 +13,8 @@ import com.github.junrar.rarfile.FileHeader; import net.sf.sevenzipjbinding.*; import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream; import net.sf.sevenzipjbinding.simple.ISimpleInArchive; -import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry; -import org.apache.commons.compress.archivers.sevenz.SevenZFile; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipFile; -import org.apache.commons.io.FileUtils; import org.springframework.stereotype.Component; import java.io.*; @@ -47,59 +44,6 @@ public class CompressFileReader { this.fileHandlerService = fileHandlerService; } - public String readZipFile(String filePath, String fileKey) { - String archiveSeparator = "/"; - Map appender = new HashMap<>(); - List imgUrls = new LinkedList<>(); - String baseUrl = BaseUrlFilter.getBaseUrl(); - String archiveFileName = fileHandlerService.getFileNameFromPath(filePath); - try { - ZipFile zipFile = new ZipFile(filePath, KkFileUtils.getFileEncode(filePath)); - Enumeration entries = zipFile.getEntries(); - // 排序 - entries = sortZipEntries(entries); - List> entriesToBeExtracted = new LinkedList<>(); - while (entries.hasMoreElements()) { - ZipArchiveEntry entry = entries.nextElement(); - String fullName = entry.getName().replaceAll("//", "").replaceAll("\\\\", ""); - int level = fullName.split(archiveSeparator).length; - // 展示名 - String originName = getLastFileName(fullName, archiveSeparator); - String childName = level + "_" + originName; - boolean directory = entry.isDirectory(); - if (!directory) { - childName = archiveFileName + "_" + originName; - entriesToBeExtracted.add(Collections.singletonMap(childName, entry)); - } - String parentName = getLast2FileName(fullName, archiveSeparator, archiveFileName); - parentName = (level - 1) + "_" + parentName; - FileType type = FileType.typeFromUrl(childName); - if (type.equals(FileType.PICTURE)) {//添加图片文件到图片列表 - imgUrls.add(baseUrl + childName); - } - FileNode node = new FileNode(originName, childName, parentName, new ArrayList<>(), directory, fileKey); - addNodes(appender, parentName, node); - appender.put(childName, node); - } - // 开启新的线程处理文件解压 - executors.submit(new ZipExtractorWorker(entriesToBeExtracted, zipFile, filePath)); - fileHandlerService.putImgCache(fileKey, imgUrls); - return new ObjectMapper().writeValueAsString(appender.get("")); - } catch (IOException e) { - e.printStackTrace(); - return null; - } - } - - private Enumeration sortZipEntries(Enumeration entries) { - List sortedEntries = new LinkedList<>(); - while (entries.hasMoreElements()) { - sortedEntries.add(entries.nextElement()); - } - sortedEntries.sort(Comparator.comparingInt(o -> o.getName().length())); - return Collections.enumeration(sortedEntries); - } - public String unRar(String filePath, String fileKey) { Map appender = new HashMap<>(); List imgUrls = new ArrayList<>(); @@ -110,14 +54,14 @@ public class CompressFileReader { List> headersToBeExtract = new ArrayList<>(); for (FileHeaderRar header : items) { String fullName = header.getFileNameW(); - String originName = getLastFileName(fullName, File.separator); + String originName = getLastFileName(fullName); String childName = originName; boolean directory = header.getDirectory(); if (!directory) { childName = archiveFileName + "_" + originName; headersToBeExtract.add(Collections.singletonMap(childName, header)); } - String parentName = getLast2FileName(fullName, File.separator, archiveFileName); + String parentName = getLast2FileName(fullName, archiveFileName); FileType type = FileType.typeFromUrl(childName); if (type.equals(FileType.PICTURE)) { imgUrls.add(baseUrl + childName); @@ -183,58 +127,6 @@ public class CompressFileReader { return itemPath; } - public String read7zFile(String filePath, String fileKey) { - String archiveSeparator = "/"; - Map appender = new HashMap<>(); - List imgUrls = new ArrayList<>(); - String baseUrl = BaseUrlFilter.getBaseUrl(); - String archiveFileName = fileHandlerService.getFileNameFromPath(filePath); - try { - SevenZFile zipFile = new SevenZFile(new File(filePath)); - Iterable entries = zipFile.getEntries(); - // 排序 - Enumeration newEntries = sortSevenZEntries(entries); - List> entriesToBeExtracted = new ArrayList<>(); - while (newEntries.hasMoreElements()) { - SevenZArchiveEntry entry = newEntries.nextElement(); - String fullName = entry.getName().replaceAll("//", "").replaceAll("\\\\", ""); - int level = fullName.split(archiveSeparator).length; - // 展示名 - String originName = getLastFileName(fullName, archiveSeparator); - String childName = level + "_" + originName; - boolean directory = entry.isDirectory(); - if (!directory) { - childName = archiveFileName + "_" + originName; - entriesToBeExtracted.add(Collections.singletonMap(childName, entry)); - } - String parentName = getLast2FileName(fullName, archiveSeparator, archiveFileName); - parentName = (level - 1) + "_" + parentName; - FileType type = FileType.typeFromUrl(childName); - if (type.equals(FileType.PICTURE)) {//添加图片文件到图片列表 - imgUrls.add(baseUrl + childName); - } - FileNode node = new FileNode(originName, childName, parentName, new ArrayList<>(), directory, fileKey); - addNodes(appender, parentName, node); - appender.put(childName, node); - } - // 开启新的线程处理文件解压 - executors.submit(new SevenZExtractorWorker(entriesToBeExtracted, filePath)); - fileHandlerService.putImgCache(fileKey, imgUrls); - return new ObjectMapper().writeValueAsString(appender.get("")); - } catch (IOException e) { - e.printStackTrace(); - return null; - } - } - - - private Enumeration sortSevenZEntries(Iterable entries) { - List sortedEntries = new ArrayList<>(); - for (SevenZArchiveEntry entry : entries) { - sortedEntries.add(entry); - } - return Collections.enumeration(sortedEntries); - } private void addNodes(Map appender, String parentName, FileNode node) { if (appender.containsKey(parentName)) { @@ -249,53 +141,28 @@ public class CompressFileReader { } } - private List sortedHeaders(List headers) { - List sortedHeaders = new ArrayList<>(); - Map mapHeaders = new TreeMap<>(); - headers.forEach( - header -> - mapHeaders.put( - new Integer(0).equals(header.getFileNameW().length()) - ? header.getFileNameString().length() - : header.getFileNameW().length(), - header)); - for (Map.Entry entry : mapHeaders.entrySet()) { - for (FileHeader header : headers) { - if (entry - .getKey() - .equals( - new Integer(0).equals(header.getFileNameW().length()) - ? header.getFileNameString().length() - : header.getFileNameW().length())) { - sortedHeaders.add(header); - } - } - } - return sortedHeaders; - } - - private static String getLast2FileName(String fullName, String seperator, String rootName) { - if (fullName.endsWith(seperator)) { + private static String getLast2FileName(String fullName, String rootName) { + if (fullName.endsWith(File.separator)) { fullName = fullName.substring(0, fullName.length() - 1); } // 1.获取剩余部分 - int endIndex = fullName.lastIndexOf(seperator); + int endIndex = fullName.lastIndexOf(File.separator); String leftPath = fullName.substring(0, endIndex == -1 ? 0 : endIndex); if (leftPath.length() > 1) { // 2.获取倒数第二个 - return getLastFileName(leftPath, seperator); + return getLastFileName(leftPath); } else { return rootName; } } - private static String getLastFileName(String fullName, String seperator) { - if (fullName.endsWith(seperator)) { + private static String getLastFileName(String fullName) { + if (fullName.endsWith(File.separator)) { fullName = fullName.substring(0, fullName.length() - 1); } String newName = fullName; - if (fullName.contains(seperator)) { - newName = fullName.substring(fullName.lastIndexOf(seperator) + 1); + if (fullName.contains(File.separator)) { + newName = fullName.substring(fullName.lastIndexOf(File.separator) + 1); } return newName; } @@ -456,50 +323,6 @@ public class CompressFileReader { } } - class SevenZExtractorWorker implements Runnable { - - private final List> entriesToBeExtracted; - private final String filePath; - - public SevenZExtractorWorker(List> entriesToBeExtracted, String filePath) { - this.entriesToBeExtracted = entriesToBeExtracted; - this.filePath = filePath; - } - - @Override - public void run() { - try { - SevenZFile sevenZFile = new SevenZFile(new File(filePath)); - SevenZArchiveEntry entry = sevenZFile.getNextEntry(); - while (entry != null) { - if (entry.isDirectory()) { - entry = sevenZFile.getNextEntry(); - continue; - } - String childName = "default_file"; - SevenZArchiveEntry entry1; - for (Map entryMap : entriesToBeExtracted) { - childName = entryMap.keySet().iterator().next(); - entry1 = entryMap.values().iterator().next(); - if (entry.getName().equals(entry1.getName())) { - break; - } - } - FileOutputStream out = new FileOutputStream(fileDir + childName); - byte[] content = new byte[(int) entry.getSize()]; - sevenZFile.read(content, 0, content.length); - out.write(content); - out.close(); - entry = sevenZFile.getNextEntry(); - } - sevenZFile.close(); - } catch (IOException e) { - e.printStackTrace(); - } - KkFileUtils.deleteFileByPath(filePath); - } - } - class RarExtractorWorker implements Runnable { private final List> headersToBeExtracted; diff --git a/server/src/main/java/cn/keking/service/cache/impl/CacheServiceJDKImpl.java b/server/src/main/java/cn/keking/service/cache/impl/CacheServiceJDKImpl.java index d484c2ac..04ffefe9 100644 --- a/server/src/main/java/cn/keking/service/cache/impl/CacheServiceJDKImpl.java +++ b/server/src/main/java/cn/keking/service/cache/impl/CacheServiceJDKImpl.java @@ -3,9 +3,9 @@ package cn.keking.service.cache.impl; import cn.keking.service.cache.CacheService; import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap; import com.googlecode.concurrentlinkedhashmap.Weighers; +import org.apache.commons.lang3.StringUtils; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.stereotype.Service; -import org.springframework.util.StringUtils; import javax.annotation.PostConstruct; import java.util.ArrayList; diff --git a/server/src/main/java/cn/keking/service/cache/impl/CacheServiceRocksDBImpl.java b/server/src/main/java/cn/keking/service/cache/impl/CacheServiceRocksDBImpl.java index bc3a1045..0e4910ef 100644 --- a/server/src/main/java/cn/keking/service/cache/impl/CacheServiceRocksDBImpl.java +++ b/server/src/main/java/cn/keking/service/cache/impl/CacheServiceRocksDBImpl.java @@ -177,6 +177,7 @@ public class CacheServiceRocksDBImpl implements CacheService { } @Override + @SuppressWarnings("unchecked") public Map getMediaConvertCache() { Map result = new HashMap<>(); try{ @@ -199,6 +200,7 @@ public class CacheServiceRocksDBImpl implements CacheService { } @Override + @SuppressWarnings("unchecked") public String getMediaConvertCache(String key) { String result = ""; try{ diff --git a/server/src/main/java/cn/keking/utils/FtpUtils.java b/server/src/main/java/cn/keking/utils/FtpUtils.java index 683697b4..8a736fd4 100644 --- a/server/src/main/java/cn/keking/utils/FtpUtils.java +++ b/server/src/main/java/cn/keking/utils/FtpUtils.java @@ -1,17 +1,18 @@ package cn.keking.utils; import cn.keking.config.ConfigConstants; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.util.StringUtils; -import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; /** * @auther: chenjh @@ -46,7 +47,7 @@ public class FtpUtils { String remoteFilePath = url.getPath(); LOGGER.debug("FTP connection url:{}, username:{}, password:{}, controlEncoding:{}, localFilePath:{}", ftpUrl, username, password, controlEncoding, localFilePath); FTPClient ftpClient = connect(host, port, username, password, controlEncoding); - OutputStream outputStream = new FileOutputStream(localFilePath); + OutputStream outputStream = Files.newOutputStream(Paths.get(localFilePath)); ftpClient.enterLocalPassiveMode(); boolean downloadResult = ftpClient.retrieveFile(new String(remoteFilePath.getBytes(controlEncoding), StandardCharsets.ISO_8859_1), outputStream); LOGGER.debug("FTP download result {}", downloadResult);