From 96481362fed4eab7b96cc9016ece1917b43bbcc5 Mon Sep 17 00:00:00 2001
From: yangys <y_ys79@sina.com>
Date: 星期一, 04 八月 2025 14:54:38 +0800
Subject: [PATCH] dnc导出,增加写入下发目录的代码
---
blade-service/blade-mdm/src/main/java/org/springblade/mdm/utils/FileContentUtil.java | 83 ++++++++++++++++++++++++++++++++++++++++-
1 files changed, 81 insertions(+), 2 deletions(-)
diff --git a/blade-service/blade-mdm/src/main/java/org/springblade/mdm/utils/FileContentUtil.java b/blade-service/blade-mdm/src/main/java/org/springblade/mdm/utils/FileContentUtil.java
index 48fb510..1ea63b1 100644
--- a/blade-service/blade-mdm/src/main/java/org/springblade/mdm/utils/FileContentUtil.java
+++ b/blade-service/blade-mdm/src/main/java/org/springblade/mdm/utils/FileContentUtil.java
@@ -1,9 +1,14 @@
package org.springblade.mdm.utils;
import org.apache.tika.Tika;
+import org.mozilla.universalchardet.UniversalDetector;
+import org.springblade.core.tool.utils.Charsets;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.List;
public class FileContentUtil {
@@ -25,4 +30,78 @@
return false;
}
}
+
+ /**
+ * 鍦ㄦ枃鏈殑杈撳叆娴佷腑鎻掑叆涓�琛屾枃瀛�
+ * @param ins 杈撳叆娴�
+ * @param lineIndexToInsert 鎻掑叆鐨勪綅缃�0based
+ * @param textToInsert 鎻掑叆鐨勬枃鏈�
+ * @return 鎻掑叆鏂囨湰鍚庣殑杈撳叆娴�
+ * @throws IOException 寮傚父
+ */
+ public static InputStream insertLine(InputStream ins, int lineIndexToInsert,String textToInsert) throws IOException {
+ byte[] bytes = ins.readAllBytes();
+ ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
+ Charset charset = Charsets.charset(detectFromInputStream(byteStream));
+
+ byteStream.reset();//閲嶇疆锛屼娇缁х画鍙敤
+ return insertLine(byteStream, lineIndexToInsert,textToInsert, charset);
+ }
+ public static InputStream insertLine(InputStream ins, int lineIndexToInsert,String textToInsert,Charset charset) throws IOException {
+ List<String> lines = new ArrayList<>();
+ try (
+ InputStreamReader r = new InputStreamReader(ins,charset);//, charset
+ BufferedReader reader = new BufferedReader(r);) {
+
+ String line;
+ while ((line = reader.readLine()) != null) {
+ lines.add(line);
+ }
+ }
+ System.out.println("line="+lines);
+ // 鎻掑叆鏂拌
+ if (lines.size() < lineIndexToInsert) {
+ lines.add(textToInsert);
+ } else {
+ lines.add(lineIndexToInsert, textToInsert);
+ }
+
+ return convert(lines,charset);
+ }
+
+ public static InputStream convert(List<String> lines,Charset charset) {
+ // 浣跨敤绯荤粺琛屽垎闅旂杩炴帴鎵�鏈夎
+ String content = String.join(System.lineSeparator(), lines);
+ // 杞崲涓篣TF-8瀛楄妭娴�
+ return new ByteArrayInputStream(content.getBytes(charset));
+ }
+
+ public static String detect(byte[] content) {
+ UniversalDetector detector = new UniversalDetector(null);
+ //寮�濮嬬粰涓�閮ㄥ垎鏁版嵁锛岃瀛︿範涓�涓嬪晩锛屽畼鏂瑰缓璁槸1000涓猙yte宸﹀彸锛堝綋鐒惰繖1000涓猙yte浣犲緱鍖呭惈涓枃涔嬬被鐨勶級
+ detector.handleData(content, 0, content.length);
+ //璇嗗埆缁撴潫蹇呴』璋冪敤杩欎釜鏂规硶
+ detector.dataEnd();
+ //绁炲鐨勬椂鍒诲氨鍦ㄨ繖涓柟娉曚簡锛岃繑鍥炲瓧绗﹂泦缂栫爜銆�
+ return detector.getDetectedCharset();
+ }
+
+ public static String detectFromInputStream(InputStream inputStream) {
+ UniversalDetector detector = new UniversalDetector(null);
+ //寮�濮嬬粰涓�閮ㄥ垎鏁版嵁锛岃瀛︿範涓�涓嬪晩锛屽畼鏂瑰缓璁槸1000涓猙yte宸﹀彸锛堝綋鐒惰繖1000涓猙yte浣犲緱鍖呭惈涓枃涔嬬被鐨勶級
+ byte[] buffer = new byte[1024];
+ try {
+ int actRead = inputStream.read(buffer);
+
+ detector.handleData(buffer, 0, actRead);
+
+ //璇嗗埆缁撴潫蹇呴』璋冪敤杩欎釜鏂规硶
+ detector.dataEnd();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ //绁炲鐨勬椂鍒诲氨鍦ㄨ繖涓柟娉曚簡锛岃繑鍥炲瓧绗﹂泦缂栫爜銆�
+ return detector.getDetectedCharset();
+ }
+
}
--
Gitblit v1.9.3