您好,登錄后才能下訂單哦!
怎么在java項(xiàng)目中上傳csv文件?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
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)沒有添加額外參數(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é)果
看完上述內(nèi)容,你們掌握怎么在java項(xiàng)目中上傳csv文件的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。