|
@@ -6,8 +6,10 @@
|
|
package ieven.server.webapp.domain.file;
|
|
package ieven.server.webapp.domain.file;
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
import cn.hutool.core.io.IoUtil;
|
|
import cn.hutool.core.io.IoUtil;
|
|
import cn.hutool.core.util.IdUtil;
|
|
import cn.hutool.core.util.IdUtil;
|
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
import com.mongodb.client.gridfs.model.GridFSFile;
|
|
import com.mongodb.client.gridfs.model.GridFSFile;
|
|
import com.mongodb.client.result.DeleteResult;
|
|
import com.mongodb.client.result.DeleteResult;
|
|
import ieven.server.webapp.domain.IdInput;
|
|
import ieven.server.webapp.domain.IdInput;
|
|
@@ -16,14 +18,15 @@ import ieven.server.webapp.domain.data.Fields;
|
|
import ieven.server.webapp.domain.model.Model;
|
|
import ieven.server.webapp.domain.model.Model;
|
|
import ieven.server.webapp.infrastructure.wrapper.Mapped;
|
|
import ieven.server.webapp.infrastructure.wrapper.Mapped;
|
|
import ieven.server.webapp.service.MongoExcelService;
|
|
import ieven.server.webapp.service.MongoExcelService;
|
|
|
|
+import ieven.server.webapp.util.EncodeDetector;
|
|
import ieven.server.webapp.util.excel.ExcelXlsReader;
|
|
import ieven.server.webapp.util.excel.ExcelXlsReader;
|
|
import ieven.server.webapp.util.excel.ExcelXlsxReader;
|
|
import ieven.server.webapp.util.excel.ExcelXlsxReader;
|
|
import ieven.server.webapp.util.excel.PublicStatic;
|
|
import ieven.server.webapp.util.excel.PublicStatic;
|
|
|
|
+import info.monitorenter.cpdetector.io.*;
|
|
import org.apache.commons.csv.CSVFormat;
|
|
import org.apache.commons.csv.CSVFormat;
|
|
import org.apache.commons.csv.CSVParser;
|
|
import org.apache.commons.csv.CSVParser;
|
|
import org.apache.commons.csv.CSVRecord;
|
|
import org.apache.commons.csv.CSVRecord;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
-import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
|
|
|
|
import org.bson.types.ObjectId;
|
|
import org.bson.types.ObjectId;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.context.annotation.Lazy;
|
|
import org.springframework.context.annotation.Lazy;
|
|
@@ -40,11 +43,9 @@ import org.springframework.scheduling.annotation.Async;
|
|
import org.springframework.scheduling.annotation.AsyncResult;
|
|
import org.springframework.scheduling.annotation.AsyncResult;
|
|
import org.springframework.scheduling.annotation.EnableAsync;
|
|
import org.springframework.scheduling.annotation.EnableAsync;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
-import org.xml.sax.SAXException;
|
|
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
|
-import java.io.InputStream;
|
|
|
|
-import java.io.InputStreamReader;
|
|
|
|
|
|
+import java.io.*;
|
|
|
|
+import java.nio.charset.Charset;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
@@ -105,7 +106,13 @@ public class FileService {
|
|
mongoExcelService.insertRest();
|
|
mongoExcelService.insertRest();
|
|
} else if (filename.endsWith(".csv")) {
|
|
} else if (filename.endsWith(".csv")) {
|
|
mongoExcelService = new MongoExcelService(this.mongoTemplate, uploaded.getId());
|
|
mongoExcelService = new MongoExcelService(this.mongoTemplate, uploaded.getId());
|
|
- CSVParser csvParser = CSVFormat.EXCEL.parse(new InputStreamReader(inputStream, "utf8"));
|
|
|
|
|
|
+ String randomFileName = RandomUtil.randomString(5);
|
|
|
|
+ String tmpdir = System.getProperty("java.io.tmpdir");
|
|
|
|
+ File file = new File(tmpdir + File.separator + randomFileName + ".csv");
|
|
|
|
+ FileUtil.writeFromStream(inputStream, file);
|
|
|
|
+ String encoding = EncodeDetector.getEncoding(file);
|
|
|
|
+ CSVParser csvParser =
|
|
|
|
+ CSVFormat.EXCEL.parse(new InputStreamReader(new FileInputStream(file), encoding));
|
|
int curRow = 0;
|
|
int curRow = 0;
|
|
int curRowWxFile = 0;
|
|
int curRowWxFile = 0;
|
|
boolean wxFile = false;
|
|
boolean wxFile = false;
|
|
@@ -170,7 +177,7 @@ public class FileService {
|
|
}
|
|
}
|
|
mongoExcelService.insertRest();
|
|
mongoExcelService.insertRest();
|
|
}
|
|
}
|
|
- } catch (OpenXML4JException | SAXException | IOException var11) {
|
|
|
|
|
|
+ } catch (Exception var11) {
|
|
this.modifyStatus(uploaded.getId(), "ERROR");
|
|
this.modifyStatus(uploaded.getId(), "ERROR");
|
|
var11.printStackTrace();
|
|
var11.printStackTrace();
|
|
}
|
|
}
|
|
@@ -377,4 +384,40 @@ public class FileService {
|
|
Model model = this.mongoTemplate.findOne(query, Model.class, "model");
|
|
Model model = this.mongoTemplate.findOne(query, Model.class, "model");
|
|
return new AsyncResult(model != null ? model.getModelName() : "");
|
|
return new AsyncResult(model != null ? model.getModelName() : "");
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public static String getCharsetName(File file) throws IOException {
|
|
|
|
+ String charsetName = "UTF-8";
|
|
|
|
+ // 获取 CodepageDetectorProxy 实例
|
|
|
|
+ CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
|
|
|
|
+ // 添加解析器,会使用到添加的后 2 个 ext 里的 jar 包
|
|
|
|
+ detector.add(new ParsingDetector(false));
|
|
|
|
+ detector.add(JChardetFacade.getInstance());
|
|
|
|
+ detector.add(ASCIIDetector.getInstance());
|
|
|
|
+ detector.add(UnicodeDetector.getInstance());
|
|
|
|
+ Charset charset = detector.detectCodepage(file.toURI().toURL());
|
|
|
|
+ if (charset != null) {
|
|
|
|
+ charsetName = charset.name();
|
|
|
|
+ }
|
|
|
|
+ return charsetName;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) throws IOException {
|
|
|
|
+ File file =
|
|
|
|
+ new File(
|
|
|
|
+ "C:\\Users\\Administrator\\Desktop\\楼\\支付宝五联单数据格式\\20120303组织卖淫案_OR222713D2400411_注册信息_20210913100124.csv");
|
|
|
|
+ String charsetName = getCharsetName(file);
|
|
|
|
+ CSVParser csvParser =
|
|
|
|
+ CSVFormat.EXCEL.parse(new InputStreamReader(new FileInputStream(file), charsetName));
|
|
|
|
+ for (CSVRecord strings : csvParser) {
|
|
|
|
+ System.out.println(strings);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ file = new File("C:\\Users\\Administrator\\Desktop\\楼\\微信支付账单(2).csv");
|
|
|
|
+ charsetName = getCharsetName(file);
|
|
|
|
+ csvParser =
|
|
|
|
+ CSVFormat.EXCEL.parse(new InputStreamReader(new FileInputStream(file), charsetName));
|
|
|
|
+ for (CSVRecord strings : csvParser) {
|
|
|
|
+ System.out.println(strings);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|