java處理csv文件上傳示例詳解
前言:示例只是做了一個(gè)最最基礎(chǔ)的上傳csv的示例,如果要引用到代碼中去,還需要根據(jù)自己的業(yè)務(wù)自行添加一些邏輯處理。
ReadCsvUtil工具類
package com.hanfengyeqiao.gjb.utils; import java.io.*; import java.util.*; /** * csv工具類 */ public class ReadCsvUtil { private static final String FIX="\uFEFF"; /** * 獲取csv文件內(nèi)容 * @return 對(duì)象list */ public static List<Map<String,Object>> getResource(byte[] bate) throws IOException { List<Map<String,Object>> allString = new ArrayList(); Map<String,Object> callLogInfo ; List<String> list = new ArrayList(); // 獲取文件內(nèi)容 list = getSource(bate); // 獲取文件表頭 List<String> title = Arrays.asList(list.get(0).split(",")); String customerName = title.get(0).trim(); String customerNo = title.get(1).trim(); // 頭部會(huì)帶有"\uFEFF"值 if(customerName.startsWith(FIX)){ customerName = customerName.replace(FIX, ""); } callLogInfo = new HashMap(); callLogInfo.put("param1",customerName); callLogInfo.put("param2",customerNo); allString.add(callLogInfo); list.remove(0); // 循環(huán)內(nèi)容 for(int i = 0; i<list.size();i++){ List<String> content = Arrays.asList(list.get(i).split(",")); // 當(dāng)沒(méi)有添加額外參數(shù)時(shí) if(content!=null){ callLogInfo = new HashMap(); callLogInfo.put("param1",content.get(0)); callLogInfo.put("param2",content.get(1)); allString.add(callLogInfo); } } return allString; } /** * 讀文件數(shù)據(jù) */ public static List<String> getSource(byte[] bate) throws IOException { BufferedReader br = null; ByteArrayInputStream fis=null; InputStreamReader isr = null; try { fis = new ByteArrayInputStream(bate); //指定以UTF-8編碼讀入 isr = new InputStreamReader(fis,"UTF-8"); br = new BufferedReader(isr); } catch (Exception e) { e.printStackTrace(); } String line; String everyLine ; List<String> allString = new ArrayList<>(); try { //讀取到的內(nèi)容給line變量 while ((line = br.readLine()) != null){ everyLine = line; allString.add(everyLine); } } catch (IOException e) { e.printStackTrace(); }finally { if(fis != null){ fis.close(); } if(isr != null){ isr.close(); } } return allString; } }
控制器(這里用的springboot):
package com.hanfengyeqiao.gjb.controller.admin; import com.hanfengyeqiao.gjb.utils.ReadCsvUtil; import io.swagger.annotations.Api; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.util.List; import java.util.Map; @Api(tags = "") @RestController @RequestMapping("/admin") public class AdminCertController { @RequestMapping("/test/upload") public void upload(HttpServletRequest request, MultipartFile upfile) throws Exception { if (request.getMethod().equals("POST")) { byte[] bate =upfile.getBytes(); List<Map<String,Object>> list=ReadCsvUtil.getResource(bate); if(list!=null){ for(Map<String,Object> m:list){ System.out.println("param1:"+m.get("param1")+";param2:"+m.get("param2")+"。"); } } } } }
html代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>test</title> </head> <body> <form action="http://localhost:8088/admin/test/upload" method="post" enctype="multipart/form-data"> 上傳:<input type="file" name="upfile"/> <input type="submit" value="提交"/> </form> </body> <script type="text/javascript"> </script> </html>
示例文件
運(yùn)行結(jié)果
在處理csv文件的時(shí)候容易出現(xiàn)編碼上的問(wèn)題,小伙伴們寫代碼的時(shí)候要多注意一下!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot ActiveMQ如何設(shè)置訪問(wèn)密碼
這篇文章主要介紹了Spring Boot ActiveMQ如何設(shè)置訪問(wèn)密碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07處理Log4j2不能打印行號(hào)的問(wèn)題(AsyncLogger)
這篇文章主要介紹了處理Log4j2不能打印行號(hào)的問(wèn)題(AsyncLogger),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12詳解java如何實(shí)現(xiàn)將數(shù)據(jù)導(dǎo)出為yaml
這篇文章主要為大家詳細(xì)介紹了java如何利用snakeyaml和freemarker實(shí)現(xiàn)將數(shù)據(jù)導(dǎo)出為yaml文件,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下2023-11-11Java中的PrintWriter 介紹_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
PrintWriter 是字符類型的打印輸出流,它繼承于Writer。接下來(lái)通過(guò)本文給大家介紹java中的 PrintWriter 相關(guān)知識(shí),感興趣的朋友一起學(xué)習(xí)吧2017-05-05Java實(shí)現(xiàn)終止線程池中正在運(yùn)行的定時(shí)任務(wù)
本篇文章給大家分享了JAVA中實(shí)現(xiàn)終止線程池中正在運(yùn)行的定時(shí)任務(wù)的具體步驟和方法,有需要的朋友跟著學(xué)習(xí)下。2018-05-05Struts2實(shí)現(xiàn)單文件或多文件上傳功能
這篇文章主要為大家詳細(xì)介紹了Struts2實(shí)現(xiàn)單文件或多文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03java WebSocket客戶端斷線重連的實(shí)現(xiàn)方法
在工作中是否會(huì)遇到實(shí)用websocket客戶端連接服務(wù)端的時(shí)候,網(wǎng)絡(luò)波動(dòng),服務(wù)端斷連的情況,本文可以直接使用的斷線重連,感興趣的可以了解一下2021-10-10