package com.csv; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; import java.util.List; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.compress.utils.Lists; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.web.client.RestTemplate; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; /** * * @Description 读取文件 * @Author wangymd * @Date 2021-11-23 10:46:07 */ public class ReadCSVFile { @Autowired private static RestTemplate restTemplate; public static void main(String[] args) { String fileName = "D:\\dev\\tempFile\\143_20211119102853.csv"; //读取excel List<JSONObject> buildCsv = buildCsv(fileName); if(CollectionUtils.isNotEmpty(buildCsv)) { for (JSONObject json : buildCsv) { invokeHttp(json); } } } public static List<JSONObject> buildCsv(String fileName) { try { //BufferedReader reader = new BufferedReader(new FileReader("D:\\dev\\tempFile\\143_20211119102853.csv"));//换成你的文件名 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"GBK"));//换成你的文件名UTF-8 reader.readLine();//第一行信息,为标题信息,不用,如果需要,注释掉 String line = null; List<JSONObject> listJSONObject = Lists.newArrayList(); while((line=reader.readLine())!=null){ String item[] = line.split(",");//CSV格式文件为逗号分隔符文件,这里根据逗号切分 String author = item[0]; String taskId = item[1]; String imgUrl = item[3]; JSONObject json = new JSONObject(); json.put("author", author); json.put("taskId", taskId); json.put("threshold", 0.2); json.put("imgUrl", "https://m.360buyimg.com/" + imgUrl); listJSONObject.add(json); } return listJSONObject; }catch (Exception e) { e.printStackTrace(); } return null; } private static void invokeHttp(JSONObject json) { restTemplate = new RestTemplate(); HttpHeaders headersUpload = new HttpHeaders(); headersUpload.setContentType(MediaType.APPLICATION_JSON); JSONObject upload = restTemplate.postForObject("http://data.com/imgUrlCheck", json, JSONObject.class); System.out.println(JSON.toJSONString(upload)); } }