[java]代码库
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import com.enterprisedt.net.ftp.FTPClient;
import com.enterprisedt.net.ftp.FTPConnectMode;
import com.enterprisedt.net.ftp.FTPMessageCollector;
import com.enterprisedt.net.ftp.FTPTransferType;
import com.enterprisedt.util.debug.Logger;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URL;
/**
*
* @author Icer
*/
public class FtpClient {
private FTPClient ftp = null;
public FTPClient connFtpServer(String host, int port, String username, String password) {
ftp = new FTPClient();
try {
ftp.setRemoteHost(host);
ftp.setRemotePort(port);
FTPMessageCollector listener = new FTPMessageCollector();
ftp.setMessageListener(listener);
System.out.println("Connecting");
ftp.connect();
System.out.println("Logging in");
ftp.login(username, password);
System.out.println("Setting up passive,ASCII transfers");
ftp.setConnectMode(FTPConnectMode.PASV);
ftp.setType(FTPTransferType.ASCII);
} catch (Exception e) {
e.printStackTrace();
}
return ftp;
}
public void upload(String localfile, URL url) {
String host = url.getHost();
int port = url.getPort();
File filePath = new File(url.getPath());
String directory = filePath.getParent().substring(1);
String filename = filePath.getName();
try {
ftp.chdir(directory);
ftp.put(new FileInputStream(localfile), filename);
} catch (Exception e) {
e.printStackTrace();
System.out.println("upload failed");
}
}
public void upload(String localpath, String filename) {
try {
if(ftp.connected()) {
ftp.put(localpath, filename);
ftp.dir();
System.out.println("hello");
} else {
System.out.println("wrong");
}
} catch (Exception e) {
System.out.println("upload failed");
e.printStackTrace();
}
}
public void DownloadFile(String localpath, String filename) {
try {
ftp.chdir("");
String[] files = ftp.dir("", true);
for (int i = 0; i < files.length; i++) {
System.out.println(files[i]);
ftp.get(new FileOutputStream(localpath + filename), filename);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("upload failed");
}
}
public void disConn() {
try {
ftp.quit();
} catch (Exception e) {
System.out.println("disconnection failed");
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
}
}