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

📄 jftp.java

📁 一个Java例子
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   Jftp.java

import java.applet.AppletContext;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;
import java.net.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.border.TitledBorder;

public class Jftp extends JApplet
    implements MouseListener, Runnable
{

    private final int MSG_OK = 0;
    private final int ERR_421 = 1;
    private final int ERR_ACCESS = 2;
    private final int ERR_FILE = 3;
    private final int ERR_PARAMS = 4;
    private final int ERR_PATH = 5;
    private final int MSG_COMP = 6;
    private final int ERR_UKNHOST = 7;
    private final String messages[] = {
        "Ready.", "Connection lost.", "Access denied.", "Invalid or No File Specified.", "Invalid connection params.", "Invalid path.", "Transfer complete.", "Host not found."
    };
    private boolean aborted;
    private boolean connected;
    public String host;
    public int port;
    public String userName;
    public String pass;
    public String serverFilePath;
    private boolean passiveMode;
    private String clientFile;
    private String clientPath;
    private String successUrl;
    private String appletStatus;
    private File localFile;
    private Socket socket;
    private PrintWriter socketCon;
    private BufferedReader socketIn;
    private String statusText;
    private int statusCode;
    private Vector fileQueue;
    private JPanel pStatus;
    private JLabel jShareware;
    private JPanel pProgress;
    private JLabel pStatusLabel;
    private JLabel pStatusTxt;
    private JProgressBar pProgressBar;
    private JPanel jPanel1;
    private JPanel BasePanel;

    private int getFtpReply()
        throws IOException
    {
        int code = -1;
        statusText = "";
        statusCode = -1;
        String reply = socketIn.readLine();
        if(reply != null)
        {
            int len = reply.length();
            if(len >= 3)
            {
                try
                {
                    code = Integer.parseInt(reply.substring(0, 3));
                }
                catch(NumberFormatException nfx) { }
                if(len > 3 && reply.charAt(3) == '-')
                    do
                    {
                        reply = socketIn.readLine();
                        if(reply == null)
                            throw new IOException(getMessage(1));
                    } while(reply.length() <= 3 || reply.charAt(3) == '-' || !Character.isDigit(reply.charAt(0)));
                if(statusCode == 421)
                    throw new IOException(getMessage(1));
            }
        }
        statusText = reply;
        statusCode = code;
        return code;
    }

    private boolean sendFtpCmd(String cmd)
    {
        socketCon.println(cmd);
        return getFtpReply() < 400;
        IOException iox;
        iox;
        setStatus(iox.getMessage());
        return false;
    }

    private void connect()
    {
        if(host == null || userName == null || pass == null)
        {
            setStatus(getMessage(4));
            return;
        }
        setStatus("Connecting to: " + host);
        Thread.yield();
        socket = new Socket(host, port);
        socket.setKeepAlive(true);
        setStatus("Connected to " + host + ":" + port);
        socketIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        socketCon = new PrintWriter(socket.getOutputStream(), true);
        getFtpReply();
        if(statusCode == 220)
        {
            sendFtpCmd("USER " + userName);
            if(statusCode == 331)
                sendFtpCmd("PASS " + pass);
            if(statusCode > 500)
            {
                setStatus(getMessage(2));
                return;
            }
        }
        setStatus(getMessage(0));
        if(serverFilePath != null && !serverFilePath.equals(""))
        {
            sendFtpCmd("CWD " + serverFilePath);
            if(statusCode > 500)
            {
                setStatus(getMessage(5));
                return;
            }
        }
        try
        {
            connected = true;
        }
        catch(ConnectException sx)
        {
            setStatus(getMessage(7));
        }
        catch(UnknownHostException sx)
        {
            setStatus(getMessage(7));
        }
        catch(IOException sx)
        {
            setStatus(sx.toString());
            sx.printStackTrace();
            connected = false;
            socket = null;
        }
        catch(Exception x)
        {
            x.printStackTrace();
        }
        return;
    }

    public synchronized void sendFile()
    {
        System.out.println("send requested.");
        notifyAll();
    }

    public void setUserName(String userName)
    {
        this.userName = userName;
    }

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

    public void setPassive(boolean passive)
    {
        passiveMode = passive;
    }

    public void setPassive(String passive)
    {
        passiveMode = passive.equalsIgnoreCase("true");
    }

    public void setFileName(String fn)
    {
        if(fn != null && !fn.equals(""))
            fileQueue.add(0, fn);
    }

    public void setServerPath(String serverPath)
    {
        serverFilePath = serverPath;
    }

    public void setClientPath(String clientPath)
    {
        this.clientPath = clientPath;
    }

    public void setClientFile(String clientFile)
    {
        this.clientFile = clientFile;
    }

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

    private synchronized void doSendFile(String file)
    {
        aborted = false;
        localFile = new File(file);
        if(localFile != null && localFile.exists())
        {
            connect();
            upload();
            disconnect();
        } else
        {
            setStatus(getMessage(3));
        }
    }

    public void start()
    {
        fileQueue = new Vector();
        (new Thread(this)).start();
    }

    public synchronized void run()
    {
        while(fileQueue != null) 
        {
            if(fileQueue.size() != 0)
                doSendFile((String)fileQueue.remove(0));
            try
            {
                wait();
            }
            catch(InterruptedException ex) { }
        }
    }

    public synchronized void stop()
    {
        fileQueue = null;
        notifyAll();
    }

    private Socket openDataSocket(String cmd)
    {
        Socket dataSocket = null;
        try
        {

⌨️ 快捷键说明

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