|
@@ -19,6 +19,7 @@ import java.io.*;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
|
|
+import java.nio.charset.Charset;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.Enumeration;
|
|
import java.util.Enumeration;
|
|
import java.util.LinkedList;
|
|
import java.util.LinkedList;
|
|
@@ -144,20 +145,14 @@ public class FileUtil {
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * 保存字符串到文件中 @throws IOException @throws
|
|
|
|
- */
|
|
|
|
- public static void saveAsFile(String path, String content) {
|
|
|
|
- saveAsFile(path, content, null);
|
|
|
|
- }
|
|
|
|
|
|
|
|
- public static void saveAsFile(String path, String content, String encoding) {
|
|
|
|
|
|
+ public static void saveAsFile(String path, String content, Charset encoding) {
|
|
if (path == null || content == null) {
|
|
if (path == null || content == null) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
if (encoding == null) {
|
|
if (encoding == null) {
|
|
- encoding = SystemConstant.CHARSET_NAME;
|
|
|
|
|
|
+ encoding = StandardCharsets.UTF_8;
|
|
}
|
|
}
|
|
BufferedWriter bw = null;
|
|
BufferedWriter bw = null;
|
|
try {
|
|
try {
|
|
@@ -170,7 +165,7 @@ public class FileUtil {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- IOUtils.write(content.getBytes(StandardCharsets.UTF_8), new FileOutputStream(file));
|
|
|
|
|
|
+ IOUtils.write(content.getBytes(encoding), new FileOutputStream(file));
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
throw ExceptionResultEnum.ERROR.exception("保存文件出错" + e.getMessage());
|
|
throw ExceptionResultEnum.ERROR.exception("保存文件出错" + e.getMessage());
|
|
} finally {
|
|
} finally {
|
|
@@ -409,74 +404,6 @@ public class FileUtil {
|
|
deleteDirectory(dirFile);
|
|
deleteDirectory(dirFile);
|
|
}
|
|
}
|
|
|
|
|
|
- private static Cipher initAESCipher(String sKey, String vector, int cipherMode) throws Exception {
|
|
|
|
- byte[] raw;
|
|
|
|
- raw = sKey.getBytes();
|
|
|
|
- SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
|
|
|
|
- Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
|
|
|
- IvParameterSpec iv = new IvParameterSpec(vector.getBytes());
|
|
|
|
- cipher.init(cipherMode, skeySpec, iv);
|
|
|
|
- return cipher;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 解密文件
|
|
|
|
- *
|
|
|
|
- * @param sourceFile 源文件
|
|
|
|
- * @param decryptFile 解密后的文件
|
|
|
|
- * @param sKey 密钥
|
|
|
|
- * @param vc 向量
|
|
|
|
- * @throws Exception
|
|
|
|
- */
|
|
|
|
- public static void decryptFile(File sourceFile, File decryptFile, String sKey, String vc) {
|
|
|
|
- decryptOrEncryptFile(Cipher.DECRYPT_MODE, sourceFile, decryptFile, sKey, vc);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 加密文件
|
|
|
|
- *
|
|
|
|
- * @param sourceFile 源文件
|
|
|
|
- * @param encryptFile 加密后的文件
|
|
|
|
- * @param sKey 密钥
|
|
|
|
- * @param vc 向量
|
|
|
|
- * @throws Exception
|
|
|
|
- */
|
|
|
|
- public static void encryptFile(File sourceFile, File encryptFile, String sKey, String vc) {
|
|
|
|
- decryptOrEncryptFile(Cipher.ENCRYPT_MODE, sourceFile, encryptFile, sKey, vc);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private static void decryptOrEncryptFile(int cipherMode, File sourceFile, File targetFile, String sKey, String vc) {
|
|
|
|
- InputStream inputStream = null;
|
|
|
|
- OutputStream outputStream = null;
|
|
|
|
- try {
|
|
|
|
- Cipher cipher = initAESCipher(sKey, vc, cipherMode);
|
|
|
|
- inputStream = new FileInputStream(sourceFile);
|
|
|
|
- outputStream = new FileOutputStream(targetFile);
|
|
|
|
- CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher);
|
|
|
|
- byte[] buffer = new byte[1024];
|
|
|
|
- int r;
|
|
|
|
- while ((r = inputStream.read(buffer)) >= 0) {
|
|
|
|
- cipherOutputStream.write(buffer, 0, r);
|
|
|
|
- }
|
|
|
|
- cipherOutputStream.close();
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- throw ExceptionResultEnum.ERROR.exception("加解密文件失败!" + e.getMessage());
|
|
|
|
- } finally {
|
|
|
|
- try {
|
|
|
|
- if (inputStream != null) {
|
|
|
|
- inputStream.close();
|
|
|
|
- }
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- }
|
|
|
|
- try {
|
|
|
|
- if (outputStream != null) {
|
|
|
|
- outputStream.close();
|
|
|
|
- }
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 文件转数组
|
|
* 文件转数组
|
|
*
|
|
*
|
|
@@ -640,11 +567,35 @@ public class FileUtil {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取临时文件根目录
|
|
|
|
+ *
|
|
|
|
+ * @param containNanoId 是否使用NanoId创建子目录
|
|
|
|
+ * @return 文件路径
|
|
|
|
+ */
|
|
|
|
+ public static String getFileTempDirPath(Boolean containNanoId) {
|
|
|
|
+ File fileTmpDir;
|
|
|
|
+ try {
|
|
|
|
+ if (containNanoId) {
|
|
|
|
+ fileTmpDir = new File(System.getProperty(SystemConstant.TMP_DIR), SystemConstant.getNanoId());
|
|
|
|
+ } else {
|
|
|
|
+ fileTmpDir = new File(System.getProperty(SystemConstant.TMP_DIR));
|
|
|
|
+ }
|
|
|
|
+ if (!fileTmpDir.exists()) {
|
|
|
|
+ fileTmpDir.mkdirs();
|
|
|
|
+ }
|
|
|
|
+ log.info("getFileTempDirPath_path:{}", fileTmpDir.getPath());
|
|
|
|
+ return fileTmpDir.getPath();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 生成文本文件
|
|
* 生成文本文件
|
|
*
|
|
*
|
|
- * @param file 文件
|
|
|
|
- * @param content 要生成的文件内容
|
|
|
|
|
|
+ * @param file 目标文件
|
|
|
|
+ * @param content 文件内容
|
|
*/
|
|
*/
|
|
public static File writeContent(File file, String content) {
|
|
public static File writeContent(File file, String content) {
|
|
try {
|
|
try {
|
|
@@ -667,7 +618,7 @@ public class FileUtil {
|
|
* @param zipFileName 压缩zip文件路径+文件名
|
|
* @param zipFileName 压缩zip文件路径+文件名
|
|
*/
|
|
*/
|
|
public static void downloadEncryptZip(HttpServletResponse response, File downloadPathFile, String zipFileName, String password) {
|
|
public static void downloadEncryptZip(HttpServletResponse response, File downloadPathFile, String zipFileName, String password) {
|
|
- if(StringUtils.isBlank(password)){
|
|
|
|
|
|
+ if (StringUtils.isBlank(password)) {
|
|
password = SystemConstant.ZIP_ENCRYPT_PWD;
|
|
password = SystemConstant.ZIP_ENCRYPT_PWD;
|
|
}
|
|
}
|
|
// 压缩zip文件和图片保存文件夹放在同一级
|
|
// 压缩zip文件和图片保存文件夹放在同一级
|