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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java上传csv错误信息_java处理csv文件上传示例详解

發布時間:2024/7/23 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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();

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 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 ;

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 = "")

@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(Mapm:list){

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

}

}

}

}

}

html代碼:

test上傳:

示例文件

運行結果

在處理csv文件的時候容易出現編碼上的問題,小伙伴們寫代碼的時候要多注意一下!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

總結

以上是生活随笔為你收集整理的java上传csv错误信息_java处理csv文件上传示例详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。