zoukankan      html  css  js  c++  java
  • 读取CSV文件

    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));
        }
    }
  • 相关阅读:
    POJ 1287 Networking
    2019 牛客多校第六场 D Move
    2019 牛客多校第六场 J Upgrading Technology
    2019 牛客多校第六场 B Shorten IPv6 Address
    POJ 1251 Jungle Roads
    POJ 3268 Silver Cow Party
    POJ 3259 Wormholes
    Codeforces Round #596 (Div. 1, based on Technocup 2020 Elimination Round 2)
    Educational Codeforces Round 75 (Rated for Div. 2)
    【知识点】多项式乘法
  • 原文地址:https://www.cnblogs.com/wangymd/p/15592188.html
Copyright © 2011-2022 走看看