java实现ftp的几种方式(第3方包)
最近在做ssh
,sftp。顺便总结一下java实现ftp的几种方式。
1、sun.net
import
sun.net.ftp.*;
import
sun.net.*;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.ByteArrayOutputStream;
import
java.util.ArrayList;
import
java.util.StringTokenizer;
public class
FtpUpfile {
private FtpClient ftpclient;
private String ipAddress;
private int ipPort;
private String userName;
private String PassWord;
public FtpUpfile(String ip, int port, String username, String
password) throws
Exception {
ipAddress = new String(ip);
ipPort = port;
ftpclient = new FtpClient(ipAddress, ipPort);
//ftpclient = new FtpClient(ipAddress);
userName = new String(username);
PassWord = new String(password);
}
public FtpUpfile(String ip, String username, String password)
throws
Exception {
ipAddress = new String(ip);
ipPort = 21;
ftpclient = new FtpClient(ipAddress, ipPort);
//ftpclient = new FtpClient(ipAddress);
userName = new String(username);
PassWord = new String(password);
}
public void login() throws Exception {
ftpclient.login(userName, PassWord);
}
public void logout() throws Exception {
//用ftpclient.closeServer()断开FTP出错时用下更语句退出
ftpclient.sendServer("QUIT\r\n");
int reply = ftpclient.readServerResponse(); //取得服务器的返回信息
}
public void buildList(String pathList) throws Exception {
ftpclient.ascii();
StringTokenizer s = new StringTokenizer(pathList, "/"); //sign
int count = s.countTokens();
String pathName = "";
while (s.hasMoreElements()) {
pathName = pathName + "/" + (String) s.nextElement();
try {
ftpclient.sendServer("XMKD " + pathName + "\r\n");
} catch (Exception e) {
e = null;
}
int reply = ftpclient.readServerResponse();
}
ftpclient.binary();
}
public ArrayList fileNames(String fullPath) throws Exception {
ftpclient.ascii(); //注意,使用字符模式
TelnetInputStream list = ftpclient.nameList(fullPath);
byte[] names = new byte[2048];
int bufsize = 0;
bufsize = list.read(names, 0, names.length); //从流中读取
list.close();
ArrayList namesList = new ArrayList();
int i = 0;
int j = 0;
while (i < bufsize ) {
//char bc = (char) names;
//System.out.println(i + " " +
bc + " : " + (int) names);
//i = i + 1;
if (names[i] == 10) { //字符模式为10,二进制模式为13
//文件名在数据中开始下标为j,i-j为文件名的长度,文件名在数据中的结束下标为i-1
//System.out.write(names, j, i - j);
//System.out.println(j +
" " + i +
" " + (i - j));
String tempName = new String(names, j, i - j);
namesList.add(tempName);
//System.out.println(temp);
// 处理代码处
//j = i + 2; //上一次位置二进制模式
j = i + 1; //上一次位置字符模式
}
i = i + 1;
}
return namesList;
}
public void upFile(String source, String destination) throws
Exception {
buildList(destination.substring(0,
destination.lastIndexOf("/")));
ftpclient.binary(); //此行代码必须放在buildList之后
TelnetOutputStream ftpOut = ftpclient.put(destination);
TelnetInputStream ftpIn = new TelnetInputStream(new
FileInputStream(source), true);
byte[] buf = new byte[204800];
int bufsize = 0;
while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) {
ftpOut.write(buf, 0, bufsize);
}
ftpIn.close();
ftpOut.close();
}
public void upFile(byte[] sourceData, String destination) throws
Exception {
buildList(destination.substring(0,
destination.lastIndexOf("/")));
ftpclient.binary(); //此行代码必须放在buildList之后
TelnetOutputStream ftpOut = ftpclient.put(destination);
ftpOut.write(sourceData, 0, sourceData.length);
// ftpOut.flush();
ftpOut.close();
}