import java.util.Vector;
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.IOException;
import sun.net.ftp.FtpClient;
import java.io.InputStream;
import java.awt.Color;
import java.io.BufferedReader;
import java.util.HashMap;
public class ConnectTest {
Vector threadVector = new Vector();
Thread t1;
StringBuffer strAllInfo;//得到所有的错误信息
ConnectTest(Vector vector, boolean bUseMail, boolean bUseSound,
boolean bUseMsg, String mailADD, String mailPWD) {
for (int i = 0; i < vector.size(); i++) {
t1 = new Thread(new ThreadFtpConn((String) vector.get(i), bUseMail,
bUseSound, bUseMsg, mailADD,
mailPWD));
t1.start();
threadVector.addElement(t1);
}
}
public void stop() {
Thread t1;
for (int j = 0; j < threadVector.size(); j++) {
t1 = (Thread) threadVector.get(j);
t1.stop();
}
System.out.println("Ftp线程已停止");
}
public static void main(String[] args) {
// ConnectTest connecttest = new ConnectTest();ghfghfg
Vector vector = new Vector();
vector.addElement("10.2.0.4:21:5");
vector.addElement("10.1.9.6:21:4");
vector.addElement("10.1.9.100:21:5");
vector.addElement("10.1.9.4:21:7");
ConnectTest CT = new ConnectTest(vector, true, true, true, "aa@", "PWD");
}
}
class ThreadFtpConn implements Runnable {
PopFrameDemo ftPopFrame = new PopFrameDemo(); //提示框
//static StringBuffer strInfo;
private String strFtpIpPort = "";
private String strFtpIp = "";
private int intFtpPort = 0;
private int intFtpSleep = 0;
private boolean msgAlarm = false;
private boolean mailAlarm = false;
private boolean soundAlarm = false;
private String mailADD = "";
private String mailPWD = "";
ThreadFtpConn(String strFtpIpPort, boolean bUseMail, boolean bUseSound,
boolean bUseMsg, String mailADD, String mailPWD) {
this.strFtpIpPort = strFtpIpPort;
this.setFtpIp(strFtpIpPort);
this.setFtpPort(strFtpIpPort);
this.setFtpSleep(strFtpIpPort);
this.mailAlarm = bUseMail;
this.msgAlarm = bUseMsg;
this.soundAlarm = bUseSound;
this.mailADD = mailADD;
this.mailPWD = mailPWD;
System.out.println(mailAlarm);
System.out.println(msgAlarm);
System.out.println(soundAlarm);
System.out.println(mailADD);
System.out.println(mailPWD);
}
/*
*
*
*/
private void setFtpIp(String strFtpIpPort) {
int intFirstPosition = strFtpIpPort.indexOf(":");
String strFtpIp = strFtpIpPort.substring(0, intFirstPosition);
this.strFtpIp = strFtpIp;
}
/*
*
*
*/
private void setFtpPort(String strFtpIpPort) {
int intFirstPosition = strFtpIpPort.indexOf(":");
int intLastPosition = strFtpIpPort.lastIndexOf(":");
int intFtpPort = Integer.parseInt(strFtpIpPort.substring(
intFirstPosition + 1,
intLastPosition));
this.intFtpPort = intFtpPort;
}
/*
*
*
*/
private void setFtpSleep(String strFtpIpPort) {
int intLastPosition = strFtpIpPort.lastIndexOf(":");
String strFtpSleep = strFtpIpPort.substring(intLastPosition + 1);
int intFtpSleep = Integer.parseInt(strFtpSleep);
this.intFtpSleep = intFtpSleep;
}
/*
*
*
*/
private boolean IsConnect(String ip, int port) {
String strInfo="";
try {
Socket socket = new Socket(ip, port);
return true;
} catch (UnknownHostException e) {
System.out.println("错误的IP地址");
} catch (IOException e) {
strInfo="不能通过端口 " + port + " 与主机 " + ip + " 进行FTP连接";
System.out.println(strInfo);
}
return false;
}
private boolean IsFtpConnect(String ip) {
FtpClient ftpclient = new FtpClient();
try {
ftpclient.openServer(ip);
ftpclient.login("anonymous", "anonymous");
InputStream is = ftpclient.list();
StringBuffer info = new StringBuffer();
int ch;
while ((ch = is.read()) >= 0) {
info.append((char) ch);
}
//System.out.println(new String(info));
is.close();
return true;
} catch (IOException e) {
System.out.println();
System.out.println("主机 " + ip + " FTP服务没有开启或者出现异常");
}
return false;
}
public void run() {
while (true) {
if (this.IsConnect(strFtpIp, intFtpPort)) {
if (this.IsFtpConnect(strFtpIp)) {
System.out.println("ftp " + strFtpIp + " 正常运行");
} else {
System.out.println("ftp " + strFtpIp + " no connect");
}
} else {
// System.out.println("connect error!");
}
try {
Thread.sleep(intFtpSleep * 1000);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
}
问题:我想用strAllInfo保存所有线程的strInfo的值,该怎么做..
求求各位了.帮忙看看该怎么做.实在想不出办法了..谢谢啦.