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

上傳:

示例文件

2090fbc8a549055170ab01481654dfc4.png

運行結果

945967e70c0a73a52c70507a408cee2b.png

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

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

如您對本文有疑問或者有任何想說的,請點擊進行留言回復,萬千網友為您解惑!

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542648.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542648.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542648.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

360更新補丁一直提示正在安裝_遠程利用POC公布|CVE20200796:微軟發布SMBv3協議“蠕蟲級”漏洞補丁通告...

更多全球網絡安全資訊盡在邑安全www.eansec.com0x00 事件描述2020年3月11日&#xff0c;360CERT監測到有海外廠家發布安全規則通告&#xff0c;通告中描述了一處微軟SMBv3協議的內存破壞漏洞&#xff0c;編號CVE-2020-0796&#xff0c;并表示該漏洞無需授權驗證即可被遠程利用&…

字符串的回文子序列個數_計算給定字符串中回文子序列的數量

字符串的回文子序列個數Problem statement: 問題陳述&#xff1a; Given a string you have to count the total number of palindromic subsequences in the giving string and print the value. 給定一個字符串&#xff0c;您必須計算給定字符串中回文子序列的總數并打印該值…

Linux-破解rhel7-root密碼

破解7的密碼1.linux16 rd.break2.mount -o remount,rw /sysroot3.chroot /sysroot4.passwd5.touch /.autorelabelexitexit7版本grub菜單加密1.grub2-mkpasswd-pbkdf22.vi /etc/grub.d/40_customset superusers"root"password_pbkdf2 root grub.pbkdf2.sha512.10000.…

適配接口 java_【Java 設計模式】接口型模式--Adapter(適配器)模式

簡介&#xff1a;【Java設計模式】接口型模式–Adapter(適配器)模式Adapter模式的宗旨就是&#xff1a;向客戶提供接口&#xff0c;并使用現有的類所提供的服務&#xff0c;以滿足客戶的需求。 或者說&#xff0c;現在有classA的方法滿足客戶的部分要求&#xff0c;將另一部分需…

deepinu盤制作工具_u盤啟動盤制作工具怎么制作 u盤啟動盤制作工具制作方法【詳細步驟】...

在電腦城很多技術人員都會使用u盤裝系統的方法給用戶電腦安裝系統&#xff0c;他們是怎么操作的呢?其實很簡單&#xff0c;就是通過u盤啟動盤來安裝系統的。而u盤啟動盤是需要用 u盤啟動盤制作工具 來制作的。那么問題又來了&#xff0c;u盤啟動盤制作工具怎么制作呢?下面就給…

openstack私有云_OpenStack-下一代私有云的未來

openstack私有云The OpenStack project is an open source cloud computing platform for all types of clouds, which aims to be simple to implement, massively scalable, and feature rich. Developers and cloud computing technologists from around the world create t…

outlook2010客戶端無法預覽及保存word,excel問題

outlook2010客戶端遇到的EXCEL預覽及保存問題今天遇到了一個這樣的問題&#xff0c;outlook2010打開以后其他的excel都可以打開預覽及保存&#xff0c;這個excel無法預覽既保存&#xff0c;經查是outlook2010預覽及打開的緩存有限制&#xff0c;超過后就無法預覽了&#xff0c;…

python自動化框架pytest pdf_Python 自動化測試框架 unittest 和 pytest 對比

