如何编写Java代码实现高效处理并上传CSV文件至服务器的示例教程?

2026-04-19 15:241阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计659个文字,预计阅读时间需要3分钟。

如何编写Java代码实现高效处理并上传CSV文件至服务器的示例教程?

前言:以下是一个最基础的CSV上传示例,若要应用于实际代码中,还需根据自身业务需求进行一些逻辑处理。

javapackage com.hanfengyeqiao.gjb.utils;

import java.io.*;import java.util.*;

public class ReadCsvUtil { public static List readCsv(String filePath) throws IOException { List dataList=new ArrayList(); BufferedReader br=new BufferedReader(new FileReader(filePath)); String line; while ((line=br.readLine()) !=null) { String[] values=line.split(,); Map dataMap=new HashMap(); for (int i=0; i

前言:示例只是做了一个最最基础的上传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.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文件至服务器的示例教程?

运行结果

在处理csv文件的时候容易出现编码上的问题,小伙伴们写代码的时候要多注意一下!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计659个文字,预计阅读时间需要3分钟。

如何编写Java代码实现高效处理并上传CSV文件至服务器的示例教程?

前言:以下是一个最基础的CSV上传示例,若要应用于实际代码中,还需根据自身业务需求进行一些逻辑处理。

javapackage com.hanfengyeqiao.gjb.utils;

import java.io.*;import java.util.*;

public class ReadCsvUtil { public static List readCsv(String filePath) throws IOException { List dataList=new ArrayList(); BufferedReader br=new BufferedReader(new FileReader(filePath)); String line; while ((line=br.readLine()) !=null) { String[] values=line.split(,); Map dataMap=new HashMap(); for (int i=0; i

前言:示例只是做了一个最最基础的上传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.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文件至服务器的示例教程?

运行结果

在处理csv文件的时候容易出现编码上的问题,小伙伴们写代码的时候要多注意一下!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。