ソースを参照

销售线索文件上传修改编码格式

yangzian 3 年 前
コミット
5a56774f7f

+ 36 - 7
ruixuan-common/src/main/java/com/ruixuan/common/utils/file/FileUtils.java

@@ -1,12 +1,6 @@
 package com.ruixuan.common.utils.file;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.UnsupportedEncodingException;
+import java.io.*;
 import java.net.URLEncoder;
 import java.nio.charset.StandardCharsets;
 import javax.servlet.http.HttpServletRequest;
@@ -19,6 +13,7 @@ import org.apache.commons.lang3.ArrayUtils;
 import com.ruixuan.common.utils.DateUtils;
 import com.ruixuan.common.utils.StringUtils;
 import org.apache.commons.io.FilenameUtils;
+import org.springframework.web.multipart.MultipartFile;
 
 /**
  * 文件处理工具类
@@ -291,4 +286,38 @@ public class FileUtils
         return baseName;
     }
 
+
+    /**
+     * 获得文件编码
+     * @param file
+     * @return
+     * @throws Exception
+     */
+    public static String getFileEncoding(File file) throws Exception {
+        BufferedInputStream bin = new BufferedInputStream(new FileInputStream(file));
+        int p = (bin.read() << 8) + bin.read();
+        bin.close();
+        String code = null;
+
+        switch (p) {
+            case 0xefbb:
+                code = "UTF-8";
+                break;
+            case 0xfffe:
+                code = "Unicode";
+                break;
+            case 0xfeff:
+                code = "UTF-16BE";
+                break;
+            default:
+                code = "GBK";
+        }
+
+        return code;
+    }
+
+
+
+
+
 }

+ 9 - 2
ruixuan-salesLeads/src/main/java/com.ruixuan.salesLeads/utils/CsvImportUtil.java

@@ -2,6 +2,7 @@ package com.ruixuan.salesLeads.utils;
 
 
 import com.ruixuan.common.utils.StringUtils;
+import com.ruixuan.common.utils.file.FileUtils;
 import de.siegmar.fastcsv.reader.CsvContainer;
 import de.siegmar.fastcsv.reader.CsvReader;
 import de.siegmar.fastcsv.reader.CsvRow;
@@ -38,7 +39,6 @@ public class CsvImportUtil {
      * @throws Exception
      */
     public static List<Map<String,String>> readCSV(MultipartFile multipartFile)throws Exception {
-
         //multipartFile 转 file
         File file = CsvImportUtil.multipartFile2File(multipartFile);
 
@@ -49,7 +49,14 @@ public class CsvImportUtil {
 //        CsvContainer csv = csvReader.read(file, StandardCharsets.UTF_8);
         CsvContainer csv = csvReader.read(file, Charset.forName("gbk"));
 */
-        InputStreamReader isr = new InputStreamReader(new FileInputStream(file),"GBK");
+
+        String encoding = System.getProperty("file.encoding");
+        System.out.println("系统编码===="+encoding);
+
+        String charset = FileUtils.getFileEncoding(file);
+        System.out.println("文件编码===="+charset);
+
+        InputStreamReader isr = new InputStreamReader(new FileInputStream(file),charset);
         CsvContainer csv = csvReader.read(isr);
 
         //行数据