⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftpclient.java

📁 Java实现的explorer程序
💻 JAVA
字号:
/*
 * FtpClient.java
 *
 * Created on 2007年8月7日, 下午10:09
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package explorer;

import java.io.*;
import java.net.*;
import java.util.*;

/**
 *
 * @author king
 */
public class FtpClient {
    private Socket socket = null;
    private ServerSocket server = null;
    private BufferedReader reader = null;
    private BufferedWriter writer = null;
    private static boolean DEBUG = false;
    public Vector<String> fileName = null;
    
    private void sendLine(String line) throws IOException{
        if (socket == null){
            throw new IOException("FtpClient is not connected.");
        }
        try{
            writer.write(line + "\r\n");
            writer.flush();
            if (DEBUG){
                System.out.println(">" + line);
            }
        }catch (IOException e){socket = null;throw e;}
    }
    
    private String readLine() throws IOException{
        String line = reader.readLine();
        if (DEBUG) {
            System.out.println("<" + line);
        }
        return line;
    }
    
    /** Creates a new instance of FtpClient */
    public FtpClient() {
    }
    
    public synchronized void connect(String host) throws IOException{    		
        connect(host,21);       	 
    }
    
    public synchronized void connect(String host,int port) throws IOException{    		
        connect(host,port,"anonymous","anonymous");       	
    }
    
    public boolean isServer(String host){
        try{
            Socket testSocket = new Socket(host,21);
            testSocket.setSoTimeout(1000);
            BufferedReader testReader = new BufferedReader(new InputStreamReader(testSocket.getInputStream()));
            String response = testReader.readLine();
            if (response.startsWith("220"))
            {
                testSocket.close();
                testReader.close();
                return true;
            }
            testSocket.close();
            testReader.close();
            return false;
        }catch(Exception e){return false;}
    }
    
    public synchronized void connect(String host,int port,String user,String pass){
        try{
            if (socket != null)
            {
                throw new IOException("FtpClient is already connected.Disconnect first.");
            }
            socket = new Socket(host,port);
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            String response = readLine();
            while (response.startsWith("220-")){
            	response = readLine();
            }
            if (!response.startsWith("220 ")){
                throw new IOException("FtpClient received an unknown response when connecting to the FTP server:"+response);
            }
            sendLine("USER " + user);
            response = readLine();
            while (response.startsWith("331-")){
            	response = readLine();
            }
            if (!response.startsWith("331 ")){
                throw new IOException("FtpClient received an unknown response after sending the user:"+response);
            }
            sendLine("PASS " + pass);
            response = readLine();
            while (response.startsWith("230-")){
            	response = readLine();
            }
            if (!response.startsWith("230 ")){
                throw new IOException("FtpClient was unable to log in with the supplied password:"+response);
            }
            System.out.println("It is connected.");
        }catch(IOException e){System.out.println(e);}
    }
    public synchronized void disconnect(){
        try{
            sendLine("QUIT");
        }catch(IOException e){System.out.println(e);}
        finally{socket = null;}
    }
    
    public synchronized void download(String dir,String fileName){
        Socket dataSocket = null;
        String cmd = "PORT ";
        int n = 0;
        try{
            server = new ServerSocket(0,1);
            server = new ServerSocket(0,1);
            byte[] address = InetAddress.getLocalHost().getAddress();
            for (int i = 0;i < 4;++i)
            {
                cmd = cmd + (address[i] & 0xff) + ",";
            }
            cmd = cmd + (((server.getLocalPort())/256) & 0xff) + "," + (server.getLocalPort() & 0xff);
            sendLine(cmd);   
            sendLine("RETR " + fileName);
            dataSocket = server.accept();
            byte[] buff = new byte[20480] ;
            // 在客户端上准备接收用的文件
            FileOutputStream outfile = new FileOutputStream(dir) ;
            // 构造传输文件用的数据流
            BufferedInputStream dataInput = new BufferedInputStream(dataSocket.getInputStream()) ;
            // 接收来自服务器的数据,写入本地文件
            while((n = dataInput.read(buff)) != -1){
                outfile.write(buff,0,n) ;
            }
            outfile.flush();
            outfile.close();
            dataInput.close();
            dataSocket.close();
        }catch(Exception e){}
    }
    
    public synchronized void getList(){
        Socket dataSocket = null;
        String cmd = "PORT ";
        String gainFile = null;
        fileName = new Vector<String>();
        try{
            server = new ServerSocket(0,1);
            byte[] address = InetAddress.getLocalHost().getAddress();
            for (int i = 0;i < 4;++i)
            {
                cmd = cmd + (address[i] & 0xff) + ",";
            }
            cmd = cmd + (((server.getLocalPort())/256) & 0xff) + "," + (server.getLocalPort() & 0xff);
            sendLine(cmd);   
            sendLine("LIST");
            String response = readLine();
            dataSocket = server.accept();
            BufferedReader dataReader = new BufferedReader(new InputStreamReader(dataSocket.getInputStream()));
            while ((gainFile = dataReader.readLine()) != null)
            {		
                fileName.addElement(gainFile);
            }
            server.close();
            server = null;
        }catch(IOException e){System.out.println(e);}
         catch(Exception e){System.out.println(e);}
    }
    public synchronized String pwd() throws IOException {
        sendLine("PWD");
        String dir = null;
        String response = readLine();
        if (response.startsWith("257 ")){
            int firstQuote = response.indexOf('\"');
            int secondQuote = response.indexOf('\"',firstQuote+1);
            if (secondQuote > 0){
                dir = response.substring(firstQuote+1,secondQuote);
            }
        }
        return dir;
    }
    public synchronized void getparentdir() throws IOException {
        sendLine("CWD..");
        String response = readLine();
        if (response.startsWith("250 ")){
            System.out.println("Got it!");
        }
    }
    public synchronized boolean cwd(String dir) throws IOException{
        sendLine("CWD " + dir);
        String response = readLine();
        return (response.startsWith("250 "));
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -