From e5bd30e774dba285e0e22bbfa5e4e0d936d88e6b Mon Sep 17 00:00:00 2001
From: lyh <925863403@qq.com>
Date: 星期四, 12 三月 2026 10:32:26 +0800
Subject: [PATCH] 430代码 SQL server
---
src/main/java/com/lxzn/framework/utils/file/FileUtil.java | 237 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 235 insertions(+), 2 deletions(-)
diff --git a/src/main/java/com/lxzn/framework/utils/file/FileUtil.java b/src/main/java/com/lxzn/framework/utils/file/FileUtil.java
index a7994d9..979ebf4 100644
--- a/src/main/java/com/lxzn/framework/utils/file/FileUtil.java
+++ b/src/main/java/com/lxzn/framework/utils/file/FileUtil.java
@@ -5,7 +5,6 @@
import com.lxzn.framework.exception.ExceptionCast;
import com.lxzn.framework.utils.SHA256Util;
import com.lxzn.framework.utils.date.DateUtil;
-import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
@@ -17,7 +16,9 @@
import java.net.URLEncoder;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
+import java.nio.file.Path;
import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Date;
@@ -28,6 +29,11 @@
public class FileUtil {
private static String fileUploadFolder;
+ private static String fileNcGateway;
+ @Value("${fileNcGateway}")
+ public void setFileNcGateway(String fileNcGateway) {
+ FileUtil.fileNcGateway = fileNcGateway;
+ }
@Value("${fileHomePath}")
public void setFileUploadFolder(String fileUploadFolder) {
@@ -62,6 +68,179 @@
dto.setFileSize(fileSize);
dto.setFileSuffix(suffix);
return dto;
+ }
+
+ /**
+ * 鏂规硶涓�:浣跨敤 FileWriter 鍐欐枃浠�
+ * @param filepath 鏂囦欢鐩綍
+ * @param content 寰呭啓鍏ュ唴瀹�
+ * @throws IOException
+ */
+ public static void fileWriterSql(String filepath, String content) throws IOException {
+ OutputStreamWriter outputStreamWriter = null;
+ try {
+ File file = new File(filepath);
+ if (!file.exists()){
+ file.createNewFile();
+ }
+ FileOutputStream outputStream = new FileOutputStream(file);
+ if (outputStream != null){
+ outputStreamWriter = new OutputStreamWriter(outputStream, "utf-8");
+ outputStreamWriter.write(content);
+ outputStreamWriter.flush();
+ }
+ try {
+ if (outputStreamWriter != null){
+ outputStreamWriter.close();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ catch (IOException e) {
+ e.getMessage();
+ }
+ finally {
+ try {
+ if (outputStreamWriter != null){
+ outputStreamWriter.close();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ /**
+ * 鍒犻櫎鏂囦欢
+ *
+ * @param filePath
+ * @return
+ */
+ public static boolean deleteNcFile(String filePath) {
+ String lastFileReName = fileUploadFolder + "/" + filePath ;
+ boolean flag = false;
+ File file = new File(lastFileReName);
+ if (!file.exists()) {
+ return flag;
+ }
+ boolean b = file.delete();
+ if (!b) {
+ System.out.println("鏂囦欢鍒犻櫎澶辫触锛� " + filePath);
+ }
+ return true;
+ }
+
+ /**
+ * 澶嶅埗鏂囦欢锛堝甫閲嶈瘯銆佸ぇ灏忔牎楠屽拰MD5鏍¢獙锛岀‘淇濆鍒跺畬鏁存�т笌鍙潬鎬э級
+ * 浠� fileUploadFolder 涓嬬殑婧愯矾寰勫鍒跺埌 fileNcGateway 涓嬬殑鐩爣璺緞锛屽彲鎸囧畾鏂扮殑鏂囦欢鎵╁睍鍚�
+ * @param lastFile 婧愭枃浠剁殑鐩稿璺緞锛堢浉瀵逛簬 fileUploadFolder锛�
+ * @param newFile 鐩爣鏂囦欢鐨勭浉瀵硅矾寰勶紙鐩稿浜� fileNcGateway锛�
+ * @param ncFix 锛堝彲閫夛級鐩爣鏂囦欢鐨勬柊鎵╁睍鍚嶏紙涓嶅甫鐐癸紝濡� "txt"锛夛紝浼犲叆 null 鎴栫┖瀛楃涓插垯涓嶄慨鏀瑰師鎵╁睍鍚�
+ * @return 澶嶅埗鎴愬姛杩斿洖 true锛屽惁鍒欒繑鍥� false
+ */
+ public static boolean copyFileUpName(String lastFile, String newFile, String ncFix) {
+ // 1. 鏋勫缓婧愭枃浠跺拰鐩爣鏂囦欢鐨勭粷瀵硅矾寰�
+ String sourceFilePath = fileUploadFolder + "/" + lastFile;
+ String targetFilePath = fileNcGateway + "/" + newFile;
+
+ // 2. 濡傛灉鎸囧畾浜嗘柊鐨勬墿灞曞悕锛屽垯闄勫姞鍒扮洰鏍囨枃浠跺悕
+ if (StringUtils.isNotBlank(ncFix)) {
+ targetFilePath = targetFilePath + "." + ncFix;
+ }
+
+ // 3. 鍒涘缓鐩爣鏂囦欢澶�
+ File targetFile = new File(targetFilePath);
+ File targetParentDir = targetFile.getParentFile();
+ if (!targetParentDir.exists() && !targetParentDir.mkdirs()) {
+ log.error("鏃犳硶鍒涘缓鐩爣鏂囦欢澶�: {}", targetParentDir.getAbsolutePath());
+ return false;
+ }
+
+ // 4. 鑾峰彇婧愭枃浠堕鏈熷ぇ灏�
+ long expectedSize = -1;
+ File sourceFile = new File(sourceFilePath);
+ if (!sourceFile.exists()) {
+ log.error("婧愭枃浠朵笉瀛樺湪: {}", sourceFilePath);
+ return false;
+ }
+ try {
+ expectedSize = sourceFile.length();
+ log.info("鏂囦欢澶嶅埗: 婧�={}, 鐩爣={}, 澶у皬={}瀛楄妭", sourceFilePath, targetFilePath, expectedSize);
+ } catch (SecurityException e) {
+ log.error("鏃犳硶璁块棶婧愭枃浠�: {}", sourceFilePath, e);
+ return false;
+ }
+
+ // 5. 澶嶅埗鎿嶄綔锛堟渶澶氶噸璇�3娆★級
+ int maxRetries = 3;
+ int attempt = 0;
+ boolean copySuccess = false;
+
+ while (attempt < maxRetries && !copySuccess) {
+ attempt++;
+ try {
+ log.info("---- 寮�濮嬬{}娆″鍒跺皾璇� ----", attempt);
+
+ // 6. 鎵ц澶嶅埗鎿嶄綔锛堜娇鐢∟IO FileChannel浠ユ彁楂樻晥鐜囷級
+ long startTime = System.currentTimeMillis();
+ try (FileChannel in = new FileInputStream(sourceFilePath).getChannel();
+ FileChannel out = new FileOutputStream(targetFilePath).getChannel()) {
+ out.transferFrom(in, 0, in.size());
+ }
+ long endTime = System.currentTimeMillis();
+ log.info("鏂囦欢浼犺緭瀹屾垚锛岃�楁椂: {}姣", (endTime - startTime));
+
+ // 7. 鏂囦欢澶у皬楠岃瘉
+ File copiedFile = new File(targetFilePath);
+ long actualSize = copiedFile.length();
+
+ if (expectedSize != actualSize) {
+ log.warn("澶у皬涓嶄竴鑷�! 棰勬湡: {}B, 瀹為檯: {}B (宸��: {}B)",
+ expectedSize, actualSize, Math.abs(actualSize - expectedSize));
+ } else {
+ log.info("鏂囦欢澶у皬楠岃瘉鎴愬姛: {}B", actualSize);
+ copySuccess = true;
+ }
+
+ // 8. MD5鏍¢獙
+ if (copySuccess) {
+ String sourceMd5 = calculateMD5(sourceFilePath);
+ String targetMd5 = calculateMD5(targetFilePath);
+
+ if (!sourceMd5.equals(targetMd5)) {
+ log.warn("MD5涓嶄竴鑷�! 婧�: {}, 鐩爣: {}", sourceMd5, targetMd5);
+ copySuccess = false;
+ } else {
+ log.info("MD5楠岃瘉鎴愬姛");
+ }
+ }
+
+ } catch (IOException | SecurityException e) {
+ log.error("绗瑊}娆″鍒跺け璐�", attempt, e);
+ }
+
+ // 9. 閲嶈瘯闂撮殧澶勭悊
+ if (!copySuccess && attempt < maxRetries) {
+ try {
+ int delay = 200 * attempt; // 閫掑寤惰繜锛�200ms, 400ms
+ log.info("绛夊緟 {}ms 鍚庨噸璇�", delay);
+ Thread.sleep(delay);
+ } catch (InterruptedException ie) {
+ Thread.currentThread().interrupt();
+ log.warn("閲嶈瘯绛夊緟琚腑鏂�", ie);
+ break; // 濡傛灉绾跨▼琚腑鏂紝鍒欓��鍑洪噸璇曞惊鐜�
+ }
+ }
+ }
+
+ if (copySuccess) {
+ log.info("鏂囦欢澶嶅埗瀹屾垚: {}", targetFilePath);
+ } else {
+ log.error("鏂囦欢澶嶅埗澶辫触锛屽凡灏濊瘯{}娆°�傛簮鏂囦欢: {}, 鐩爣鏂囦欢: {}", maxRetries, sourceFilePath, targetFilePath);
+ }
+
+ return copySuccess;
}
public static FileUploadResult uploadFile(String fileName, InputStream fis) {
@@ -179,6 +358,54 @@
log.error(e.getMessage());
}
return -1;
+ }
+
+
+
+ /**
+ * 鍒犻櫎鏂囦欢
+ *
+ * @param filePath
+ * @return
+ */
+ public static boolean deleteFile(String filePath) {
+ boolean flag = false;
+ File file = new File(filePath);
+ if (!file.exists()) {
+ return flag;
+ }
+ boolean b = file.delete();
+ if (!b) {
+ try {
+ System.gc();
+ Path path = Paths.get(filePath);
+ Files.deleteIfExists(path);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ return true;
+ }
+
+
+ /**
+ *
+ * @param file
+ * @return
+ */
+ public static long selectFileSize(File file) {
+ try (FileInputStream fis = new FileInputStream(file)) {
+ long size = 0;
+ int read;
+ byte[] buffer = new byte[1024];
+ while ((read = fis.read(buffer)) != -1) {
+ size += read;
+ }
+ return size;
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return 0;
}
/**
@@ -438,7 +665,6 @@
}
}
-
public static String getFileAbsPath(String path, String fileName){
path = fileUploadFolder + path;
File file = new File(path, fileName);
@@ -448,5 +674,12 @@
return null;
}
+ public static String getFileAbsPathTxt(String path){
+ File file = new File(path);
+ if(file.exists()){
+ return file.getAbsolutePath();
+ }
+ return null;
+ }
}
--
Gitblit v1.9.3