1. 程式人生 > >socket實現兩臺FTP服務器指定目錄下的文件轉移(不依賴第三方jar包)

socket實現兩臺FTP服務器指定目錄下的文件轉移(不依賴第三方jar包)

gets util new ram ali ftp文件 turn tps lap

通過socket實現兩臺FTP服務器指定目錄下的文件轉移,其中包含了基礎了ftp文件列表顯示、上傳和下載。這裏僅供學習用,需掌握的點有socket、ftp命令、文件流讀取轉換等

完整代碼如下:

FtpFileTransfer.java

package com.xxx.ftp.client;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.Socket; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.StringTokenizer; import com.xxx.ftp.model.ConnectInfo; import com.xxx.ftp.model.FtpServerInfo; public class FtpFileTransfer {
/** * 文件轉移 * * @param source * 源ftp文件目錄 * @param dest * 目標ftp文件目錄 * @throws IOException */ public void transfer(FtpServerInfo source, FtpServerInfo dest) throws IOException { // 源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 = new Socket(ip, port); sendCommand(source.getConnect(), "LIST"); BufferedReader br = new BufferedReader(new InputStreamReader(dataSocket.getInputStream())); // 讀取源服務器的目錄下所有文件名稱 List<String> fileNameList = new ArrayList<String>(); 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 = new Socket(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 = new Socket(ip, port); sendCommand(dest.getConnect(), "STOR " + dest.getDir() + "/" + fileName); OutputStream outputStream = destDataSocket.getOutputStream(); int offset; 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服務器連接 * * @param ftpServerInfo * ftp服務器信息 包含用戶名 密碼 * @return * @throws IOException */ public ConnectInfo connect(FtpServerInfo ftpServerInfo) throws IOException { Socket socket = new Socket(ftpServerInfo.getHost(), ftpServerInfo.getPort()); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); ConnectInfo connectInfo = new ConnectInfo(); connectInfo.setSocket(socket); connectInfo.setWriter(writer); connectInfo.setReader(reader); sendCommand(connectInfo, "USER " + ftpServerInfo.getUser()); sendCommand(connectInfo, "PASS " + ftpServerInfo.getPass()); return connectInfo; } /** * 斷開ftp服務器連接 * * @param connectInfo * @throws IOException */ public void disconnect(ConnectInfo connectInfo) throws IOException { if (connectInfo.getSocket() == null || !connectInfo.getSocket().isConnected()) { return; } connectInfo.getSocket().close(); } /** * 發送ftp命令 * * @param connectInfo * ftp服務器連接信息 * @param command * ftp命令 * @throws IOException */ private void sendCommand(ConnectInfo connectInfo, String command) throws IOException { if (command == null || command == "") { return; } connectInfo.getWriter().write(command + "\r\n"); connectInfo.getWriter().flush(); } /** * 根據連接讀取ftp信息 * * @param connect * @return * @throws IOException */ private String readNewMessage(ConnectInfo connect) throws IOException { 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; } } return response; } /** * 獲取ip和端口 * * @param response * @return * @throws IOException */ private String[] getIPPort(String response) throws IOException { 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); return ret; } public static void main(String[] args) throws IOException { FtpFileTransfer ft = new FtpFileTransfer(); // 源ftp服務器 FtpServerInfo source = new FtpServerInfo(); 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 = new FtpServerInfo(); 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); } }

ConnectInfo.java (javabean)

技術分享圖片
package com.xxx.ftp.model;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.net.Socket;

public class ConnectInfo {

    private Socket socket;
    private BufferedReader reader;
    private BufferedWriter writer;

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public BufferedReader getReader() {
        return reader;
    }

    public void setReader(BufferedReader reader) {
        this.reader = reader;
    }

    public BufferedWriter getWriter() {
        return writer;
    }

    public void setWriter(BufferedWriter writer) {
        this.writer = writer;
    }
    
    

}
View Code

FtpServerInfo.java (javabean)

技術分享圖片
package com.xxx.ftp.model;

public class FtpServerInfo {

    public FtpServerInfo() {
        this.port = 21;
    }

    private String host;
    private int port;
    private String user;
    private String pass;

    private ConnectInfo connect;

    public ConnectInfo getConnect() {
        return connect;
    }

    public void setConnect(ConnectInfo connect) {
        this.connect = connect;
    }

    /** 文件目錄 */
    String dir;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public String getDir() {
        return dir;
    }

    public void setDir(String file) {
        this.dir = file;
    }
    
    public static void main(String[] args) {
        FtpServerInfo fs=new FtpServerInfo();
        String pass=fs.getPass();
        System.out.println(pass);
        
        fs=new FtpServerInfo();
        fs.setPass("123456");
        pass=fs.getPass();
        
        System.out.println(pass);
    }

}
View Code

socket實現兩臺FTP服務器指定目錄下的文件轉移(不依賴第三方jar包)