import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
public class Alexa implements Runnable { public ArrayList s = new ArrayList(); public static void main(String[] args) throws IOException {
Alexa alexa = new Alexa();
alexa.readFile("c:\\sss.txt");
Thread t = new Thread(alexa);
t.start();
}
public void run() { String a = "";
for (int i = 0; i < s.size(); i++) {
a = (String) s.get(i);
try {
saveBinaryFile(a);
System.out.println("i=" + i);
System.out.println("a=" + a);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} public void saveBinaryFile(String url1) throws IOException { BufferedInputStream in = null;
OutputStream out = null;
try { URL url = new URL(url1);
URLConnection conn = url.openConnection(); in = new BufferedInputStream(conn.getInputStream()); out = new FileOutputStream(new File("c://tmp//"
+ System.currentTimeMillis()));
int n = 0;
byte[] bytes = new byte[1024]; while ((n = in.read(bytes)) != -1) {
out.write(bytes, 0, n);
}
} catch (Exception e) {
} finally {
try {
in.close();
out.close();
} catch (Exception ioe) {
ioe.getStackTrace();
}
} } public void readFile(String path) {
String fileline;
ArrayList domains = new ArrayList();
try {
BufferedReader d = new BufferedReader(new FileReader(path)); while ((fileline = d.readLine()) != null) {
domains.add(fileline);
System.out.println(fileline);
} } catch (Exception e) {
System.out.println("readFile.Error: " + e.getMessage() + " "
+ e.getClass());
e.printStackTrace();
} finally {
try { } catch (Exception e) { }
}
s = domains;
}}
c:\\sss.txt 放了一些htm的连接,我想将这些页面下载下来,想用多线程,但是其中saveBinaryFile(a);不执行,但是
System.out.println("i=" + i);
System.out.println("a=" + a);却打印出来了,请问怎么回事?请指教,谢谢

解决方案 »

  1.   

    你没把异常打出来看看吧:
    c:\tmp\1175136790484 (系统找不到指定的路径。)你的目录不存在!
    加上下面代码:File dir = new File("c://tmp//");
    if (!dir.exists()){
        dir.mkdirs();
    }

    out = new FileOutputStream(new File("c://tmp//"+ System.currentTimeMillis()));
    之前
      

  2.   

    } catch (Exception e) {
    } finally {}
    这样只捕获异常不处理异常,你看不到异常状态的
      

  3.   

    楼上说的对,现在问题是 有700个连接,但是只下载了一部分,会报错Connection timed out: connect,是不是因为同时打开很多连接,服务器堵塞了,现在想10个连接一块下载,下载完后,再接着下载
      

  4.   

    700多线程同时跑。
    不妨你试试线程池管理,每次同时跑10-20个先程就可以了:
    ThreadPool thePool = new DefaultThreadPool(10);
    thePool.invokeLater(
        new Thread() {
            public void run() {
                .....
            }
        }还有一种办法:
    MultiThreadedHttpConnectionManager manager=new MultiThreadedHttpConnectionManager();
    manager.getParams().setConnectionTimeout(20000);
    manager.getParams().setSoTimeout(10000);
    manager.getParams().setDefaultMaxConnectionsPerHost(20);
    manager.getParams().setMaxTotalConnections(50);
    HttpClient client = new HttpClient(manager);
    String url="......";
    PostMethod postMethod = new PostMethod(url);
    int statusCode = client.executeMethod(postMethod);
    InputStream is = postMethod.getResponseBodyAsStream();
    InputStreamReader isr = new InputStreamReader(is, "gb2312");
    BufferedReader in = new BufferedReader(isr);
    String inputLine = in.readLine();
    StringBuffer resultBuffer = new StringBuffer();
    while (inputLine != null) {
        resultBuffer.append(inputLine);
        resultBuffer.append("\n");    inputLine = in.readLine();
    }String result = resultBuffer.toString();
    ......