2014/03/28 从指定网址下载数据并保存
2014-03-28 21:23
281 查看
从指定网址下载数据并保存,用两个线程分别下载
代码的思路是创建两个线程,分别将网址传递给ur方法,调用ur方法从指定网址获得数据流,ur将数据信息交给fil和input方法完成文件才创建和数据的保存
再调用jx类中的方法实现文件的解析和保存(jx还没有实现)
import java.io.*;
import java.net.*;
public class Xz {
public static Thread x1;
public static Thread x2;
public String fileName;
public static void main(String[] args) {
Thread x1 = new Thread(){
String strUrl = "http://192.168.0.101:7000/hfs";
int numb = 1;
public void run(){
Ur(strUrl , numb);//调用ur方法
}
}; x1.start();
Thread x2 = new Thread(){
String strUrl = "http://192.168.0.101:7000/ww.txt";
int numb = 2;
public void run(){
Ur(strUrl , numb);//调用ur方法
}
}; x2.start();
//Jx.qwe(); //调用此类读取文件然后解析xml和json,暂时还没有实现
}
public static void Ur(String strUrl,int numb){
try{
URL url = new URL(strUrl); //创建URL对象,创建shuj连接到指定网址
HttpURLConnection shuj = (HttpURLConnection)url.openConnection();
InputStream inStream = shuj.getInputStream();//通过输入流获取文件数据
byte[] btImg = input(inStream);//得到文件的数据
if(numb == 1){
String fileName = "hfs.exe";
fil(fileName,numb,btImg);}//创建文件
else{
String fileName = "ww.txt";
fil(fileName,numb,btImg);
}
}catch (Exception e) {
e.printStackTrace();
}
}
public static void fil(String fileName ,int numb , byte[] btImg){
try {
File file = new File("D:\\" + fileName);//指定文件路径和名称
FileOutputStream fops = new FileOutputStream(file);
fops.write(btImg);
fops.flush();
fops.close();
System.out.println("文件"+numb+"已经写入到d盘");
} catch (Exception e) {
e.printStackTrace();
}
}
public static byte[] input(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len=inStream.read(buffer)) != -1 ){
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}
}
代码的思路是创建两个线程,分别将网址传递给ur方法,调用ur方法从指定网址获得数据流,ur将数据信息交给fil和input方法完成文件才创建和数据的保存
再调用jx类中的方法实现文件的解析和保存(jx还没有实现)
import java.io.*;
import java.net.*;
public class Xz {
public static Thread x1;
public static Thread x2;
public String fileName;
public static void main(String[] args) {
Thread x1 = new Thread(){
String strUrl = "http://192.168.0.101:7000/hfs";
int numb = 1;
public void run(){
Ur(strUrl , numb);//调用ur方法
}
}; x1.start();
Thread x2 = new Thread(){
String strUrl = "http://192.168.0.101:7000/ww.txt";
int numb = 2;
public void run(){
Ur(strUrl , numb);//调用ur方法
}
}; x2.start();
//Jx.qwe(); //调用此类读取文件然后解析xml和json,暂时还没有实现
}
public static void Ur(String strUrl,int numb){
try{
URL url = new URL(strUrl); //创建URL对象,创建shuj连接到指定网址
HttpURLConnection shuj = (HttpURLConnection)url.openConnection();
InputStream inStream = shuj.getInputStream();//通过输入流获取文件数据
byte[] btImg = input(inStream);//得到文件的数据
if(numb == 1){
String fileName = "hfs.exe";
fil(fileName,numb,btImg);}//创建文件
else{
String fileName = "ww.txt";
fil(fileName,numb,btImg);
}
}catch (Exception e) {
e.printStackTrace();
}
}
public static void fil(String fileName ,int numb , byte[] btImg){
try {
File file = new File("D:\\" + fileName);//指定文件路径和名称
FileOutputStream fops = new FileOutputStream(file);
fops.write(btImg);
fops.flush();
fops.close();
System.out.println("文件"+numb+"已经写入到d盘");
} catch (Exception e) {
e.printStackTrace();
}
}
public static byte[] input(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len=inStream.read(buffer)) != -1 ){
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}
}
相关文章推荐
- Tip:如何指定浏览器下载并保存动态生成的数据时对话框里的默认文件名
- php怎么将数据写入指定url的txt中,然后并返回保存地址提供下载?
- python3爬取指定百度贴吧页面并保存成本地文档(批量爬取贴吧页面数据)
- 下载图片,并保存到指定大小和清晰度
- 访问局域网内指定电脑,下载文件,读取数据
- 【matlab】读生成的指定路径下的数据文件并保存到矩阵中
- 20110327-8.4.2-用ofstream打开文件,要保存文件中已经存在的数据,必须显式的指定app模式打开
- GIS数据下载网址
- jsp页面导出用jxl方式导出数据到excel表格并下载保存(第二种直接用java类实现)
- 基于Java语言完成:使用URL下载指定的文件保存到指定的文件夹中
- python实现下载指定网址所有图片的方法
- python实现下载指定网址所有图片的方法
- AFNetworking的图片、视频下载并保存到指定的app的Caches目录下
- nginx中文件下载指定保存文件名的配置方法
- 下载报表数据保存到服务器
- python从ftp下载数据保存实例
- PB 利用timer()函数实现定时将数据窗口以excel文档保存至指定地方
- 如何在jsp页面下载后台服务器返回的数据并保存为txt格式
- 遥感影像数据下载网址
- 转载:Ajax 实现在WebForm中拖动控件并即时在服务端保存状态数据 (Asp.net 2.0)(示例代码下载)