java 将接口 数据写入到csv文件中
1 定时去调接口数据 写入
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¶m2=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;
}
写csv用文件流就可以。
https://blog.csdn.net/qq_20500811/article/details/91515123
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632