wxh努力中 2021-05-20 16:32 采纳率: 53.8%
浏览 409

java 调用接口 将接口返回数据写入到csv文件中

java 将接口 数据写入到csv文件中 
1 定时去调接口数据  写入  

  • 写回答

3条回答 默认 最新

  • 小P聊技术 2021-05-20 16:46
    关注

    1 定时任务

    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class TimerDemo {
      public static String getCurrentTime() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
      }
    
      public static void main(String[] args) throws InterruptedException {
        System.out.println("main start:"+getCurrentTime());
        startTimer();
        Thread.sleep(1000*5); //休眠5秒
        System.out.println("main  end:"+getCurrentTime());
      }
    
      public static void startTimer(){
        TimerTask task = new TimerTask() {
          @Override
          public void run() {
            System.out.println("task  run:"+getCurrentTime());
          }
        };
        Timer timer = new Timer();
        timer.schedule(task, 0);
      }
    }
    
    

    2 获取接口数据

     <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.12</version>
            </dependency>
     
            <!--fafastjson-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.71</version>
            </dependency>
    /**
         * 发送get请求
         * @param url  请求路径
         * @param params  请求参数,格式为key-value
         * @param header  请求头
         * @return
         */
        public String getData(String url, Map<String, String> params, Map<String, String> header) {
            String responeStr = null;
            //获取Http客户端
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //返回模型
            CloseableHttpResponse response = null;
     
            try {
                //获取url的请求路径
                URIBuilder uriBuilder = new URIBuilder(url);
                //获取参数的迭代器
                Iterator paramsIterator = params.entrySet().iterator();
     
                //迭代参数,将参数挂在url后
                while(paramsIterator.hasNext()) {
                    Map.Entry entry = (Map.Entry)paramsIterator.next();
                    uriBuilder.addParameter((String)entry.getKey(), (String)entry.getValue());
                }
     
                //创建httpget请求
                HttpGet httpGet = new HttpGet(uriBuilder.build());
                //判断请求头是否为空
                if (header != null) {
                    Iterator headerIterator = header.entrySet().iterator();
     
                    //封装请求头
                    while(headerIterator.hasNext()) {
                        Map.Entry headerEntrty = (Map.Entry)headerIterator.next();
                        httpGet.setHeader((String)headerEntrty.getKey(), (String)headerEntrty.getValue());
                    }
                }
     
                //发送请求
                response = httpClient.execute(httpGet);
                //获取并判断请求
                if (response != null && response.getStatusLine().getStatusCode() == 200) {
                    HttpEntity httpEntity = response.getEntity();
                    responeStr = this.entityToString(httpEntity);
                }
     
                String resultStr = responeStr;
                return resultStr;
            } catch (URISyntaxException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    httpClient.close();
                    if (response != null) {
                        response.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
     
            }
     
            return null;
        }
     
     
     
     
     
    /**
         * 解析HttpEntity
         * @param entity
         * @return
         * @throws IOException
         */
        private String entityToString(HttpEntity entity) throws IOException {
            String result = null;
            InputStreamReader inputStreamReader = null;
            try {
                if (entity != null) {
                    long contentLength = entity.getContentLength();
                    if (contentLength != -1L && contentLength < 2048L) {
                        result = EntityUtils.toString(entity, "UTF-8");
                    } else {
                        inputStreamReader = new InputStreamReader(entity.getContent(), "UTF-8");
                        CharArrayBuffer charArrayBuffer = new CharArrayBuffer(2048);
                        char[] chars = new char[1024];
     
                        int index;
                        while((index = inputStreamReader.read(chars)) != -1) {
                            charArrayBuffer.append(chars, 0, index);
                        }
     
                        result = charArrayBuffer.toString();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                inputStreamReader.close();
            }
     
            return result;
        }
    
     /**
         * 发起post请求
         * @param URL  请求地址
         * @param params  参数 格式为map
         * @param header 请求头,可以为null
         * @return
         */
        public String postData(String URL, Map<String, String> params, Map<String, String> header) {
            String respStr = null;
            //获取Http客户端
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //创建post请求
            HttpPost httpPost = new HttpPost(URL);
            //存储参数的BasicNameValuePair集合
            ArrayList list = new ArrayList();
     
            //获得参数的迭代器
            Iterator iterator = params.entrySet().iterator();
     
            //迭代参数
            while(iterator.hasNext()) {
                Map.Entry entry = (Map.Entry)iterator.next();
                //BasicNameValuePair通常是用来封装post请求中的参数名称和值;
                list.add(new BasicNameValuePair((String)entry.getKey(), (String)entry.getValue()));
            }
     
            //请求模型
            CloseableHttpResponse response = null;
     
            try {
                /*两个键值对,被UrlEncodedFormEntity实例编码后变为如下内容:
                param1=value1&param2=value2
                然后将请求参数放进Entity中*/
                httpPost.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
                //如果请求头不为null
                if (header != null) {
                    Iterator headerIterator = header.entrySet().iterator();
     
                    //就遍历请求头中的参数
                    while(headerIterator.hasNext()) {
                        Map.Entry headerEntry = (Map.Entry)headerIterator.next();
                        httpPost.setHeader((String)headerEntry.getKey(), (String)headerEntry.getValue());
                    }
                }
     
                //发送post请求
                response = httpClient.execute(httpPost);
                //如果请求不为空并且响应code为200
                if (response != null && response.getStatusLine().getStatusCode() == 200) {
                    //获取请求模型返回的实体
                    HttpEntity responseEntity = response.getEntity();
                    respStr = this.entityToString(responseEntity);
                }
     
                String resultStr = respStr;
                return resultStr;
            } catch (UnsupportedEncodingException var23) {
                var23.printStackTrace();
            } catch (ClientProtocolException var24) {
                var24.printStackTrace();
            } catch (IOException var25) {
                var25.printStackTrace();
            } finally {
                try {
                    httpClient.close();
                    if (response != null) {
                        response.close();
                    }
                } catch (IOException var22) {
                    var22.printStackTrace();
                }
     
            }
     
            return null;
        }
     
     
     
    /**
         * 解析HttpEntity
         * @param entity
         * @return
         * @throws IOException
         */
        private String entityToString(HttpEntity entity) throws IOException {
            String result = null;
            InputStreamReader inputStreamReader = null;
            try {
                if (entity != null) {
                    long contentLength = entity.getContentLength();
                    if (contentLength != -1L && contentLength < 2048L) {
                        result = EntityUtils.toString(entity, "UTF-8");
                    } else {
                        inputStreamReader = new InputStreamReader(entity.getContent(), "UTF-8");
                        CharArrayBuffer charArrayBuffer = new CharArrayBuffer(2048);
                        char[] chars = new char[1024];
     
                        int index;
                        while((index = inputStreamReader.read(chars)) != -1) {
                            charArrayBuffer.append(chars, 0, index);
                        }
     
                        result = charArrayBuffer.toString();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                inputStreamReader.close();
            }
     
            return result;
        }

    3 写入csv

    /**
         * 生成为CVS文件
         *
         * @param exportData 源数据List
         * @param map        csv文件的列表头map
         * @param outPutPath 文件路径
         * @param fileName   文件名称
         * @return
         */
        public File createCSVFile(List<List<String>> exportData, String outPutPath, String fileName) {
            File csvFile = null;
            BufferedWriter csvFileOutputStream = null;
            try {
                File file = new File(outPutPath);
                if (!file.exists()) {
                    if (file.mkdirs()) {
                        log.info("创建成功");
                    } else {
                        log.error("创建失败");
                    }
                }
                //定义文件名格式并创建
                csvFile = File.createTempFile(fileName, ".csv", new File(outPutPath));
                csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), StandardCharsets.UTF_8), 1024);
                for (List<String> exportDatum : exportData) {
                    writeRow(exportDatum, csvFileOutputStream);
                    csvFileOutputStream.newLine();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (csvFileOutputStream != null) {
                        csvFileOutputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return csvFile;
        }
       
       /**
         * 写一行数据
         *
         * @param row       数据列表
         * @param csvWriter
         * @throws IOException
         */
        private void writeRow(List<String> row, BufferedWriter csvWriter) throws IOException {
            int i=0;
            for (String data : row) {
                csvWriter.write(DelQuota(data));
                if (i!=row.size()-1){
                    csvWriter.write(",");
                }
                i++;
            }
       }
    
       /**
         * 剔除特殊字符
         *
         * @param str       数据
         */
    public String DelQuota(String str) {
            String result = str;
            String[] strQuota = {"~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "`", ";", "'", ",", ".", "/", ":", "/,", "<", ">", "?"};
            for (int i = 0; i < strQuota.length; i++) {
                if (result.indexOf(strQuota[i]) > -1)
                    result = result.replace(strQuota[i], "");
            }
            return result;
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 微信聊天记录备份到电脑提示成功了,但还是没同步到电脑微信
  • ¥15 python怎么在已有视频文件后添加新帧
  • ¥20 虚幻UE引擎如何让多个同一个蓝图的NPC执行一样的动画,
  • ¥15 fluent里模拟降膜反应的UDF编写
  • ¥15 MYSQL 多表拼接link
  • ¥15 关于某款2.13寸墨水屏的问题
  • ¥15 obsidian的中文层级自动编号
  • ¥15 同一个网口一个电脑连接有网,另一个电脑连接没网
  • ¥15 神经网络模型一直不能上GPU
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!