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

📄 ftpframe.java

📁 ftp client with java
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        }catch(Exception e1)        {            e1.printStackTrace();            showError("Could't cdup or ls ",e1.getMessage());        }        return;    }    void btnCDUP_actionPerformed(ActionEvent e)    {        setWaitCursor();        execCDUP();        restoreCursor();    }    void btnSearch_actionPerformed(ActionEvent objActionEvent)    {        statusBar.setText(" ");        statusBar.repaint();        setWaitCursor();        showSearchDialogAndSearch();        restoreCursor();    }    private void showSearchDialogAndSearch()    {        String strSubDirs=m_strCurrentDir;        JOptionPane objJOptionPane=new JOptionPane("Search text");        String strQuery=objJOptionPane.showInputDialog(this,"Search text");        if(strQuery==null)            return;        if(strQuery.equals(""))            return;        try        {            recourseFind(strSubDirs,strQuery);        }catch(Exception e)        {            e.printStackTrace();            showError("Could't find ... ",e.getMessage());        }        statusBar.setText("Ready");        String strAllFoundFiles="";        for(Enumeration enum=m_vFounded.elements();enum.hasMoreElements();)        {            strAllFoundFiles +=(String)enum.nextElement()+"\n";        }        if(strAllFoundFiles.equals(""))        {            strAllFoundFiles="NOT FOUND";            JOptionPane.showMessageDialog(this,strAllFoundFiles,                strAllFoundFiles,                JOptionPane.INFORMATION_MESSAGE);        }        else        {            JOptionPane.showMessageDialog(this,"Found files",                strAllFoundFiles,                JOptionPane.INFORMATION_MESSAGE);        }    }    Vector m_vFounded=new Vector();    private void setWaitCursor()    {        m_objOLDCursor=this.getCursor();        m_objOLDHTMLCursor=htmlPane.getCursor();        this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));        htmlPane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));    }    Cursor m_objOLDCursor;    Cursor m_objOLDHTMLCursor;    private void restoreCursor()    {        this.setCursor(m_objOLDCursor);        htmlPane.setCursor(m_objOLDHTMLCursor);    }    private void recourseFind(String strSubDirs,String strQuery)        throws Exception    {        //System.out.println("Searching in "+strSubDirs);        Vector vSubDirs=new Vector();        Vector vCrashSubDirs=null;        Logger.log("changing dir to : "+strSubDirs);        statusBar.setText("Sending : cd "+strSubDirs);        if(!(strSubDirs.equals("")))            m_objFtpClient.cd(strSubDirs);        statusBar.setText("Sending ls");        statusBar.repaint();            TelnetInputStream objTelnetInputStream=m_objFtpClient.list();            BufferedReader objBufferedReader=                new BufferedReader(                    new InputStreamReader(objTelnetInputStream));            int iCountLines=0;            while(true)            {                String strLine=objBufferedReader.readLine();                if(strLine==null)                    break;                strLine=strLine.trim();                iCountLines++;//                statusBar.setText("Searching "+String.valueOf(iCountLines));//                Rectangle rect=statusBar.getBounds();//                Graphics objGraphics=statusBar.getGraphics();                //statusBar.paint(objGraphics);//                this.repaint();                int i=strLine.lastIndexOf(" ");                String strFileName=strLine.substring(i+1).trim();                if(strLine.startsWith("d") ||                    (strLine.startsWith("l") &&                      strLine.endsWith("/")))                {                    if(!(strFileName.equals(".")) && !(strFileName.equals("..")))                    {                        vSubDirs.addElement(strSubDirs+"/"+strFileName);                    }                }                else                {                    if(strFileName.indexOf(strQuery)>=0)                    {                        String strFound=strSubDirs+"/"+strFileName;                        Logger.log("Found "+strFound);                        m_vFounded.addElement(strFound);                    }//                    Logger.log(strLine);                }            }            objBufferedReader.close();            objTelnetInputStream.close();            vCrashSubDirs=recourseSubDirs(vSubDirs,strQuery);            for(int i=1;i<m_iMaxRetries;i++)            {                vCrashSubDirs=recourseSubDirs(vCrashSubDirs,strQuery);                if(vCrashSubDirs.isEmpty())                    break;            }            if(!(vCrashSubDirs.isEmpty()))            {                Logger.log("Following dirs are not scaned maximum "+                    " retries exceeded m_iMaxRetries"+m_iMaxRetries);                for(Enumeration enum=vCrashSubDirs.elements();enum.hasMoreElements(); )                {                    Logger.log((String)enum.nextElement());                }            }    }    int m_iMaxRetries=10;    JButton btnCD;    Vector recourseSubDirs(Vector vSubDirs,String strQuery)    {        Vector vCrashSubDirs=new Vector();        for(Enumeration enum=vSubDirs.elements();enum.hasMoreElements(); )        {            String strSubDir="";            try            {                strSubDir=(String)enum.nextElement();                recourseFind(strSubDir,strQuery);            }catch(Exception e)            {                Logger.log("Dir : "+strSubDir+" not searched ");                e.printStackTrace();                vCrashSubDirs.addElement(strSubDir);                //showError("Could't find ... ",e.getMessage());            }        }        return vCrashSubDirs;    }    ChangeDirPanel m_objChangeDirPanel=null;    JPanel jPanel1;    GridLayout gridLayout1;    JTextField statusBar;    JProgressBar progressBar;    void showCDAndChangeDirectory()    {        if(m_objChangeDirPanel==null)            m_objChangeDirPanel=new ChangeDirPanel();   		Object l_objects[] = new Object[1];		l_objects[0] = m_objChangeDirPanel;        String l_options[] = {            "Change Dir", "Cancel"        };        if(JOptionPane.showOptionDialog(this, ((Object) (l_objects)),        	 "Change Directory", -1, 3, null, l_options, l_options[0]) == 0)        {        	Logger.log("Connect");            String strDir=m_objChangeDirPanel.m_txtDir.getText();            if(!(strDir.equals("")))            {                try                {                    if(strDir.startsWith("/"))                    {                        strDir=strDir.substring(1);                    }                    StringTokenizer objStringTokenizer=                                new StringTokenizer(strDir );                    while(objStringTokenizer.hasMoreTokens())                    {                        String strSubDir=objStringTokenizer.nextToken("/");                        m_objFtpClient.cd(strSubDir);                    }                    lsCurrentDir();                }catch(Exception e1)                {                    e1.printStackTrace();                    showError("Could't cd "+strDir,e1.getMessage());                }            }        }    }    void btnCD_actionPerformed(ActionEvent e)    {        setWaitCursor();        showCDAndChangeDirectory();        restoreCursor();    }	void showError(String strMessage,String strTitle)	{        JOptionPane.showMessageDialog(this,			strMessage,strTitle,			JOptionPane.ERROR_MESSAGE);	}    public void taskAccomplished(TaskEvent objTaskEvent)    {        /**@todo: Implement this ftpgui.TaskEventListener method*/        //throw new java.lang.UnsupportedOperationException("Method taskAccomplished() not yet implemented.");    }    private void showUploadFileDialogAndUpload()    {        JFileChooser chooser=new JFileChooser();        chooser.setDialogTitle("Choose file to upload");        int returnVal=chooser.showOpenDialog(this);        if(returnVal == JFileChooser.APPROVE_OPTION)        {            String strFileName=chooser.getSelectedFile().getAbsoluteFile().toString();            Logger.log("You choose to upload this file: " +                strFileName);            setWaitCursor();            uploadFile(strFileName);            restoreCursor();        }    }	public static String cutPath(String strFileName)	{		//System.out.println("Cutting Path : strFileName="+strFileName);		int iIndex=strFileName.lastIndexOf("/");		if(iIndex<0)		{			iIndex=strFileName.lastIndexOf("\\");		}		if(iIndex<0)		{			//System.out.println("Return strFileName="+strFileName);			return strFileName;		}		else		{			strFileName=strFileName.substring(iIndex+1);			//System.out.println("Return strFileName="+strFileName);			return strFileName;		}	}    private void uploadFile(String strFileName)    {        try        {            String strRemoteFileName=cutPath(strFileName);            System.out.println("strRemoteFileName = "+strRemoteFileName);            m_objFtpClient.binary();//It is not necessary becouse by default                                    //sun.net.ftp.FtpClient sets binary transfer mode            TelnetOutputStream objTelnetOutputStream=m_objFtpClient.put(strRemoteFileName);            FileInputStream objFileInputStream=new FileInputStream(strFileName);            int iAvail=0;            int iTotalBytes=0;            while((iAvail=objFileInputStream.available())>0)            {                byte b[]=new byte[iAvail];                int iReadBytes=objFileInputStream.read(b);                if(iReadBytes<=0)                    break;                objTelnetOutputStream.write(b,0,iReadBytes);                iTotalBytes+=iAvail;                statusBar.setText("Uploaded bytes "+iTotalBytes);                statusBar.repaint();                statusBar.getUI().update(statusBar.getGraphics(),statusBar);            }            objFileInputStream.close();            objTelnetOutputStream.close();        }        catch(java.io.IOException ioex)        {            ioex.printStackTrace();            showError("Couldn't upload file : "+strFileName,"Couldn't upload file!");        }        catch(Exception e)        {            e.printStackTrace();            showError("Couldn't upload file : "+strFileName,"Couldn't upload file!\nI don't know why.");        }    }}

⌨️ 快捷键说明

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