马鞍山网站建设报价,建筑方案设计说明,建设企业网站的价格,三维立体图制作软件app前言#xff1a;示例只是做了一个最最基础的上传csv的示例#xff0c;如果要引用到代码中去#xff0c;还需要根据自己的业务自行添加一些逻辑处理。ReadCsvUtil工具类package com.hanfengyeqiao.gjb.utils;import java.io.*;import java.util.*;/*** csv工具类*/public cla…前言示例只是做了一个最最基础的上传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 getResource(byte[] bate) throws IOException {List allString new ArrayList();MapcallLogInfo ;Listlist new ArrayList();// 获取文件内容list getSource(bate);// 获取文件表头Listtitle 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; icontent 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 ListgetSource(byte[] bate) throws IOException {BufferedReader br null;ByteArrayInputStream fisnull;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 ;ListallString 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 )RestControllerRequestMapping(/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 listReadCsvUtil.getResource(bate);if(list!null){for(Mapm:list){System.out.println(param1:m.get(param1);param2:m.get(param2)。);}}}}}html代码test上传:示例文件运行结果在处理csv文件的时候容易出现编码上的问题小伙伴们写代码的时候要多注意一下以上就是本文的全部内容希望对大家的学习有所帮助也希望大家多多支持脚本之家。