java上传csv_java处理csv文件上传示例

前言:示例只是做了一个最最基础的上传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();

Map callLogInfo ;

List list = new ArrayList();

// 获取文件内容

list = getSource(bate);

// 获取文件表头

List 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 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 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 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> list=ReadCsvUtil.getResource(bate);

if(list!=null){

for(Map m:list){

System.out.println("param1:"+m.get("param1")+";param2:"+m.get("param2")+"。");

}

}

}

}

}

html代码:

test

上传:

示例文件

bbc06dfbdac7dd6a262510e4fca7f031.png

运行结果:

70994da6997391d0cbbbc46dab0c50b1.png

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值