一、用例編寫規則1.unittest提供了test cases、test suites、test fixtures、test runner相關的類,讓測試更加明確、方便、可控。使用unittest編寫用例,必須遵守以下規則:(1)測試文件必須先import unittest(2)測試類必須繼承unittest.TestCase(3)測試方法必須以“test_”開頭(4…

freemarker的測試結果框架_java必背綜合知識點總結(框架篇)

框架篇一、Struts1的運行原理在啟動時通過前端總控制器ActionServlet加載struts-config.xml并進行解析&#xff0c;當用戶在jsp頁面發送請求被struts1的核心控制器ActionServlet接收&#xff0c;ActionServlet在用戶請求時將請求參數放到對應的ActionForm對象中的成員變量中&am…

Java SecurityManager checkPackageDefinition()方法與示例

SecurityManager類的checkPackageDefinition()方法 (SecurityManager Class checkPackageDefinition() method) checkPackageDefinition() method is available in java.lang package. checkPackageDefinition()方法在java.lang包中可用。 We call getProperty("package.d…

java容器詳解_詳解Java 容器(第①篇)——概覽

![](http://img.blog.itpub.net/blog/2020/04/02/9d89d3008962c127.png?x-oss-processstyle/bb)容器主要包括 Collection 和 Map 兩種&#xff0c;Collection 存儲著對象的集合&#xff0c;而 Map 存儲著鍵值對(兩個對象)的映射表。# 一、Collection![](https://upload-images…

python圖形界面庫哪個好_8個必備的Python GUI庫

Python GUI 庫有很多&#xff0c;下面給大家羅列常用的幾種 GUI庫。下面介紹的這些GUI框架&#xff0c;能滿足大部分開發人員的需要&#xff0c;你可以根據自己的需求&#xff0c;選擇合適的GUI庫。1. wxPython wxPython 是一個跨平臺的 GUI 工具集&#xff0c;是 Python 語言的…

為什么在Python中使用string.join(list)而不是list.join(string)?

join() is a string method and while using it the separator string iterates over an arbitrary sequence, forming string representations of each of the elements, inserting itself between the elements. join()是一個字符串方法&#xff0c;使用它時&#xff0c;分隔…

js的client、scroll、offset詳解與兼容性

clientWidth&#xff1a;可視區寬說明&#xff1a;樣式寬padding參考&#xff1a;js的client詳解 scrollTop : 滾動條滾動距離說明&#xff1a;chrome下他會以為滾動條是文檔元素的&#xff0c;所以需要做兼容&#xff1a;var scrollTop document.documentElement.scrollTop |…

88是python語言的整數類型_Python基礎數據類型題

Python基礎數據類型 題 考試時間&#xff1a;三個小時 滿分100分&#xff08;80分以上包含80分及格&#xff09; 1&#xff0c;簡述變量命名規范&#xff08;3分&#xff09;1.必須是字母&#xff0c;數字&#xff0c;下劃線的任意組合。 2.不能是數字開頭 3.不能是python中的關…

[轉載]使用awk進行數字計算,保留指定位小數

對于在Shell中進行數字的計算&#xff0c;其實方法有很多&#xff0c;但是常用的方法都有其弱點&#xff1a; 1、bc bc應該是最常用的Linux中計算器了&#xff0c;簡單方便&#xff0c;支持浮點。 [wangdongcentos715-node1 ~]$ echo 12 |bc 3 [wangdongcentos715-node1 ~]$ ec…

dcom配置_spring cloud 二代架構依賴組件 全配置放送

一 背景介紹先來看一下我們熟悉的第一代 spring cloud 的組件spring cloud 現在已經是一種標準了&#xff0c;各公司可以基于它的編程模型編寫自己的組件 &#xff0c;比如Netflix、阿里巴巴都有自己的一套通過spring cloud 編程模型開發的分布式服務組件 。Spring Cloud 二代組…

olap 多維分析_OLAP(在線分析處理)| OLAP多維數據集和操作

olap 多維分析In the previous article of OLAP, we have seen various applications of OLAP, Various types of OLAP, advantages, and disadvantages of OLAP. In this article, we will learn about the, 在OLAP的上一篇文章中&#xff0c;我們了解了OLAP的各種應用&#x…

dede mysql語句_讓dede運行php代碼和mysql語句

一、dede運行php代碼舉例1&#xff1a;{dede:name runphpyes}$str "hello ";me $str;me . "world";{/dede:name}結果&#xff1a;hello world說明&#xff1a;"name"為任意定義的名字&#xff0c;me 表示當前的值&#xff0c;也就是要輸出最后…

每周一書-2016年8月28日到9月4日獲獎讀者公布

每周一書-2016年8月28日到9月4日獲獎讀者公布 上次送出的《Bootstrap基礎教程》&#xff0c;已經被幸運者收到了。我們先來回顧下《改善C程序代碼的125個建議》活動文章下的精彩留言。 這是一段高屋建瓴的評述&#xff0c;足見作者對C語言的了解和熱愛層度&#xff0c;當然也得…