日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java上传csv文件上传_java处理csv文件上传示例详解

發(fā)布時(shí)間:2025/3/11 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java上传csv文件上传_java处理csv文件上传示例详解 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言:示例只是做了一個(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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。