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.mzph.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;当然也得…