怎么在java项目中上传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<Map<String,Object>> getResource(byte[] bate) throws IOException {
List<Map<String,Object>> allString = new ArrayList();
Map<String,Object> callLogInfo ;
List<String> list = new ArrayList();
// 获取文件内容
list = getSource(bate);
// 获取文件表头
List<String> 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.size();i++){
List<String> 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<String> 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<String> 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<Map<String,Object>> list=ReadCsvUtil.getResource(bate);
if(list!=null){
for(Map<String,Object> m:list){
System.out.println("param1:"+m.get("param1")+";param2:"+m.get("param2")+"。");
}
}
}
}
}
html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body>
<form action="http://localhost:8088/admin/test/upload" method="post" enctype="multipart/form-data">
上传:<input type="file" name="upfile"/>
<input type="submit" value="提交"/>
</form>
</body>
<script type="text/javascript">
</script>
</html>
示例文件
运行结果
看完上述内容,你们掌握怎么在java项目中上传csv文件的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。