728x90
반응형
//NetComponents-1.3.8 (http://www.savarese.org/downloads/NetComponents/) 라이브러리 필요
import java.io.IOException;
import java.io.PrintWriter;
import com.oroinc.net.ftp.FTP;
import com.oroinc.net.ftp.FTPClient;
import com.oroinc.net.ftp.FTPConnectionClosedException;
import com.oroinc.net.ftp.FTPReply;
public class FtpManager {
protected static FTPClient ftp;
protected static boolean FTPConnect(String server, String username, String password){
ftp = new FTPClient();
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
try {
int reply;
ftp.connect(server);
System.out.println("Connected to " + server + ".");
// After connection attempt, you should check the reply code to verify
// success.
reply = ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.out.println("FTP server refused connection.");
return false;
}
//Login FTP
if(!ftp.login(username, password)) {
ftp.logout();
return false;
}
System.out.println("Remote system is " + ftp.getSystemName());
//Setting BINARY method
ftp.setFileType(FTP.BINARY_FILE_TYPE);
}catch(FTPConnectionClosedException e) {
System.out.println("Server closed connection.");
e.printStackTrace();
return false;
} catch(IOException e) {
if(ftp.isConnected()) {
try {
ftp.disconnect();
} catch(IOException f) {
//do nothing
}
}
System.out.println("Could not connect to server.");
e.printStackTrace();
return false;
}
return true;
}
protected static boolean FTPConnect(String server, int port, String username, String password){
ftp = new FTPClient();
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
try {
int reply;
ftp.connect(server, port);
System.out.println("Connected to " + server + ":" + port);
// After connection attempt, you should check the reply code to verify
// success.
reply = ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.out.println("FTP server refused connection.");
return false;
}
//Login FTP
if(!ftp.login(username, password)) {
ftp.logout();
return false;
}
System.out.println("Remote system is " + ftp.getSystemName());
//Setting BINARY method
ftp.setFileType(FTP.BINARY_FILE_TYPE);
}catch(FTPConnectionClosedException e) {
System.out.println("Server closed connection.");
e.printStackTrace();
return false;
} catch(IOException e) {
if(ftp.isConnected()) {
try {
ftp.disconnect();
} catch(IOException f) {
//do nothing
}
}
System.out.println("Could not connect to server.");
e.printStackTrace();
return false;
}
return true;
}
public static void FTPDisconnect(){
if(ftp.isConnected()) {
try {
ftp.disconnect();
System.out.println("Disconnect to server.");
} catch(IOException f) {
// do nothing
System.out.println("Disconnect Error. : " + f.getMessage());
f.printStackTrace();
}
}
}
}
728x90
반응형