网络客户端工具与Java邮件功能实现
立即解锁
发布时间: 2025-08-18 02:37:46 阅读量: 3 订阅数: 11 

# 网络客户端工具与Java邮件功能实现
## 1. 网页链接检查器(LinkChecker)
### 1.1 背景与需求
在维护大型网站时,确保所有超文本链接、图像、小程序等在网站发展和变化过程中保持有效是一项具有挑战性的任务。一个小的更改可能会破坏其他地方的链接,导致用户遇到404错误。因此,需要一个程序来自动检查链接的有效性。
### 1.2 LinkChecker程序基本思路
利用之前创建的读取网页并提取包含URL标签的程序,以一个起始URL为基础,创建一个`GetURLs`对象。若创建成功,则读取URL列表并继续处理。该程序还具备通过简单缩进在图形窗口中显示网站结构的功能。
### 1.3 代码实现
以下是`LinkChecker`程序的代码:
```java
/** A simple HTML Link Checker.
* Need a Properties file to set depth, URLs to check. etc.
* Responses not adequate; need to check at least for 404-type errors!
* When all that is (said and) done, display in a Tree instead of a TextArea.
* Then use Color coding to indicate errors.
*/
public class LinkChecker extends Frame implements Runnable {
protected Thread t = null;
/** The "global" activation flag: set true to halt. */
boolean done = false;
protected Panel p;
/** The textfield for the starting URL.
* Should have a Properties file and a JComboBox instead.
*/
protected TextField textFldURL;
protected Button checkButton;
protected Button killButton;
protected TextArea textWindow;
protected int indent = 0;
protected Map hash = new HashMap( );
public static void main(String[] args) {
LinkChecker lc = new LinkChecker( );
lc.setSize(500, 400);
lc.setLocation(150, 150);
lc.setVisible(true);
if (args.length == 0)
return;
lc.textFldURL.setText(args[0]);
}
public void startChecking( ) {
done = false;
checkButton.setEnabled(false);
killButton.setEnabled(true);
textWindow.setText("");
doCheck( );
}
public void stopChecking( ) {
done = true;
checkButton.setEnabled(true);
killButton.setEnabled(false);
}
/** Construct a LinkChecker */
public LinkChecker( ) {
super("LinkChecker");
addWindowListener(new WindowAdapter( ) {
public void windowClosing(WindowEvent e) {
setVisible(false);
dispose( );
System.exit(0);
}
});
setLayout(new BorderLayout( ));
p = new Panel( );
p.setLayout(new FlowLayout( ));
p.add(new Label("URL"));
p.add(textFldURL = new TextField(40));
p.add(checkButton = new Button("Check URL"));
// Make a single action listener for both the text field (when
// you hit return) and the explicit "Check URL" button.
ActionListener starter = new ActionListener( ) {
public void actionPerformed(ActionEvent e) {
startChecking( );
}
};
textFldURL.addActionListener(starter);
checkButton.addActionListener(starter);
p.add(killButton = new Button("Stop"));
killButton.setEnabled(false); // until startChecking is called.
killButton.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent e) {
if (t == null || !t.isAlive( ))
return;
stopChecking( );
}
});
// Now lay out the main GUI - URL & buttons on top, text larger
add("North", p);
textWindow = new TextArea(80, 40);
add("Center", new JScrollPane(textWindow));
}
public void doCheck( ) {
if (t!=null && t.isAlive( ))
return;
t = new Thread(this);
t.start( );
}
public synchronized void run( ) {
textWindow.setText("");
checkOut(textFldURL.getText( ));
textWindow.append("-- All done --");
}
/** Start checking, given a URL by name.
* Calls checkLink to check each link.
*/
public void checkOut(String rootURLString) {
URL rootURL = null;
GetURLs urlGetter = null;
if (done)
return;
if (rootURLString == null) {
textWindow.append("checkOut(null) isn't very useful");
return;
}
if (hash.get(rootURLString) != null) {
return; // already visited
}
hash.put(rootURLString, Boolean.TRUE);
// Open the root URL for reading
try {
rootURL = new URL(rootURLString);
urlGetter = new GetURLs(rootURL);
} catch (MalformedURLException e) {
textWindow.append("Can't parse " + rootURLString + "\n");
return;
} catch (FileNotFoundException e) {
textWindow.append("Can't open file " + rootURLString + "\n");
return;
} catch (IOException e) {
textWindow.append("openStream " + rootURLString + " " + e + "\n");
return;
}
// If we're still here, the root URL given is OK.
// Next we make up a "directory" URL from it.
String rootURLdirString;
if (rootURLString.endsWith("/") ||
rootURLString.endsWith("\\"))
rootURLdirString = rootURLString;
else {
rootURLdirString = rootURLString.substring(0,
rootURLString.lastIndexOf('/')); // TODO might be \
}
try {
ArrayList urlTags = urlGetter.getURLs( );
Iterator urlIterator = urlTags.iterator( );
while (urlIterator.hasNext( )) {
if (done)
return;
String tag = (String)urlIterator.next( );
System.out.println(tag);
String href = extractHREF(tag);
for (int j=0; j<indent; j++)
textWindow.append("\t");
textWindow.append(href + " -- ");
// Can't really validate these!
if (href.startsWith("mailto:")) {
textWindow.append(href + " -- not checking\n");
continue;
}
if (href.startsWith("..") || href.startsWith("#")) {
textWindow.append(href + " -- not checking\n");
```
0
0
复制全文
相关推荐










