Java股票数据下载
2016-07-05 05:01
211 查看
package main;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
/**
* <p>Title: RunStock2.java</p>
* <p>Description:定时插入mydql数据库 </p>
* <p>Copyright: Copyright (c) 2013</p>
* <p>Company: zyf</p>
* @author zyf
* @date 2014-8-3
* @version 1.0
*/
public class RunStock {
long starttime;
public void RunStock(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
java.util.Calendar c1=java.util.Calendar.getInstance();
long s = new Date().getTime();
String dates = sdf1.format(new Date());
String dates1 = sdf.format(new Date());
System.out.println(dates1);
String ss = dates + " 09:00:00" ;
System.out.println(ss);
try {
Date sdate = sdf.parse(ss);
long l ;
System.out.println("开始" + sdate.getTime());
System.out.println("结束" + new Date().getTime());
System.out.println(dates);
l = sdate.getTime() - new Date().getTime();
System.out.println("绝对值=" + Math.abs(l));
if(l<0){
starttime = Math.abs(l)+ 12*3600*1000;
}else{
starttime = l;
}
System.out.print(l);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MyTask mt = new MyTask();
mt.run();
mt.Command();
}
public static void main(String[] args){
RunStock rs = new RunStock();
rs.RunStock();
}
}
class MyTask{
public void run(){
List<String> list = null;
try {
list = setStockCode();
getStockTextinfo(list);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean getStockTextinfo(List<String> list) throws Exception {
// TODO Auto-generated method stub
boolean bl = false;
//List<String> list = setStockCode();
Iterator<String> it = list.iterator();
HttpClient client = new DefaultHttpClient();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
System.out.println("----------start:=" + getDate());
while(it.hasNext()){
String str1 = it.next();
String url = "http://hq.sinajs.cn/list=" + str1;
//System.out.println(url);
HttpGet request = new HttpGet(url);
HttpResponse response;
try {
response = client.execute(request);
// 得到结果,进行解释
if (response.getStatusLine().getStatusCode() == 200) {
// 连接成功
String strResponse = EntityUtils.toString(response.getEntity(),
HTTP.UTF_8);// 将响应结果转换为String类型
String str123 = str1 + ',' +strResponse.substring(strResponse.indexOf('"')+1,strResponse.lastIndexOf('"'));
String [] strlength = str123.split(",");
if(strlength.length == 34){
String str_1 = str123 + ',' + sdf1.format(new java.util.Date()) +'\n';
doWriteText(str_1);
}
bl = true;
} else {
// 没有请求成功
System.out.println("请求的连接状态没有ok");
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.abort();
}
System.out.println("----------end:=" + getDate());
return bl;
}
public List<String> setStockCode() throws Exception {
List<String> alist = new ArrayList<String>(); //从文本存到List
File file = new File("D:/zyf_Workspaces/Stockv7/src/main/txt/stock_data.txt");//读文件
//File file = new File("/root/stock/stock_d/stock_data.txt");//读文件
FileInputStream inputstream;
BufferedReader reader = null;
String strtemp = "";
try {
inputstream = new FileInputStream(file);
reader = new BufferedReader(new InputStreamReader(inputstream));
int j = 0;
while((strtemp = reader.readLine()) != null){
alist.add(strtemp);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(reader != null){
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return alist;
}
public String getDate(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
String sdate = sdf.format(new java.util.Date());
return sdate;
}
public boolean doWriteText(String str) throws Exception {
// TODO Auto-generated method stub
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
String sdate = sdf1.format(new java.util.Date());
//String filepath = "/root/stock/stock_d/data/" + sdate + ".dat";
String filepath = "d:/data/" + sdate + ".dat";
File file = new File(filepath);
if(!file.exists())
file.createNewFile();
FileOutputStream out = new FileOutputStream(file,true);
out.write(str.getBytes("GBK"));
out.close();
return true;
}
public void Command(){
try {
String shpath="/root/stock/stock_d/ImpStock_d.sh"; //程序路径
Process process =null;
String command1 = shpath;
process = Runtime.getRuntime().exec(command1);
try {
process.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
/**
* <p>Title: RunStock2.java</p>
* <p>Description:定时插入mydql数据库 </p>
* <p>Copyright: Copyright (c) 2013</p>
* <p>Company: zyf</p>
* @author zyf
* @date 2014-8-3
* @version 1.0
*/
public class RunStock {
long starttime;
public void RunStock(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
java.util.Calendar c1=java.util.Calendar.getInstance();
long s = new Date().getTime();
String dates = sdf1.format(new Date());
String dates1 = sdf.format(new Date());
System.out.println(dates1);
String ss = dates + " 09:00:00" ;
System.out.println(ss);
try {
Date sdate = sdf.parse(ss);
long l ;
System.out.println("开始" + sdate.getTime());
System.out.println("结束" + new Date().getTime());
System.out.println(dates);
l = sdate.getTime() - new Date().getTime();
System.out.println("绝对值=" + Math.abs(l));
if(l<0){
starttime = Math.abs(l)+ 12*3600*1000;
}else{
starttime = l;
}
System.out.print(l);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MyTask mt = new MyTask();
mt.run();
mt.Command();
}
public static void main(String[] args){
RunStock rs = new RunStock();
rs.RunStock();
}
}
class MyTask{
public void run(){
List<String> list = null;
try {
list = setStockCode();
getStockTextinfo(list);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean getStockTextinfo(List<String> list) throws Exception {
// TODO Auto-generated method stub
boolean bl = false;
//List<String> list = setStockCode();
Iterator<String> it = list.iterator();
HttpClient client = new DefaultHttpClient();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
System.out.println("----------start:=" + getDate());
while(it.hasNext()){
String str1 = it.next();
String url = "http://hq.sinajs.cn/list=" + str1;
//System.out.println(url);
HttpGet request = new HttpGet(url);
HttpResponse response;
try {
response = client.execute(request);
// 得到结果,进行解释
if (response.getStatusLine().getStatusCode() == 200) {
// 连接成功
String strResponse = EntityUtils.toString(response.getEntity(),
HTTP.UTF_8);// 将响应结果转换为String类型
String str123 = str1 + ',' +strResponse.substring(strResponse.indexOf('"')+1,strResponse.lastIndexOf('"'));
String [] strlength = str123.split(",");
if(strlength.length == 34){
String str_1 = str123 + ',' + sdf1.format(new java.util.Date()) +'\n';
doWriteText(str_1);
}
bl = true;
} else {
// 没有请求成功
System.out.println("请求的连接状态没有ok");
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.abort();
}
System.out.println("----------end:=" + getDate());
return bl;
}
public List<String> setStockCode() throws Exception {
List<String> alist = new ArrayList<String>(); //从文本存到List
File file = new File("D:/zyf_Workspaces/Stockv7/src/main/txt/stock_data.txt");//读文件
//File file = new File("/root/stock/stock_d/stock_data.txt");//读文件
FileInputStream inputstream;
BufferedReader reader = null;
String strtemp = "";
try {
inputstream = new FileInputStream(file);
reader = new BufferedReader(new InputStreamReader(inputstream));
int j = 0;
while((strtemp = reader.readLine()) != null){
alist.add(strtemp);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(reader != null){
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return alist;
}
public String getDate(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
String sdate = sdf.format(new java.util.Date());
return sdate;
}
public boolean doWriteText(String str) throws Exception {
// TODO Auto-generated method stub
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
String sdate = sdf1.format(new java.util.Date());
//String filepath = "/root/stock/stock_d/data/" + sdate + ".dat";
String filepath = "d:/data/" + sdate + ".dat";
File file = new File(filepath);
if(!file.exists())
file.createNewFile();
FileOutputStream out = new FileOutputStream(file,true);
out.write(str.getBytes("GBK"));
out.close();
return true;
}
public void Command(){
try {
String shpath="/root/stock/stock_d/ImpStock_d.sh"; //程序路径
Process process =null;
String command1 = shpath;
process = Runtime.getRuntime().exec(command1);
try {
process.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相关文章推荐
- Struts2系列:(27)i18n-5 i18n tag
- Struts2系列:(26) 国际化(四)使用UI标签的key属性获取国际化资源
- Struts2系列:(25) 国际化(三)TextProvider如何搜索ResouceBundle的位置
- 运用eclipse
- spring+hibernate 两种整合方式配置文件
- Java集合---HashMap
- 开发第一个Java程序 HelloWorld!
- Java环境安装
- Struts2系列:(24) 国际化(二)案例-快速入门
- Java 理论与实践: 正确使用 Volatile 变量
- Eclipse Java注释模板设置详解/版权声明及编码风格的注释
- Ubuntu 16.04 64位 Eclipse 背景色 豆沙绿
- Java ssh的实现具体步骤
- Spring(三):IoC容器装配Bean(xml配置方式和注解方式)
- [10] MQTT,mosquitto,Eclipse Paho---MQTT消息格式之Publication(消息发布)之QoS==0
- RxJava学习(一)
- Spring中 @Autowired注解与@Resource注解的区别
- Struts2系列:(23) 国际化(一)介绍
- java中的this,super关键字
- java多线程下载