From f8499faf6a13f77b1fe0082eaba5e9ad50d7d481 Mon Sep 17 00:00:00 2001
From: yangys <y_ys79@sina.com>
Date: 星期四, 07 八月 2025 14:22:31 +0800
Subject: [PATCH] 增加任务表

---
 blade-service/blade-mdm/src/main/java/org/springblade/mdm/utils/FileContentUtil.java |   97 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 95 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..315080f 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,92 @@
 			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[2048];
+		try {
+			int actRead = inputStream.read(buffer);
+
+			detector.handleData(buffer, 0, actRead);
+
+			//璇嗗埆缁撴潫蹇呴』璋冪敤杩欎釜鏂规硶
+			detector.dataEnd();
+		} catch (IOException e) {
+			throw new RuntimeException(e);
+		}
+		//绁炲鐨勬椂鍒诲氨鍦ㄨ繖涓柟娉曚簡锛岃繑鍥炲瓧绗﹂泦缂栫爜銆�
+		return detector.getDetectedCharset();
+	}
+
+	/**
+	 * 浠庤緭鍏ユ祦鑾峰彇鏂囨湰
+	 * @param ins 鏉熸祦
+	 * @return 鏂囨湰瀛楃
+	 * @throws IOException
+	 */
+	public static String getContentFromStream(InputStream ins) throws IOException {
+		byte[] bytes = ins.readAllBytes();
+		ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
+		Charset charset = Charsets.charset(detectFromInputStream(byteStream));
+		byteStream.reset();//閲嶇疆锛屼娇缁х画鍙敤
+
+		return new String(bytes, charset);
+	}
 }

--
Gitblit v1.9.3