java什么时候用到多线程 Java多线程下载
Java多线程下载
同时下载多个文件 不过单文件没有多线程的下载并且没有断点续传功能 继续完善:
view plaincopy to clipboardprint? package util file;
public class Files {
/*** * 获取应用程序的根目录 * @return 应用程序根目录 */ public static String getSysPath(){ return System getProperty( user dir ); }
}
package 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 re crawl;
import java io File; import java io FileOutputStream; import java io IOException; import java io InputStream; import HttpURLConnection; import URL;
import re Http;
public class WebSpider implements Runnable{
private 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 Conn = null;
try { URL url = new URL(webAddress);
Conn = (HttpURLConnection) url openConnection(); ( GET ); ( User Agent Mozilla/ (Windows; U; Windows NT ; zh CN; rv: ) Gecko/ Firefox/ ); InputStream in = (); String fileType = fileType(()); System out println(fileType); FileOutputStream out = new FileOutputStream(new File(destFile + fileType)); int chByte = in read(); while (chByte != ) { out write(chByte); //System out println(chByte); chByte = in read(); } } catch (Exception ex) { System out println(ex toString()); } finally { (); } 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 re crawl; import java io File; import java io FileOutputStream; import java io IOException; import java io InputStream; import HttpURLConnection; import URL; import re Http; public class WebSpider implements Runnable{ private 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 Conn = null; try { URL url = new URL(webAddress); Conn = (HttpURLConnection) url openConnection(); ( GET ); ( User Agent Mozilla/ (Windows; U; Windows NT ; zh CN; rv: ) Gecko/ Firefox/ ); InputStream in = (); String fileType = fileType(()); System out println(fileType); FileOutputStream out = new FileOutputStream(new File(destFile + fileType)); int chByte = in read(); while (chByte != ) { out write(chByte); //System out println(chByte); chByte = in read(); } } catch (Exception ex) { System out println(ex toString()); } finally { (); } return true; } public void run() { try { //System out println(Thread currentThread() getName()); download(); } catch (IOException e) { e printStackTrace(); } catch (InterruptedException e) { e printStackTrace(); } } } package re crawl;
import java io File; import java io FileOutputStream; import java io IOException; import java io InputStream; import HttpURLConnection; import URL;
import re Http;
public class WebSpider implements Runnable{
private 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 Conn = null;
try { URL url = new URL(webAddress);
Conn = (HttpURLConnection) url openConnection(); ( GET ); ( User Agent Mozilla/ (Windows; U; Windows NT ; zh CN; rv: ) Gecko/ Firefox/ ); InputStream in = (); String fileType = fileType(()); System out println(fileType); FileOutputStream out = new FileOutputStream(new File(destFile + fileType)); int chByte = in read(); while (chByte != ) { out write(chByte); //System out println(chByte); chByte = in read(); } } catch (Exception ex) { System out println(ex toString()); } finally { (); } 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 re crawl;
import java io IOException;
import 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 spider = new WebSpider(); WebSpider spider = new WebSpider(); spider setWebAddress( ); spider setDestFile(Files getSysPath() + / + spider );
spider setWebAddress( ); spider setDestFile(Files getSysPath() + / + spider );
Thread t = new Thread(spider ); Thread t = new Thread(spider ); t start(); t start();
t join(); t join();
System out println( the end ); System out println(System currentTimeMillis() begin); }
lishixinzhi/Article/program/Java/gj/201311/27577