|
|
|
@ -1,12 +1,11 @@ |
|
|
|
package com.ruoyi.web.controller.system; |
|
|
|
|
|
|
|
import java.io.BufferedReader; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.InputStreamReader; |
|
|
|
import java.io.*; |
|
|
|
import java.nio.charset.Charset; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.util.*; |
|
|
|
import java.util.regex.Matcher; |
|
|
|
import java.util.regex.Pattern; |
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
|
|
|
|
@ -69,30 +68,31 @@ public class ZhyPointController extends BaseController |
|
|
|
if (file.isEmpty()) { |
|
|
|
return AjaxResult.error("上传的文件为空"); |
|
|
|
} |
|
|
|
|
|
|
|
// 2. 校验是否为 CSV 文件
|
|
|
|
String fileName = file.getOriginalFilename(); |
|
|
|
if (fileName == null || !fileName.toLowerCase().endsWith(".csv")) { |
|
|
|
return AjaxResult.error("仅支持上传 CSV 格式的文件"); |
|
|
|
} |
|
|
|
|
|
|
|
// 3. 自动检测编码并读取 CSV 文件(支持 GBK / UTF-8)
|
|
|
|
// 3. 自动检测编码并读取 CSV 文件(支持 GBK / UTF-8)
|
|
|
|
List<Map<String, String>> dataList = new ArrayList<>(); |
|
|
|
String[] headers = null; |
|
|
|
|
|
|
|
InputStream inputStream = null; |
|
|
|
BufferedReader reader = null; |
|
|
|
|
|
|
|
try { |
|
|
|
inputStream = file.getInputStream(); |
|
|
|
// ✅ 关键修改:先将整个文件读入内存(byte[]),避免流只能读一次的问题
|
|
|
|
byte[] fileBytes = file.getBytes(); // ✅ 使用 MultipartFile.getBytes() 安全读取全部字节
|
|
|
|
|
|
|
|
// 🔍 第一步:探测编码
|
|
|
|
String encoding = detectCsvEncoding(inputStream); |
|
|
|
// 🔍 第一步:使用字节数组探测编码(不会破坏流)
|
|
|
|
String encoding = detectCsvEncoding(new ByteArrayInputStream(fileBytes)); |
|
|
|
System.out.println("自动检测到编码: " + encoding); |
|
|
|
|
|
|
|
// 重新打开流(因为探测时已读取部分数据)
|
|
|
|
inputStream.close(); |
|
|
|
inputStream = file.getInputStream(); |
|
|
|
|
|
|
|
reader = new BufferedReader(new InputStreamReader(inputStream, encoding)); |
|
|
|
// ✅ 使用字节数组创建新的 InputStream,确保内容完整
|
|
|
|
InputStream decodedInputStream = new ByteArrayInputStream(fileBytes); |
|
|
|
reader = new BufferedReader(new InputStreamReader(decodedInputStream, encoding)); |
|
|
|
|
|
|
|
String line; |
|
|
|
int lineNumber = 0; |
|
|
|
@ -100,7 +100,7 @@ public class ZhyPointController extends BaseController |
|
|
|
while ((line = reader.readLine()) != null) { |
|
|
|
lineNumber++; |
|
|
|
|
|
|
|
// 去除 BOM(如果存在)
|
|
|
|
// 去除 BOM(如果存在,仅第一行)
|
|
|
|
if (lineNumber == 1 && line.startsWith("\uFEFF")) { |
|
|
|
line = line.substring(1); |
|
|
|
} |
|
|
|
@ -108,7 +108,7 @@ public class ZhyPointController extends BaseController |
|
|
|
// ✅ 替换 split(",") 为能处理引号内逗号的解析方法
|
|
|
|
String[] row = parseCsvLine(line); |
|
|
|
for (int i = 0; i < row.length; i++) { |
|
|
|
row[i] = row[i].trim(); // 去空格
|
|
|
|
row[i] = row[i] != null ? row[i].trim() : ""; // 防空指针
|
|
|
|
} |
|
|
|
|
|
|
|
if (lineNumber == 1) { |
|
|
|
@ -130,16 +130,25 @@ public class ZhyPointController extends BaseController |
|
|
|
} |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return AjaxResult.error("读取文件时发生错误:" + e.getMessage()); |
|
|
|
} finally { |
|
|
|
try { |
|
|
|
if (reader != null) reader.close(); |
|
|
|
if (inputStream != null) inputStream.close(); |
|
|
|
// inputStream 由 byte[] 创建,无需额外 close MultipartFile 流(Spring 会管理)
|
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
for (Map<String, String> data : dataList) { |
|
|
|
String mainKeyword = data.get("main_keyword"); |
|
|
|
Pattern pattern = Pattern.compile("'([^']*)'"); |
|
|
|
Matcher matcher = pattern.matcher(mainKeyword); |
|
|
|
while (matcher.find()) { |
|
|
|
data.put("main_keyword", matcher.group(1)); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
pointService.buildPoint(dataList); |
|
|
|
|
|
|
|
return AjaxResult.success(); |
|
|
|
|