该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
public class AllThread extends Thread {
public String content = "";
public String url = "";
public void run() {
System.out.println(url);
content = getContents(url);
}
public AllThread(String the_url) {
url = the_url;
}
private static String getContents(String urlName) {
try {
StringBuffer sb = new StringBuffer();
URL url = new URL(urlName);
URLConnection u = url.openConnection();
InputStream is = u.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "gbk");
int c;
while ((c = isr.read()) != -1) {
sb.append((char) c);
}
is.close();
isr.close();
return sb.toString();
} catch (IOException ex) {
return ex.getMessage();
}
}
public static void main(String args[]) {
//TODO自动生成方法存根
//String[] dir = {"http://www.baidu.com ", "http://www.yisou.com "}; File[] dir = new File("//192.168.18.232/heritrix/www.duote.com/softin").listFiles();//提取softin中所有的html文件
AllThread[] threads = new AllThread[dir.length];
//循环输出文件名
for (int i = 0; i < dir.length; i++) {
threads[i] = new AllThread(dir[i].getName());
threads[i].start();
}
//循环输出文件内容
for (int i = 0; i < dir.length; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace(); }
System.out.println(threads[i].content);
}
}
}
输出结果:31283.html3108.html53809.html22770.html42448.html11075.html22426.htmlno protocol: 2253.htmlno protocol: 19455.htmlno protocol: 26932.htmlno protocol: 21136.html…………………………(读取文件名成功,读取内容却失败了TwT)