首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

程序运行出错,该如何处理

2012-05-01 
程序运行出错有以下程序,功能是实现利用百度快照,将百度文库中的txt文件下载到本地。现在的问题是,程序运行

程序运行出错
有以下程序,功能是实现利用百度快照,将百度文库中的txt文件下载到本地。现在的问题是,程序运行后就死机。请高手帮我看看是怎么回事,谢谢了……
package DownloadWindow;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.net.*;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.*;
import org.htmlparser.util.NodeList;

public class DownloadWindow extends JFrame implements ActionListener{
static JTextArea jta;
Label label=new Label("URL地址:");
JTextField jtf;
JButton jbDownLoad;
JButton jbChooseFolder;
JPanel jp;
static File directoryToSave;
public DownloadWindow(){
jta=new JTextArea();
jtf=new JTextField(50);
jbDownLoad=new JButton("开始下载");
jbChooseFolder=new JButton("选择目标文件夹");
jbDownLoad.addActionListener(this);
jbChooseFolder.addActionListener(this);
jp=new JPanel();
jp.setLayout(new FlowLayout());
jp.add(label);
jp.add(jtf);
jp.add(jbDownLoad);
jp.add(jbChooseFolder);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(jta,BorderLayout.CENTER);
this.getContentPane().add(jp,BorderLayout.SOUTH);
setTitle("文件下载器");
setSize(1000,600);
setVisible(true);
jta.append("你必须首先选择目标文件夹,然后再输入URL地址,最后点击下载按钮开始下载"+"\n");
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jbDownLoad){
Set <String> txtToDownload=parserBaiDu(jtf.getText());
beginToDownload(txtToDownload);
}
else if(e.getSource()==jbChooseFolder){
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("选择保存文件的目标文件夹");
chooser.setCurrentDirectory(new File("F:\\Test文件夹\\"));
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.showOpenDialog(this);
directoryToSave=chooser.getSelectedFile();
if(directoryToSave!=null){
jta.append("你选择的文件保存路径为:"+directoryToSave.toString()+"\n");
}
}
}
public static Set<String> parserBaiDu(String url){
Set <String> links=new HashSet<String>();
StringBuffer strTemp=new StringBuffer();
while(true){
try {
URL urlToParser=new URL(url);
URLConnection urlConnection=urlToParser.openConnection();
InputStream is=urlConnection.getInputStream();
byte[] bt=new byte[1024];
while((is.read(bt,0,1023)!=-1)){
strTemp.append(bt);
}
}
catch(Exception e){
e.printStackTrace();
}
int j=0;
NodeList nodes=null;
try{
Parser parser = new Parser(url);
NodeFilter tagNameFilter = new TagNameFilter("a");
NodeFilter attrbFilter = new HasAttributeFilter("class","m");
NodeFilter andFilter = new AndFilter(tagNameFilter,attrbFilter);
nodes = parser.extractAllNodesThatMatch(andFilter);
}
catch(Exception e){
e.printStackTrace();
}
for(int i=0;i<nodes.size();i++){
Node node = (Node)nodes.elementAt(i);
String nodeTxt = node.getText();
int startIndex = nodeTxt.indexOf("\"");
int endIndex = nodeTxt.indexOf("\"",startIndex+1);
String link = nodeTxt.substring(startIndex+1, endIndex);
links.add(link);
}
if((strTemp.indexOf(new String("提示:限于网页篇幅,部分结果未予显示")))==-1){
url=url.replace("pn="+10*j, "pn="+10*(++j));
}
else{
break;
}
}
return links;
}
public static boolean beginToDownload(Set<String> hrefs){
int i=1;
for(Iterator iter=hrefs.iterator();iter.hasNext();){


String content="";
String nextHref=(String)iter.next();
File fileToCreate=new File(directoryToSave.toString()+"\\"+i+".txt");
try {
FileWriter writer=new FileWriter(fileToCreate);
Parser parser = new Parser(nextHref);
NodeFilter tagFilter = new TagNameFilter("div");
NodeFilter idFilter = new HasAttributeFilter("id","textContainer");
NodeFilter andFilter = new AndFilter(tagFilter,idFilter);
NodeList nodes = parser.extractAllNodesThatMatch(andFilter);
content = ((Node)nodes.elementAt(0)).toPlainTextString();
System.out.println(content);
writer.write(content);
writer.flush();
jta.invalidate();
} catch (Exception e) {
e.printStackTrace();
return false;
}
jta.append("文件"+directoryToSave.toString()+"\\"+i+".txt下载成功!"+"\n");
i++;
}
return true; 
}
public static void main(String []args){
DownloadWindow window=new DownloadWindow();
window.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

[解决办法]
是不是太大了?
[解决办法]
运行程序,打开任务栏管理器,看下CPU使用率是否是100% 可能出现死循环

热点排行