av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

java處理csv文件上傳示例詳解

瀏覽:60日期:2022-08-18 18:12:10

前言:示例只是做了一個最最基礎的上傳csv的示例,如果要引用到代碼中去,還需要根據自己的業務自行添加一些邏輯處理。

ReadCsvUtil工具類

package com.hanfengyeqiao.gjb.utils;import java.io.*;import java.util.*; /** * csv工具類 */public class ReadCsvUtil { private static final String FIX='uFEFF'; /** * 獲取csv文件內容 * @return 對象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(); // 獲取文件內容 list = getSource(bate); // 獲取文件表頭 List<String> title = Arrays.asList(list.get(0).split(',')); String customerName = title.get(0).trim(); String customerNo = title.get(1).trim(); // 頭部會帶有'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); // 循環內容 for(int i = 0; i<list.size();i++){ List<String> content = Arrays.asList(list.get(i).split(',')); // 當沒有添加額外參數時 if(content!=null){callLogInfo = new HashMap();callLogInfo.put('param1',content.get(0));callLogInfo.put('param2',content.get(1));allString.add(callLogInfo); } } return allString; } /** * 讀文件數據 */ 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 { //讀取到的內容給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>

示例文件

java處理csv文件上傳示例詳解

運行結果

java處理csv文件上傳示例詳解

在處理csv文件的時候容易出現編碼上的問題,小伙伴們寫代碼的時候要多注意一下!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 午夜影院普通用户体验区 | 黄色高清视频 | 色精品视频 | 97精品超碰一区二区三区 | 欧美中文字幕一区二区三区 | 成人性视频免费网站 | 国产精品视频入口 | 久久99久久98精品免观看软件 | 欧美色综合网 | 丁香六月激情 | 精品亚洲一区二区 | 神马久久av | 毛片.com | 欧美激情精品久久久久久 | 嫩草视频网 | 欧美视频一区二区三区 | 亚洲欧洲日韩精品 中文字幕 | 激情综合五月 | 天天弄天天操 | 日韩中文字幕一区二区三区 | 国产免费拔擦拔擦8x高清 | 国产精品国产三级国产aⅴ中文 | 综合久久av| 国产情侣啪啪 | 在线国产一区 | 五月网婷婷 | 欧美一级毛片免费观看 | 日韩中文一区二区三区 | 欧美精品三区 | 搞黄视频免费看 | 极品在线 | 黄色综合 | 国产欧美日韩在线 | 欧美日韩专区 | 亚洲国产成人久久久 | 国产高清精品在线 | 一级大片免费 | 日韩视频免费 | 日韩国产中文字幕 | 在线观看成人小视频 | 麻豆精品久久久 |