因此,这是一个与FTP服务器联系的相对基本的程序,并记录存在的文件。它使用GUI来启动和启动进程。但是,在Netbeans之外(作为独立的.jar或Eclipse),一旦按下开始按钮(内部类启动),它就会崩溃。我无法理解为什么会发生这种情况所以非常感谢任何帮助。
public class GUI {
private JFrame frame;
private JPanel jPanel;
private FTPSearch demo;
public GUI() {
initialise();
}
public void initialise() {
//set nimbus look and feel
try {
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
frame = new JFrame();
jPanel = new JPanel();
jPanel.setPreferredSize(new Dimension(500, 500));
JButton jButton = new JButton("Start");
jButton.addActionListener(new Start());
jPanel.add(jButton);
frame.setContentPane(jPanel);
frame.setTitle("FTP Communicator");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
demo = new FTPSearch();
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
try {
GUI window = new GUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
});
}
class Start implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
EventQueue.invokeLater(() -> {
try {
demo.searchAndLog();
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
}
然后是FTP类:
public class FTPSearch {
String server;
int port;
String user;
String password;
FTPClient Client;
public FTPSearch() {
server = "ftp.pagesuite.com";
port = 21;
user = "tim";
password = "access28";
Client = new FTPClient();
}
public void searchAndLog() {
try {
Client.connect(server, port);
int reply = Client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
System.out.println("Connect fail");
return;
}
boolean ableToConnect = Client.login(user, password);
if (!ableToConnect) {
System.out.println("Login Fail");
return;
}
String[] files2 = Client.listNames();
//String dir2 = ftpClient.printWorkingDirectory();
//System.out.println("directory is: " + dir2);
printNames(files2);
logNames(files2);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (Client.isConnected()) {
Client.logout();
Client.disconnect();
System.out.println("logged out");
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
private static void printNames(String files[]) {
if (files != null && files.length > 0) {
for (String aFile : files) {
System.out.println(aFile);
}
}
}
private static void logNames(String files[]) {
if (files != null && files.length > 0) {
for (String aFile : files) {
writeToLog(aFile);
}
}
}
private static void showServerReply(FTPClient ftpClient) {
String[] replies = ftpClient.getReplyStrings();
if (replies != null && replies.length > 0) {
for (String reply : replies) {
System.out.println("SERVER: " + reply);
}
}
}
public static void writeToLog(String log) {
File f = new File("LogFile.txt");
boolean exists = f.exists();
if (!exists) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileWriter fWriter;
try {
fWriter = new FileWriter(f, true);
BufferedWriter out = new BufferedWriter(fWriter);
out.write(log + "\n");
out.newLine();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
所以这里似乎失败了:
System.out.println("attempt to retrieve names");
String[] files2 = Client.listNames();
System.out.println("names retrieved");
它将打印第一个语句,但永远不会到达第二个打印。它必须在第二行代码上失败,不知道为什么......