java上传csv文件上传_java处理csv文件上传示例详解
前言:示例只是做了一個(gè)最最基礎(chǔ)的上傳csv的示例,如果要引用到代碼中去,還需要根據(jù)自己的業(yè)務(wù)自行添加一些邏輯處理。
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> getresource(byte[] bate) throws ioexception {
list> allstring = new arraylist();
map callloginfo ;
list list = new arraylist();
// 獲取文件內(nèi)容
list = getsource(bate);
// 獲取文件表頭
list 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 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 getsource(byte[] bate) throws ioexception {
bufferedreader br = null;
bytearrayinputstream fis=null;
inputstreamreader isr = null;
try {
fis = new bytearrayinputstream(bate);
//指定以u(píng)tf-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 {
//讀取到的內(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> list=readcsvutil.getresource(bate);
if(list!=null){
for(map m:list){
system.out.println("param1:"+m.get("param1")+";param2:"+m.get("param2")+"。");
}
}
}
}
}
html代碼:
test上傳:
示例文件
運(yùn)行結(jié)果
在處理csv文件的時(shí)候容易出現(xiàn)編碼上的問題,小伙伴們寫代碼的時(shí)候要多注意一下!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持萬(wàn)仟網(wǎng)。
如您對(duì)本文有疑問或者有任何想說(shuō)的,請(qǐng)點(diǎn)擊進(jìn)行留言回復(fù),萬(wàn)千網(wǎng)友為您解惑!
總結(jié)
以上是生活随笔為你收集整理的java上传csv文件上传_java处理csv文件上传示例详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 论文公式编号右对齐_论文不会写?最详细的
- 下一篇: 全连接条件随机场_条件随机场CRF简介