|
|
@@ -49,6 +49,7 @@ import java.io.Serializable;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.nio.charset.Charset;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
+import java.nio.file.Files;
|
|
|
import java.util.*;
|
|
|
|
|
|
@Service
|
|
|
@@ -275,7 +276,7 @@ public class SpatialFilesDbServiceImpl implements ISpatialFilesDbService {
|
|
|
String tableId = null;
|
|
|
try {
|
|
|
shapefileDataStore = new ShapefileDataStore(shpFile.toURI().toURL());
|
|
|
- shapefileDataStore.setCharset(Charset.forName(readShpBM(shpFile.getAbsolutePath())));
|
|
|
+ shapefileDataStore.setCharset(Charset.forName(detectEncoding(shpFile.getAbsolutePath())));
|
|
|
// 这个typeNamae不传递,默认是文件名称
|
|
|
FeatureSource featuresource = shapefileDataStore.getFeatureSource(shapefileDataStore.getTypeNames()[0]);
|
|
|
|
|
|
@@ -348,6 +349,30 @@ public class SpatialFilesDbServiceImpl implements ISpatialFilesDbService {
|
|
|
return retFeatureList;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private String detectEncoding(String shpPath) {
|
|
|
+ // 1. 优先读 .cpg 文件
|
|
|
+ String cpgPath = shpPath.substring(0, shpPath.length() - 4) + ".cpg";
|
|
|
+ File cpgFile = new File(cpgPath);
|
|
|
+ if (cpgFile.exists()) {
|
|
|
+ try {
|
|
|
+ String encoding = new String(Files.readAllBytes(cpgFile.toPath()), "ASCII").trim();
|
|
|
+ System.out.println("从 .cpg 文件读取编码: " + encoding);
|
|
|
+ return encoding; // 返回 "gb18030"
|
|
|
+ } catch (IOException e) {
|
|
|
+ // 忽略,走下面的兜底
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 2. 没有 .cpg,再读 DBF 的 LDID 兜底
|
|
|
+ return readShpBM(shpPath);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
private String readShpBM(String shpPath) {
|
|
|
String dbfPath = shpPath.substring(0, shpPath.length() - 4) + ".dbf";
|
|
|
try (FileInputStream fis = new FileInputStream(dbfPath)) {
|