📄 frame1.java
字号:
package boco.ftp.ftp2;import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.*;import javax.swing.*;import sun.net.*;import sun.net.ftp.*;import com.borland.jbcl.layout.*;public class Frame1 extends JFrame { //定义变量 private JPanel contentPane; private XYLayout xYLayout1 = new XYLayout(); private JLabel LblHost = new JLabel(); private JLabel LblUser = new JLabel(); private JTextField txtHost = new JTextField(); private JTextField txtUser = new JTextField(); private JLabel LblPass = new JLabel(); private JPasswordField passPWD = new JPasswordField(); private JButton BtnConn = new JButton(); private JButton BtnClose = new JButton(); private JLabel LblPath = new JLabel(); private JTextField txtPath = new JTextField(); private JLabel LblFile = new JLabel(); private JTextField txtFilepath = new JTextField(); private JButton BtnBrow1 = new JButton(); private JButton BtnUpload = new JButton(); private JTextArea lsArea = new JTextArea(); FtpClient aftp; DataOutputStream outputs; TelnetInputStream tis; TelnetOutputStream tos; FileInputStream fis; String host = ""; String user = ""; String pass = ""; String path = ""; String file1 = ""; String prompts = "没有连接主机!"; String str =""; //private JLabel LblPrompt = new JLabel(); final JFileChooser fc = new JFileChooser(); private JComboBox ComboPath = new JComboBox(); private JScrollPane jScrollPane1 = new JScrollPane(); //Construct the frame public Frame1() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]"))); contentPane = (JPanel) this.getContentPane(); String filename1 = "open.gif"; String filename2 = "upload.gif"; String path1 = "/image/" + filename1; String path2 = "/image/" + filename2; ImageIcon icon1 =new ImageIcon(getClass().getResource(path1)); ImageIcon icon2 =new ImageIcon(getClass().getResource(path2)); JButton BtnBrow2 = new JButton("浏览文件",icon1); LblHost.setText("远程主机"); txtHost.setText("196.168.1.132"); contentPane.setLayout(xYLayout1); this.setSize(new Dimension(561, 151)); this.setTitle("FTP小程序"); LblUser.setText("用户名"); txtUser.setText("webftp"); LblPass.setText("密码"); passPWD.setText("webftp"); BtnConn.setText("连接"); BtnClose.setText("断开"); BtnClose.setEnabled(false); LblPath.setText("主机路径"); txtPath.setText(""); txtPath.setVisible(false); LblFile.setText("文件路径"); txtFilepath.setText(""); BtnBrow1.setText("浏览主机文件"); BtnUpload.setText("上传文件"); BtnUpload.setIcon(icon2); lsArea.setText("主机文件目录"); lsArea.setEditable(false); //ComboPath.setEditable(true); ComboPath.addItem("请选择路径"); ComboPath.addItem("bin"); ComboPath.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent ae){ ComboPath_actionPerformed(ae); } }); BtnBrow2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int returnVal = fc.showOpenDialog(contentPane); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); file1 = file.getPath(); txtFilepath.setText(file.getPath()); } } }); BtnConn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(BtnConn()){ BtnConn.setEnabled(false); BtnClose.setEnabled(true); } } }); BtnClose.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { BtnClose(); } }); BtnBrow1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { BtnBrow1(); } }); BtnUpload.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { BtnUpload(); } }); contentPane.add(txtHost, new XYConstraints(63, 9, 98, 25)); contentPane.add(LblUser, new XYConstraints(167, 9, 47, 25)); contentPane.add(txtUser, new XYConstraints(218, 9, 84, 25)); contentPane.add(passPWD, new XYConstraints(348, 9, 62, 25)); contentPane.add(BtnClose, new XYConstraints(484, 9, 64, 25)); contentPane.add(BtnConn, new XYConstraints(414, 9, 66, 25)); contentPane.add(LblPass, new XYConstraints(305, 9, 39, 25)); contentPane.add(txtPath, new XYConstraints(377, 39, 32, 25)); contentPane.add(LblHost, new XYConstraints(5, 9, 56, 25)); contentPane.add(ComboPath, new XYConstraints(64, 48, 183, 25)); contentPane.add(LblPath, new XYConstraints(3, 48, 56, 25)); contentPane.add(LblFile, new XYConstraints(258, 48, 56, 25)); contentPane.add(txtFilepath, new XYConstraints(335, 48, 203, 25)); contentPane.add(BtnBrow2, new XYConstraints(308, 86, 108, 25)); contentPane.add(BtnUpload, new XYConstraints(430, 86, 108, 25)); contentPane.add(BtnBrow1, new XYConstraints(430, 118, -1, 25)); contentPane.add(jScrollPane1, new XYConstraints(12, 146, 525, 260)); jScrollPane1.getViewport().add(lsArea, null); jScrollPane1.setVisible(false); BtnBrow1.setVisible(false); } //Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } public void getTxt(){ host = txtHost.getText().toString(); user = txtUser.getText().toString(); pass = new String(passPWD.getPassword()); } //建立与主机的按钮 public boolean connect(String hostname,String uid,String pwd){ try{ getTxt(); aftp = new FtpClient(hostname); aftp.login(uid,pwd); //aftp.binary(); JOptionPane.showMessageDialog(contentPane,"连接主机"+hostname+"成功","连接FTP主机返回提示",JOptionPane.INFORMATION_MESSAGE); //showFileContents(); } catch(FtpLoginException fle){ JOptionPane.showMessageDialog(contentPane,"无权限与主机:"+hostname+"连接!","连接FTP主机返回提示",JOptionPane.ERROR_MESSAGE); return false; } catch (IOException ioe){ JOptionPane.showMessageDialog(contentPane,"连接主机:"+hostname+"失败!","连接FTP主机返回提示",JOptionPane.ERROR_MESSAGE); return false; } catch(SecurityException se){ JOptionPane.showMessageDialog(contentPane,"安全权限不允许与主机:"+hostname+"连接!","连接FTP主机返回提示",JOptionPane.ERROR_MESSAGE); return false; } return true; } //连接按钮:与主机建立Ftp的连接 public boolean BtnConn(){ getTxt(); if(connect(host,user,pass)){ BtnConn.setEnabled(false); BtnClose.setEnabled(true); } return true; } public void stop(){ try{ aftp.closeServer(); aftp = null; }catch(IOException ioe){ ioe.printStackTrace (); } } //响应下拉框的选择 public void ComboPath_actionPerformed(ActionEvent ae) { JComboBox cb = (JComboBox)ae.getSource(); Object Item = cb.getSelectedItem(); if(Item!=null){ String path1 = (String)cb.getSelectedItem(); if(!(Item.equals(cb.getItemAt(0)))){ txtPath.setText(path1); } else txtPath.setText(""); } System.out.println(txtPath.getText()); } //断开与主机的连接按钮 public boolean BtnClose(){ URL img = getClass().getResource("/image/disconnect.gif"); String imagesrc = "<img src=\"" + img + "\" width=\"15\" height=\"15\">"; int result = JOptionPane.showConfirmDialog(contentPane,"<html><center>"+imagesrc+"<b>你确定关闭与服务器的连接?</b></center></html>","断开Ftp提示",JOptionPane.INFORMATION_MESSAGE); if(result == JOptionPane.YES_OPTION){ stop(); BtnConn.setEnabled(true); BtnClose.setEnabled(false); JOptionPane.showMessageDialog(contentPane,"与主机"+host+"连接已断开","返回提示",JOptionPane.INFORMATION_MESSAGE); return true; }else if(result == JOptionPane.NO_OPTION){ return false; } return true; } //点击浏览主机目录文件按钮 public void BtnBrow1(){ try{ if(path.length()!=0){ aftp.cd(path); path = ".."; } //showFileContents(); if(path.length()!=0){ aftp.cd(path); aftp.cd(path); } }catch(IOException ioe){ ioe.printStackTrace (); } } //Ftp上传文件 public void BtnUpload(){ //file1 = txtFilepath.getText().toString(); int lastSlash = file1.lastIndexOf('\\'); String filename = file1.substring(lastSlash+1); filename = filename.toLowerCase(); path = txtPath.getText().toString(); URL img = getClass().getResource("/image/upload1.gif"); String imagesrc = "<img src=\"" + img + "\" width=\"24\" height=\"24\">"; String message = "你真的要上传文件"; String str = ""; int j = 1; if(aftp!=null){ int result = JOptionPane.showConfirmDialog(contentPane,"<html><center>" + imagesrc + "</center><center><h2>" + message + filename +"?</h2></center></html>","上传文件提示",JOptionPane.INFORMATION_MESSAGE); if(result == JOptionPane.YES_OPTION){ try{ getTxt(); if(path.length()!=0) aftp.cd(path); aftp.binary(); tos=aftp.put(filename); File file_in=new File(file1); fis=new FileInputStream(file_in); //byte[] bytes=new byte[1024]; byte[] bytes = new byte[fis.available()]; //tos.write(bytes); //tos.flush(); int c; System.out.println("putting: " + filename); while ((c=fis.read(bytes))!=-1){ tos.write(bytes,0,c); } fis.close(); tos.close(); int k=0; if(path.length()!=0){ if(path.lastIndexOf("/")==path.length()-1) path =path.substring(0,path.length()-1); for(int i=0;i<path.length();i++){ str = path.substring(i,i+1); if(str.equals("/")){ j +=1; } } System.out.println(j); for(int i=0;i<j;i++) aftp.cd(".."); } JOptionPane.showMessageDialog(contentPane,"你已经成功上传文件"+filename,"上传文件成功",JOptionPane.INFORMATION_MESSAGE); } catch (IOException ioe){ JOptionPane.showMessageDialog(contentPane,"上传文件"+filename+"失败咯!","上传文件失败",JOptionPane.ERROR_MESSAGE); ioe.printStackTrace(); } }else if(result == JOptionPane.NO_OPTION){ } } else { JOptionPane.showMessageDialog(contentPane,"你还没连接Ftp服务器哪!","错误提示",JOptionPane.ERROR_MESSAGE); } } //显示主机目录内容 public void showFileContents(){ String buf = ""; byte[] bt; StringBuffer sb = new StringBuffer(); int ch; lsArea.setText(""); try{ //if(path.length()!=0) // aftp.cd(path); //str = aftp.getResponseString (); //System.out.println(str); tis= aftp.list(); //bt=new byte[tis.available()]; //tis.read (bt); //buf = new String (bt); //lsArea.append(buf); while ((ch=tis.read())>=0){ sb.append((char)ch); } lsArea.append(sb.toString()); tis.close(); } catch(IOException ioe){ ioe.printStackTrace (); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -