这两天接到一个任务是让从一个地址接口中取出响应的数据然后转储为文件,在转储文件的同时,再创建另一个文件让两个文件中的内容同步,也就是说“文件1”是从接口中拿到的数据,而同时建立的“文件2”中的数据是从“文件1”中同步过来的,,,光请求一次数据是不行的,它的数据是会变化的,所以需要定时每段时间请求一次,“文件1”中每次都是最新数据,相应的“文件2”中的数据也会在每次“文件1”更新之后同步一次,,这里用的是spring里面自带的Task轮询的功能,具体的注解是@Scheduled;
配置文件之类的就不贴上来了,直接放一个job的工程代码
package com.cssc.proxy.api.jobs;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
@Service
@Component
public class HifleetJob {
public final static String base_url = "自己的URL地址";
public final static String default_token = "自己所需要的验证口令";
@Scheduled(cron = "0 */1 * * * ?")//每分钟轮询一次
public void getData() throws Exception {
System.out.println("执行取数据");
String url = "/position/gettraffic/token";
Map<String, String> params = new HashMap<String, String>();
params.put("bbox", "120.5,30.5,121,31");
String resp = HttpPut(url+"?usertoken="+default_token+"&bbox=120.5,30.5,121,31");
saveDataToFile(resp);
append("hifleet.txt","log.txt");
}
public static String HttpPut(String url) throws Exception {
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(base_url+url);
client.executeMethod(method);
InputStream in = method.getResponseBodyAsStream();
in = new BufferedInputStream(in);
Reader r = new InputStreamReader(in,"utf-8");
int c;
StringBuffer buffer = new StringBuffer();
try {
while ((c = r.read()) != -1)
buffer.append((char) c);
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
method.releaseConnection();
return buffer.toString();
}
/**
* //保存数据
*
*/
public static void saveDataToFile(String data) {
System.out.println("导入开始");
File file = new File("hifleet.txt");
if(file.exists()) {
System.out.println("已存在hifleet.txt文件,将更新文件内容");
file.delete();
}
if(!file.exists()) {
try {
file.createNewFile();
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file), "GB2312");
BufferedWriter bw = new BufferedWriter(osw);
bw.write(data);
System.out.println("导入结束");
bw.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
public static void append(String sourcePath,String destPath){
File af = new File(sourcePath);
File bf = new File(destPath);
FileInputStream is = null;
FileOutputStream os = null;
if(!bf.exists()){
try {
bf.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
is = new FileInputStream(af);
os = new FileOutputStream(bf,true);
byte b[] = new byte[1024];
int len;
try {
len = is.read(b);
while (len != -1) {
os.write(b, 0, len);
len = is.read(b);
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
try {
if(is != null) is.close();
if(os != null) os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
本人是一名新手实习生,如果有错误的地方欢迎指正,开头说的有可能有些麻烦请见谅,