| | |
| | | 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.*; |
| | | 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; |
| | | |
| | |
| | | * @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); |
| | |
| | | public static String detectFromInputStream(InputStream inputStream) { |
| | | UniversalDetector detector = new UniversalDetector(null); |
| | | //开始给一部分数据,让学习一下啊,官方建议是1000个byte左右(当然这1000个byte你得包含中文之类的) |
| | | byte[] buffer = new byte[1024]; |
| | | byte[] buffer = new byte[2048]; |
| | | try { |
| | | int actRead = inputStream.read(buffer); |
| | | |
| | |
| | | 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 replaceWith 替换的文本 |
| | | * @param lineIndex 行索引 0 based |
| | | */ |
| | | public static InputStream replaceAtLine(InputStream ins, int lineIndex,String replaceWith) 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(replaceWith); |
| | | } else { |
| | | writer.write(line); |
| | | } |
| | | writer.newLine(); |
| | | currentLine++; |
| | | } |
| | | |
| | | } |
| | | return new ByteArrayInputStream(Files.newInputStream(tempFile).readAllBytes()); |
| | | } |
| | | } |