packagecom.xxx.ftp.client;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStream;importjava.io.OutputStreamWriter;importjava.net.Socket;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.List;importjava.util.StringTokenizer;importcom.xxx.ftp.model.ConnectInfo;importcom.xxx.ftp.model.FtpServerInfo;public classFtpFileTransfer {/*** 文件转移
*
*@paramsource
* 源ftp文件目录
*@paramdest
* 目标ftp文件目录
*@throwsIOException*/
public void transfer(FtpServerInfo source, FtpServerInfo dest) throwsIOException {//源ftp目录下的文件列表
sendCommand(source.getConnect(), "CWD " +source.getDir());
sendCommand(source.getConnect(),"PASV");
String response=readNewMessage(source.getConnect());
String[] ipPort=getIPPort(response);
String ip= ipPort[0];int port = Integer.valueOf(ipPort[1]);
Socket dataSocket= newSocket(ip, port);
sendCommand(source.getConnect(),"LIST");
BufferedReader br= new BufferedReader(newInputStreamReader(dataSocket.getInputStream()));//读取源服务器的目录下所有文件名称
List fileNameList = new ArrayList();
String s= "";while ((s = br.readLine()) != null) {
String fileInfo= new String(s.getBytes("ISO-8859-1"), "utf-8");
String[] arr= fileInfo.split("\\s+");
String fileName= arr[arr.length - 1];
fileNameList.add(fileName);
System.out.println(fileName);
}if (fileNameList.size() > 0) {for (Iterator iterator =fileNameList.iterator(); iterator.hasNext();) {//根据文件名获取文件流
String fileName =(String) iterator.next();
sendCommand(source.getConnect(),"PASV");
response=readNewMessage(source.getConnect());
ipPort=getIPPort(response);
ip= ipPort[0];
port= Integer.valueOf(ipPort[1]);
Socket sourceDataSocket= newSocket(ip, port);
sendCommand(source.getConnect(),"RETR " + source.getDir() + "/" +fileName);
InputStream inputStream=sourceDataSocket.getInputStream();//文件流通过socket传输至目标ftp服务器文件目录下(输入流->输出流)
sendCommand(dest.getConnect(), "PASV");
response=readNewMessage(dest.getConnect());
ipPort=getIPPort(response);
ip= ipPort[0];
port= Integer.valueOf(ipPort[1]);
Socket destDataSocket= newSocket(ip, port);
sendCommand(dest.getConnect(),"STOR " + dest.getDir() + "/" +fileName);
OutputStream outputStream=destDataSocket.getOutputStream();intoffset;byte[] bytes = new byte[1024];while ((offset = inputStream.read(bytes)) != -1) {
outputStream.write(bytes,0, offset);
}
inputStream.close();
outputStream.close();
dataSocket.close();
}
}
System.out.println("transfer success!");
}/*** ftp服务器连接
*
*@paramftpServerInfo
* ftp服务器信息 包含用户名 密码
*@return*@throwsIOException*/
public ConnectInfo connect(FtpServerInfo ftpServerInfo) throwsIOException {
Socket socket= newSocket(ftpServerInfo.getHost(), ftpServerInfo.getPort());
BufferedReader reader= new BufferedReader(newInputStreamReader(socket.getInputStream()));
BufferedWriter writer= new BufferedWriter(newOutputStreamWriter(socket.getOutputStream()));
ConnectInfo connectInfo= newConnectInfo();
connectInfo.setSocket(socket);
connectInfo.setWriter(writer);
connectInfo.setReader(reader);
sendCommand(connectInfo,"USER " +ftpServerInfo.getUser());
sendCommand(connectInfo,"PASS " +ftpServerInfo.getPass());returnconnectInfo;
}/*** 断开ftp服务器连接
*
*@paramconnectInfo
*@throwsIOException*/
public void disconnect(ConnectInfo connectInfo) throwsIOException {if (connectInfo.getSocket() == null || !connectInfo.getSocket().isConnected()) {return;
}
connectInfo.getSocket().close();
}/*** 发送ftp命令
*
*@paramconnectInfo
* ftp服务器连接信息
*@paramcommand
* ftp命令
*@throwsIOException*/
private void sendCommand(ConnectInfo connectInfo, String command) throwsIOException {if (command == null || command == "") {return;
}
connectInfo.getWriter().write(command+ "\r\n");
connectInfo.getWriter().flush();
}/*** 根据连接读取ftp信息
*
*@paramconnect
*@return*@throwsIOException*/
private String readNewMessage(ConnectInfo connect) throwsIOException {
String response= null;while (true) {
response=connect.getReader().readLine();if (response == null || response.length() == 0) {return null;
}
String rexp= "Entering Passive Mode";if(response.contains(rexp)) {break;
}
}returnresponse;
}/*** 获取ip和端口
*
*@paramresponse
*@return*@throwsIOException*/
private String[] getIPPort(String response) throwsIOException {
String[] ret= new String[2];
String ip= null;int port = -1;int opening = response.indexOf('(');int closing = response.indexOf(')', opening + 1);if (closing > 0) {
String dataLink= response.substring(opening + 1, closing);
StringTokenizer tokenizer= new StringTokenizer(dataLink, ",");try{
ip= tokenizer.nextToken() + "." + tokenizer.nextToken() + "." + tokenizer.nextToken() + "."
+tokenizer.nextToken();
port= Integer.parseInt(tokenizer.nextToken()) * 256 +Integer.parseInt(tokenizer.nextToken());
}catch(Exception e) {throw new IOException("SimpleFTP received bad data link information: " +response);
}
}
ret[0] =ip;
ret[1] =String.valueOf(port);returnret;
}public static void main(String[] args) throwsIOException {
FtpFileTransfer ft= newFtpFileTransfer();//源ftp服务器
FtpServerInfo source = newFtpServerInfo();
source.setHost("192.168.11.129");
source.setUser("haoxr");
source.setPass("123456");
source.setDir("/home/haoxr/test");
ConnectInfo sourceConnectInfo=ft.connect(source);
source.setConnect(sourceConnectInfo);//目标ftp服务器
FtpServerInfo dest = newFtpServerInfo();
dest.setHost("192.168.11.130");
dest.setUser("haoxr");
dest.setPass("123456");
dest.setDir("/home/haoxr/test");
ConnectInfo destConnectInfo=ft.connect(dest);
dest.setConnect(destConnectInfo);//文件转移
ft.transfer(source, dest);
ft.disconnect(sourceConnectInfo);
ft.disconnect(destConnectInfo);
}
}