📄 ftpframe.java
字号:
" with user name : "+strUserName+ " and password "+strPassword+ "\nBroken connection.", "Couldn't login "); } catch(Exception e) { e.printStackTrace(); showError("Couldn't login "+"to Server : "+strServerIP+ " on port : "+strServerPort+ " with user name : "+strUserName+ " and password "+strPassword+ "\nI don't know why.", "Couldn't login "); } restoreCursor(); } private void closeConnection() { try { if(m_objFtpClient!=null) m_objFtpClient.closeServer(); }catch(Exception e) { e.printStackTrace(); } } private void lsCurrentDir() throws Exception { statusBar.setText(" "); statusBar.repaint(); Logger.log("sending ls"); statusBar.setText("Sending ls"); statusBar.repaint(); StringBuffer strbuff=new StringBuffer(); TelnetInputStream objTelnetInputStream=m_objFtpClient.list(); BufferedReader objBufferedReader= new BufferedReader( new InputStreamReader(objTelnetInputStream)); String strPrefix="get "; int iCountLines=0; strbuff.append( "<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td>"+ "<td><a href='"+m_strCDUP_COMMAND+"'>parent dir</a></td>"+ "<td><a href='"+m_strCDUP_COMMAND+"'>..</a></td></tr>"); while(true) { String strLine=objBufferedReader.readLine(); if(strLine==null) break; strLine=strLine.trim(); iCountLines++; statusBar.setText("Retreived "+String.valueOf(iCountLines)+" lines"); Rectangle rect=statusBar.getBounds(); Graphics objGraphics=statusBar.getGraphics(); this.repaint(); if(strLine.startsWith("d") || (strLine.startsWith("l") && strLine.endsWith("/"))) { strPrefix=m_strDIR_COMMAND; } else { strPrefix=m_strGET_COMMAND; } int i=strLine.lastIndexOf(" "); String str=strLine.substring(i+1); strLine=strLine.substring(0,i); strbuff.append("<tr>"+tabulate(strLine)+ "<td><a href='"+strPrefix+" "+ str+"'> "+str+"</a></td></tr>"); } htmlPane.setContentType("text/html"); String strContent="<html><body><table>"+strbuff.toString()+"</table></body></html>"; htmlPane.setText(strContent); objBufferedReader.close(); objTelnetInputStream.close(); statusBar.setText("Ready"); } private String tabulate(String strLine) { String strRet=""; StringTokenizer objStringTokenizer=new StringTokenizer(strLine); while(objStringTokenizer.hasMoreElements()) { String str=objStringTokenizer.nextToken(" "); strRet+="<td>"+str+"</td>"; } return strRet; } private void getFile(String strFileNameToSave) { JFileChooser chooser=new JFileChooser(); String strCurrentDir=chooser.getCurrentDirectory().toString(); String strPathFileName=strCurrentDir+File.separator+strFileNameToSave; Logger.log("strPathFileName="+strPathFileName); File f=new File(strFileNameToSave); chooser.setSelectedFile(f); chooser.setDialogTitle("Choose file name and where to save file"); int returnVal=chooser.showSaveDialog(this); if(returnVal == JFileChooser.APPROVE_OPTION) { String strFileName=chooser.getSelectedFile().getAbsoluteFile().toString(); Logger.log("You choose to save this file: " + strPathFileName + " AS " + strFileName); setWaitCursor(); statusBar.setText("Downloading"); saveFile(strFileNameToSave,strFileName); restoreCursor(); statusBar.setText("Ready"); } } void saveFile(String strFileNameToSave,String strFileName) { try { FileOutputStream objFileOutputStream=new FileOutputStream(strFileName); m_objFtpClient.binary(); TelnetInputStream objTelnetInputStream=m_objFtpClient.get(strFileNameToSave); int iTotalBytes=0; byte b[]=new byte[1024]; while(true) { int iReadBytes=objTelnetInputStream.read(b); if(iReadBytes<=0) break; objFileOutputStream.write(b,0,iReadBytes); iTotalBytes+=iReadBytes; statusBar.setText("Downloaded bytes "+iTotalBytes); statusBar.repaint(); statusBar.getUI().update(statusBar.getGraphics(),statusBar); } objFileOutputStream.close(); //objTelnetInputStream.close(); }catch(Exception e) { e.printStackTrace(); showError("Could't save "+strFileNameToSave,e.getMessage()); } } private static final String m_strDIR_COMMAND="ls "; private static final String m_strGET_COMMAND="get "; private static final String m_strCDUP_COMMAND="cdup"; JButton btnCDUP; JButton btnSearch; public void hyperlinkUpdate(HyperlinkEvent e) { if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { String str=e.getDescription(); Logger.log(str); if(str.startsWith(m_strCDUP_COMMAND)) { btnCDUP_actionPerformed(null); return; } if(str.startsWith(m_strDIR_COMMAND)) { String strDir=str.substring(m_strDIR_COMMAND.length()).trim(); try { setWaitCursor(); m_objFtpClient.cd(strDir); lsCurrentDir(); restoreCursor(); m_strCurrentDir=strDir; }catch(Exception e1) { e1.printStackTrace(); showError("Could't cd "+strDir,e1.getMessage()); } return; } if(str.startsWith(m_strGET_COMMAND)) { String strFileNameToSave=str.substring( m_strGET_COMMAND.length()).trim(); Logger.log("strFileNameToSave="+strFileNameToSave); try { getFile(strFileNameToSave); }catch(Exception e2) { e2.printStackTrace(); showError("Could't save "+strFileNameToSave,e2.getMessage()); } return; } } } String m_strCurrentDir=""; public void actionPerformed(ActionEvent e) { if(e.getSource().equals(m_mnuConnect) || e.getSource().equals(m_btnOpen)) { Logger.log("Open"); setWaitCursor(); showOpenServerDialog(); restoreCursor(); return; } if(e.getSource().equals(m_mnuClose) ) { Logger.log("Close"); closeConnection(); return; } if(e.getSource().equals(m_btnUpload)) { Logger.log("Upload"); showUploadFileDialogAndUpload(); return; } if(e.getSource().equals(btnCDUP)) { btnCDUP_actionPerformed(null); return; } if(e.getSource().equals(m_mnuFileExit)) { exitFromAplication(); return; } if(e.getSource().equals(m_mnuHelpAbout)) { m_mnuHelpAbout_actionPerformed(e); return; } if(e.getSource().equals(btnCD)) { btnCD_actionPerformed(e); return; } if(e.getSource().equals(btnSearch)) { btnSearch_actionPerformed(e); return; } if(e.getSource().equals(m_btnAbout)) { m_btnAbout_actionPerformed(e); return; } /* if(e.getSource().equals()) { return; } */ } void exitFromAplication() { closeConnection(); if(m_booIsStandalone) System.exit(0); else this.hide(); } /**File | Exit action performed*/ public void m_mnuFileExit_actionPerformed(ActionEvent e) { exitFromAplication(); } /**Help | About action performed*/ public void m_mnuHelpAbout_actionPerformed(ActionEvent e) { FtpFrame_AboutBox dlg = new FtpFrame_AboutBox(this); Dimension dlgSize = dlg.getPreferredSize(); Dimension frmSize = getSize(); Point loc = getLocation(); dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); dlg.setModal(true); dlg.show(); } /**Overridden so we can exit when window is closed*/ protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { m_mnuFileExit_actionPerformed(null); } } void m_btnAbout_actionPerformed(ActionEvent e) { try { setWaitCursor(); lsCurrentDir(); restoreCursor(); }catch(Exception e1) { e1.printStackTrace(); showError("Could't list directories ",e1.getMessage()); } } private void execCDUP() { try { m_objFtpClient.cd(".."); lsCurrentDir();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -