Java多线程下载
来源:互联网 发布:mac os mavericks 编辑:程序博客网 时间:2024/05/22 12:52
同时下载多个文件,不过单文件没有多线程的下载并且没有断点续传功能,继续完善: view plaincopy to clipboardprint?
package com.util.file;
public class Files {
/***
* 获取应用程序的根目录
* @return 应用程序根目录
*/
public static String getSysPath(){
return System.getProperty("user.dir");
}
}
package com.util.file;
public class Files {
/***
* 获取应用程序的根目录
* @return 应用程序根目录
*/
public static String getSysPath(){
return System.getProperty("user.dir");
}
}view plaincopy to clipboardprint?
view plaincopy to clipboardprint?
<PRE class=csharp name="code">package com.core.crawl;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.core.http.Http;
public class WebSpider implements Runnable{
private Http http = new Http();
private String webAddress = "";
private String destFile = "";
public void setWebAddress(String webAddress){
this.webAddress = webAddress;
}
public void setDestFile (String destFile){
this.destFile = destFile;
}
public boolean download() throws IOException, InterruptedException {
HttpURLConnection httpConn = null;
try {
URL url = new URL(webAddress);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14");
InputStream in = httpConn.getInputStream();
String fileType = http.fileType(httpConn.getContentType());
System.out.println(fileType);
FileOutputStream out = new FileOutputStream(new File(destFile + fileType));
int chByte = in.read();
while (chByte != -1) {
out.write(chByte);
//System.out.println(chByte);
chByte = in.read();
}
} catch (Exception ex) {
System.out.println(ex.toString());
} finally {
httpConn.disconnect();
}
return true;
}
public void run() {
try {
//System.out.println(Thread.currentThread().getName());
download();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
</PRE>
view plaincopy to clipboardprint?package com.core.crawl; importjava.io.File; import java.io.FileOutputStream; importjava.io.IOException; import java.io.InputStream; importjava.net.HttpURLConnection; import java.net.URL; importcom.core.http.Http; public class WebSpiderimplementsRunnable{ private Http http = newHttp(); privateString webAddress = ""; private StringdestFile =""; public void setWebAddress(StringwebAddress){ this.webAddress = webAddress; } public voidsetDestFile (String destFile){ this.destFile =destFile; } public boolean download() throwsIOException,InterruptedException { HttpURLConnection httpConn=null; try { URL url =newURL(webAddress); httpConn =(HttpURLConnection)url.openConnection(); httpConn.setRequestMethod("GET"); httpConn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows; U;Windows NT 5.1; zh-CN; rv:1.8.1.14)Gecko/20080404Firefox/2.0.0.14"); InputStream in=httpConn.getInputStream(); String fileType=http.fileType(httpConn.getContentType()); System.out.println(fileType); FileOutputStream out = newFileOutputStream(new File(destFile +fileType)); int chByte =in.read(); while (chByte !=-1) { out.write(chByte); //System.out.println(chByte); chByte = in.read(); } } catch (Exception ex){ System.out.println(ex.toString()); } finally{ httpConn.disconnect(); } return true; } publicvoid run() { try{ //System.out.println(Thread.currentThread().getName()); download(); } catch (IOException e) { e.printStackTrace(); } catch(InterruptedException e) { e.printStackTrace(); } } } package com.core.crawl;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.core.http.Http;
public class WebSpider implements Runnable{
private Http http = new Http();
private String webAddress = "";
private String destFile = "";
public void setWebAddress(String webAddress){
this.webAddress = webAddress;
}
public void setDestFile (String destFile){
this.destFile = destFile;
}
public boolean download() throws IOException, InterruptedException {
HttpURLConnection httpConn = null;
try {
URL url = new URL(webAddress);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows;U;Windows NT 5.1; zh-CN; rv:1.8.1.14) Gecko/20080404Firefox/2.0.0.14");
InputStream in = httpConn.getInputStream();
String fileType = http.fileType(httpConn.getContentType());
System.out.println(fileType);
FileOutputStream out = new FileOutputStream(new File(destFile + fileType));
int chByte = in.read();
while (chByte != -1) {
out.write(chByte);
//System.out.println(chByte);
chByte = in.read();
}
} catch (Exception ex) {
System.out.println(ex.toString());
} finally {
httpConn.disconnect();
}
return true;
}
public void run() {
try {
//System.out.println(Thread.currentThread().getName());
download();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
view plaincopy to clipboardprint?
<PRE class=csharp name="code">package com.core.crawl;
import java.io.IOException;
import com.util.file.Files;
public class Crawl {
/**
* @param args
* @throws IOException
* @throws InterruptedException
*/
public static void main(String[] args) throws IOException, InterruptedException {
long begin = System.currentTimeMillis();
WebSpider spider2 = new WebSpider();
WebSpider spider1 = new WebSpider();
spider1.setWebAddress("http://www.163.com");
spider1.setDestFile(Files.getSysPath() + "/"+"spider1.");
spider2.setWebAddress("http://blog.csdn.net/longronglin");
spider2.setDestFile(Files.getSysPath() + "/"+"spider2.");
Thread t1 = new Thread(spider1);
Thread t2 = new Thread(spider2);
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("the end");
System.out.println(System.currentTimeMillis() - begin);
}
}</PRE>
<PRE class=csharp name="code"> </PRE>
<PRE class=csharp name="code">测试通过:</PRE>
<PRE class=csharp name="code"></PRE>
- java 多线程下载
- java 多线程断点下载
- Java多线程下载
- java多线程下载
- java多线程下载
- Java多线程下载
- java多线程下载
- Java多线程下载
- java多线程下载程序
- Java多线程下载文件
- java多线程下载文件
- java实现多线程下载
- Java多线程下载
- Java多线程下载文件
- java多线程下载
- Java多线程下载文件
- Java多线程下载文件
- Java多线程下载代码
- 解决mysql "GOT error 127 from table handler" 错误
- 利用GridView完成增删改及排序(不用数据源)
- 代理网络环境下使用HttpURLConnection
- 使用jquery过滤上传文件的类型和限制文件的大小
- 比较子
- Java多线程下载
- java.net.HttpURLConnection的使用
- 通过java.net.Socket 类抓取网页内容
- VS2010 正式版序列号
- S3C6410的WinCE系统烧录
- 在java中"模拟" XMLHttpRequest
- 电动小车的电机驱动及控制
- 如何安装window media play 11
- MyEclipse 5.5.1 GA 安装 jbpm 插件