From 86c8efdb19a2f4be79a947650ef62ed79382011b Mon Sep 17 00:00:00 2001
From: yangys <y_ys79@sina.com>
Date: 星期六, 30 八月 2025 20:55:31 +0800
Subject: [PATCH] 重构机床回传文件;撤回后禁用撤回按钮
---
blade-service/blade-mdm/src/main/java/org/springblade/mdm/utils/FileContentUtil.java | 172 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 169 insertions(+), 3 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..7aef091 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,24 @@
package org.springblade.mdm.utils;
import org.apache.tika.Tika;
+import org.apache.tika.config.TikaConfig;
+import org.apache.tika.detect.CompositeDetector;
+import org.apache.tika.detect.DefaultDetector;
+import org.apache.tika.detect.Detector;
+import org.apache.tika.detect.TextDetector;
+import org.apache.tika.mime.MimeType;
+import org.apache.tika.mime.MimeTypeException;
+import org.apache.tika.mime.MimeTypes;
+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.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
public class FileContentUtil {
@@ -13,7 +28,9 @@
* @return
*/
public static boolean isTextFile(InputStream inputStream) {
- Tika tika = new Tika();
+ MimeTypes mimeTypes = MimeTypes.getDefaultMimeTypes();
+ Tika tika = new Tika(mimeTypes);
+
try {
String mimeType = tika.detect(inputStream);
//String mimeType = tika.detect(file);
@@ -25,4 +42,153 @@
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 inputStream
+ * @param lineIndex 琛岀储寮� 0based
+ * @return 琛屾枃鏈�
+ * @throws IOException
+ */
+ public static String readLineAt(InputStream inputStream,int lineIndex) throws IOException {
+ byte[] bytes = inputStream.readAllBytes();
+ ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
+ Charset charset = Charsets.charset(detectFromInputStream(byteStream));
+ byteStream.reset();//閲嶇疆锛屼娇缁х画鍙敤
+
+ String lineText = "";
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(byteStream,charset))) {
+ // 璺宠繃绗竴琛�
+ int current = 0;
+ while(current<=lineIndex && lineText!=null) {
+ lineText = reader.readLine();
+ current++;
+ }
+ }
+ return lineText;
+ }
+
+ /**
+ * 浠庤緭鍏ユ祦鑾峰彇鏂囨湰
+ * @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);
+ }
+
+ /**
+ * 鏇挎崲鏂囦欢涓殑鏌愪竴琛�
+ * @param ins 杈撳叆娴�
+ * @param repalceWith 鏇挎崲鍚庣殑鏂囨湰
+ * @param lineIndex 琛岀储寮� 0 based
+ */
+ public static InputStream replaceAtLine(InputStream ins, int lineIndex,String repalceWith) throws IOException {
+ byte[] bytes = ins.readAllBytes();
+ ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
+ Charset charset = Charsets.charset(detectFromInputStream(byteStream));
+ byteStream.reset();
+
+ Path tempFile = Files.createTempFile("temppro"+System.currentTimeMillis(), ".tmp");
+
+ try (InputStreamReader isr = new InputStreamReader(byteStream, charset);
+ BufferedReader reader = new BufferedReader(isr);
+ BufferedWriter writer = Files.newBufferedWriter(tempFile,charset)) {//
+
+ int currentLine = 0;
+ String line;
+
+ while ((line = reader.readLine()) != null) {
+ // 濡傛灉鏄洰鏍囪锛屽啓鍏ユ柊鍐呭
+ if (currentLine == lineIndex) {
+ writer.write(repalceWith);
+ } else {
+ writer.write(line);
+ }
+ writer.newLine();
+ currentLine++;
+ }
+
+ }
+ return new ByteArrayInputStream(Files.newInputStream(tempFile).readAllBytes());
+ }
}
--
Gitblit v1.9.3