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

📄 jftp.java

📁 一个Java例子
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            if(passiveMode)
            {
                socketCon.println("PASV");
                String status;
                do
                    status = socketIn.readLine();
                while(!status.startsWith("227"));
                setStatus(status);
                int start = status.indexOf(40);
                int end = status.indexOf(41);
                String connStr = status.substring(start + 1, end);
                end = connStr.lastIndexOf(44);
                int highByte = Integer.parseInt(connStr.substring(end + 1));
                connStr = connStr.substring(0, end);
                start = connStr.lastIndexOf(44);
                int lowByte = Integer.parseInt(connStr.substring(start + 1));
                int dataPort = lowByte * 256 + highByte;
                dataSocket = new Socket(host, dataPort);
                if(!sendFtpCmd(cmd))
                    dataSocket = null;
            } else
            {
                ServerSocket ss = new ServerSocket(0);
                StringBuffer addr = new StringBuffer(InetAddress.getLocalHost().getHostAddress().replace('.', ','));
                addr.append(',');
                addr.append(ss.getLocalPort() >>> 8);
                addr.append(',');
                addr.append(ss.getLocalPort() & 0xff);
                sendFtpCmd("PORT " + addr.toString());
                if(statusCode < 300 && sendFtpCmd(cmd))
                {
                    dataSocket = ss.accept();
                    ss.close();
                }
            }
        }
        catch(Exception sx)
        {
            setStatus(sx.toString());
            sx.printStackTrace();
            dataSocket = null;
        }
        return dataSocket;
    }

    private void disconnect()
    {
        if(connected)
            try
            {
                socketCon.println("QUIT");
                socket.close();
            }
            catch(IOException iox) { }
    }

    private void upload()
    {
        if(!connected)
            return;
        int bytesRead = 0;
        int bsize = 0x13880;
        byte dummy[] = new byte[bsize];
        Socket dataSocket = null;
        try
        {
            appletStatus = "Transfering";
            FileInputStream fileIn = new FileInputStream(localFile);
            if(sendFtpCmd("TYPE I"))
                dataSocket = openDataSocket("STOR " + localFile.getName());
            if(dataSocket != null && statusCode < 400)
            {
                OutputStream fileOut = dataSocket.getOutputStream();
                int fileSize = fileIn.available();
                pProgressBar.setMaximum(fileSize);
                int available;
                do
                {
                    available = fileIn.available();
                    fileIn.read(dummy, 0, bsize);
                    bytesRead = fileSize - available;
                    setProgress(bytesRead);
                    if(available < bsize)
                        bsize = available;
                    fileOut.write(dummy, 0, bsize);
                } while(!aborted && available > 0);
                fileIn.close();
                fileOut.close();
                setProgress(0);
                if(successUrl != null)
                    getAppletContext().showDocument(new URL(successUrl));
                if(aborted)
                {
                    String tmp = socketIn.readLine();
                    socketCon.println("DELE " + localFile.getName());
                    tmp = socketIn.readLine();
                    setStatus("Transfer aborted by User");
                } else
                {
                    setStatus(socketIn.readLine());
                }
            } else
            {
                setStatus("Upload Failed!");
            }
        }
        catch(MalformedURLException ux) { }
        catch(FileNotFoundException sx)
        {
            appletStatus = "Failed";
            setStatus("File not found!");
        }
        catch(Exception sx)
        {
            appletStatus = "Failed";
            setStatus(sx.toString());
            sx.printStackTrace();
            FileInputStream fileIn = null;
        }
    }

    private String getMessage(int key)
    {
        return messages[key];
    }

    private void setStatus(String status)
    {
        pStatusTxt.setText(status);
    }

    public String getStatus()
    {
        return appletStatus;
    }

    private void setProgress(int value)
    {
        pProgressBar.setValue(value);
        pStatusTxt.setText(pProgressBar.getString() + " done.");
    }

    public void init()
    {
        host = getParameter("host");
        serverFilePath = getParameter("serverFilePath");
        userName = getParameter("userName");
        pass = getParameter("pass");
        successUrl = getParameter("successUrl");
        port = 21;
        pStatusTxt.setText("idle");
    }

    public Jftp()
    {
        aborted = false;
        appletStatus = "Idle";
        initComponents();
        jShareware.setCursor(new Cursor(12));
        jShareware.addMouseListener(this);
    }

    private Color getColor(String hexColor)
    {
        String h = String.valueOf(hexColor.charAt(0));
        if(h.equals("#"))
            h = hexColor.substring(1, 7);
        else
            h = hexColor;
        int R = Integer.parseInt(h.substring(0, 2), 16);
        int G = Integer.parseInt(h.substring(2, 4), 16);
        int B = Integer.parseInt(h.substring(4, 6), 16);
        return new Color(R, G, B);
    }

    private void initComponents()
    {
        BasePanel = new JPanel();
        jPanel1 = new JPanel();
        jShareware = new JLabel();
        pProgress = new JPanel();
        pProgressBar = new JProgressBar();
        pStatus = new JPanel();
        pStatusLabel = new JLabel();
        pStatusTxt = new JLabel();
        setBackground(new Color(255, 255, 255));
        setName("RootPanel");
        BasePanel.setLayout(new GridLayout(3, 1));
        BasePanel.setBackground(new Color(255, 255, 255));
        BasePanel.setBorder(new TitledBorder("Progress:"));
        jPanel1.setBackground(new Color(255, 255, 255));
        jShareware.setForeground(new Color(204, 0, 0));
        jShareware.setText("Jftp v1.1 Shareware Edition");
        jPanel1.add(jShareware);
        BasePanel.add(jPanel1);
        pProgress.setBackground(new Color(255, 255, 255));
        pProgressBar.setForeground(new Color(51, 102, 255));
        pProgress.add(pProgressBar);
        BasePanel.add(pProgress);
        pStatus.setLayout(new FlowLayout(0));
        pStatus.setBackground(new Color(255, 255, 255));
        pStatusLabel.setText("Status:");
        pStatus.add(pStatusLabel);
        pStatusTxt.setForeground(new Color(102, 102, 102));
        pStatusTxt.setText(" Transfer Complete.");
        pStatus.add(pStatusTxt);
        BasePanel.add(pStatus);
        getContentPane().add(BasePanel, "Center");
    }

    public void mouseClicked(MouseEvent e)
    {
        try
        {
            getAppletContext().showDocument(new URL("http://www.somethingjava.com"), "_jftp");
        }
        catch(Exception x) { }
    }

    public void mouseEntered(MouseEvent mouseevent)
    {
    }

    public void mouseExited(MouseEvent mouseevent)
    {
    }

    public void mousePressed(MouseEvent mouseevent)
    {
    }

    public void mouseReleased(MouseEvent mouseevent)
    {
    }

    public String getAppletInfo()
    {
        String info = "Jftp v1.0 -- FTP Applet Shareware version. (c) 2003 somethingJava.com";
        return info;
    }

    public String[][] getParameterInfo()
    {
        String info[][] = {
            {
                "host", "url", "ftp server to upload to"
            }, {
                "port", "21", "port not available in shareware version"
            }, {
                "userName", "String", "user name may be set at runtime"
            }, {
                "pass", "String", "password may be set at runtime"
            }, {
                "serverFilePath", "String", "remote directory to store file"
            }
        };
        return info;
    }
}

⌨️ 快捷键说明

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