java实现FTP
来源:互联网 发布:山长水阔知何处 编辑:程序博客网 时间:2024/05/16 00:30
package ftpclient;import java.awt.*;import java.awt.event.*;import javax.swing.*;import com.borland.jbcl.layout.*;import java.beans.*;import java.io.*;import sun.net.TelnetInputStream;import sun.net.ftp.*;import java.lang.Object;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.DefaultTreeModel;import javax.swing.JTextField;import javax.swing.JCheckBox;import javax.swing.tree.TreePath;import sun.net.TelnetOutputStream;import java.util.Properties;/** * <p>Title: an example</p> * <p>Description:an no</p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: home</p> * @author liujun * @version 1.0 */public class Frame1 extends JFrame { private JPanel contentPane; private JTextField jTextField1 = new JTextField(); private JTextField jTextField2 = new JTextField(); private JTextField jTextField3 = new JTextField(); private JCheckBox jCheckbox1 = new JCheckBox(); FtpClient ftp=null; JTabbedPane jTabbedPane1 = new JTabbedPane(); JPanel jPanel1 = new JPanel(); JPanel jPanel2 = new JPanel(); PaneLayout paneLayout1 = new PaneLayout(); XYLayout xYLayout1 = new XYLayout(); JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JLabel jLabel3 = new JLabel(); JTextField jTextField4 = new JTextField(); JLabel jLabel4 = new JLabel(); BorderLayout borderLayout1 = new BorderLayout(); Box box1; JPanel jPanel3 = new JPanel(); JPanel jPanel4 = new JPanel(); XYLayout xYLayout2 = new XYLayout(); BorderLayout borderLayout2 = new BorderLayout(); JScrollPane jScrollPane1 = new JScrollPane(); JButton jButton3 = new JButton(); JButton jButton4 = new JButton(); JScrollPane jScrollPane2 = new JScrollPane(); //定义树节点,模型和树视图 DefaultMutableTreeNode root2 = new DefaultMutableTreeNode("目录中没有文件"); DefaultTreeModel model2 = new DefaultTreeModel(root2); JTree jTree2 = new JTree(model2); JButton jButton5 = new JButton(); JLabel statusLabel = new JLabel(); List list1 = new List(); JScrollPane jScrollPane3 = new JScrollPane(); JTextArea jTextArea1 = new JTextArea(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); JLabel jLabel5 = new JLabel(); JLabel jLabel6 = new JLabel(); //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(); box1 = Box.createVerticalBox(); jTextField1.setText("192.168.101.2"); contentPane.setLayout(paneLayout1); this.setSize(new Dimension(487, 462)); this.setTitle("文件传输系统"); jTextField2.setText("anonymous"); jTextField3.setText("a"); jCheckbox1.setText("使用匿名"); jCheckbox1.setSelected(true); jPanel1.setLayout(xYLayout1); jLabel1.setText("服务器地址:"); jLabel2.setText("用户名:"); jLabel3.setText("密 码:"); jTextField4.setText("21"); jLabel4.setText("端口号:"); jPanel2.setLayout(borderLayout1); jPanel3.setLayout(xYLayout2); jPanel4.setDebugGraphicsOptions(0); jPanel4.setMinimumSize(new Dimension(160, 18)); jPanel4.setPreferredSize(new Dimension(160, 18)); jPanel4.setLayout(borderLayout2); jButton3.setText("文件上载"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton3_actionPerformed(e); } }); jButton4.setText("文件下载"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton4_actionPerformed(e); } }); box1.setEnabled(true); jPanel3.setDebugGraphicsOptions(0); jPanel3.setMinimumSize(new Dimension(400, 240)); jPanel3.setPreferredSize(new Dimension(400, 240)); jTree2.setToggleClickCount(2); jButton5.setText("选择目录"); jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton5_actionPerformed(e); } }); statusLabel.setText("没有连接ftp服务器"); list1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(MouseEvent e) { list1_mouseClicked(e); } }); jTextArea1.setText(" "); jButton1.setText("连接到服务器"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); jButton2.setText("断开服务器连接"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton2_actionPerformed(e); } }); list1.setMultipleMode(true); jLabel5.setText("^^FTP服务器目录列表^^"); jLabel6.setText("^^本地硬盘目录列表^^"); jPanel1.add(jTextField1, new XYConstraints(81, 11, 297, -1)); jPanel1.add(jLabel1, new XYConstraints(21, 11, -1, -1)); jPanel1.add(statusLabel, new XYConstraints(-2, 370, 484, 29)); jPanel1.add(jButton2, new XYConstraints(34, 304, 122, -1)); jPanel1.add(jButton1, new XYConstraints(34, 268, 106, 23)); jPanel1.add(jLabel4, new XYConstraints(22, 50, -1, -1)); jPanel1.add(jTextField4, new XYConstraints(82, 49, 88, -1)); jPanel1.add(jCheckbox1, new XYConstraints(22, 84, -1, -1)); jPanel1.add(jTextField2, new XYConstraints(80, 124, 86, -1)); jPanel1.add(jLabel2, new XYConstraints(20, 124, 58, 21)); jPanel1.add(jTextField3, new XYConstraints(80, 165, 87, -1)); jPanel1.add(jLabel3, new XYConstraints(20, 166, -1, -1)); jPanel2.add(box1, BorderLayout.CENTER); jPanel3.add(jScrollPane2, new XYConstraints(295, 6, 173, 280)); jPanel3.add(jScrollPane1, new XYConstraints(7, 6, 194, 281)); jPanel3.add(jButton4, new XYConstraints(205, 93, 84, 19)); jPanel3.add(jButton3, new XYConstraints(205, 53, 86, 19)); jPanel3.add(jButton5, new XYConstraints(205, 136, 85, 19)); jPanel3.add(jLabel5, new XYConstraints(16, 293, 144, 22)); jPanel3.add(jLabel6, new XYConstraints(314, 293, 144, 22)); jScrollPane1.getViewport().add(list1, null); jScrollPane2.getViewport().add(jTree2, null); box1.add(jPanel3, null); box1.add(jPanel4, null); jPanel4.add(jScrollPane3, BorderLayout.CENTER); jScrollPane3.getViewport().add(jTextArea1, null); myInit(); jTabbedPane1.add(jPanel2, "文件处理"); jTabbedPane1.add(jPanel1, "连接FTP服务器"); contentPane.add(jTabbedPane1, new PaneConstraints("jTabbedPane1", "jTabbedPane1", PaneConstraints.ROOT, 0.5f)); } private void myInit() { File rootfile= new File("c:\\");//得到根目录文件 if(rootfile.isFile()) rootfile=rootfile.getParentFile();//如果得到的不是目录,则使用他的目录 DefaultMutableTreeNode rootTree2 =new DefaultMutableTreeNode(rootfile.getPath()); setTree(rootfile.getPath(),rootTree2);//遍历目录树 model2.setRoot(rootTree2);//设置模型的根节点 model2.reload();//重新构造树视图 if(ftp!=null) { ReloadList(); jTextArea1.append(ftp.welcomeMsg); } } //---------------------------------------------------------------------------// 浏览程序: private void ReloadList()// 作用:清空目录列表,调用List()方法获取文件列表。 private void ReloadList(){ StringBuffer buf=new StringBuffer(); int ch; //清空目录列表 list1.removeAll(); try { //调用List()方法得到目录表 TelnetInputStream t = ftp.list(); t.setStickyCRLF(true); //分解TelnetInputStream数据流 while ( (ch = t.read()) >= 0) { if (ch == '\n') { //向列表框添加分解得到的目录和文件 list1.add(getDIR(buf.toString())); buf.setLength(0); } else { buf.append( (char) ch); } } //完成后关闭TelnetInputStream数据流 t.close(); } catch (IOException ex) { ex.printStackTrace(); } //刷新列表框的内容 list1.validate(); } //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 String getDIR(String path){ String DIRName; int ch; //分解字符串得到目录和文件名 int begin=55; DIRName=path.substring(begin).trim(); return DIRName; }//---------------------------------------------------------------------------// 浏览程序: public void setTree(String Path,DefaultMutableTreeNode TreePath)// 作用:构造jTree对象的目录树 public void setTree(String Path,DefaultMutableTreeNode TreePath){ //递归遍历目录树 try{ File source = new File(Path);//得到源文件路径 String[] fileName=source.list();//的到该目录下文件列表 for(int i=0;i<fileName.length;i++){//显示选择了的文件列表 File fileMem=new File(source.getPath(),fileName[i]); if(fileMem.isDirectory()){//是目录则添加目录 DefaultMutableTreeNode TreeMem =new DefaultMutableTreeNode(fileMem.getName()); TreePath.add(TreeMem);//在树中添加目录 setTree(fileMem.getPath(),TreeMem);//递归遍历目录树 }//end if else{//是文件则添加节点 DefaultMutableTreeNode TreeMem =new DefaultMutableTreeNode(fileMem.getName()); TreePath.add(TreeMem);//添加节点 } }//end for }//End try catch(Exception ei) { JOptionPane.showMessageDialog(this, "系统出错:"+ei);//显示提示信息。 } } void JCheckbox1_propertyChange(PropertyChangeEvent e) { if(jCheckbox1.isSelected()){ jTextField1.setText("anonymous"); jTextField2.setText("a"); } }//---------------------------------------------------------------------------// 浏览程序: void jButton5_actionPerformed(ActionEvent e)// 作用:选择需要的本地文件目录 void jButton5_actionPerformed(ActionEvent e) { JFileChooser JFileCh = new JFileChooser();//创建文件对话框 JFileCh.setFileSelectionMode(JFileCh.DIRECTORIES_ONLY);//只选择目录 int returnVal = JFileCh.showOpenDialog(this);//显示文件对话框 if(returnVal == JFileChooser.APPROVE_OPTION){ File root2 = JFileCh.getSelectedFile();//得到根目录文件 if(root2.isFile()) root2=root2.getParentFile();//如果得到的不是目录,则使用他的目录 DefaultMutableTreeNode rootTree2 =new DefaultMutableTreeNode(root2.getPath()); setTree(root2.getPath(),rootTree2);//遍历目录树 model2.setRoot(rootTree2);//设置模型的根节点 model2.reload();//重新构造树视图 }//end if else JOptionPane.showMessageDialog(this, "没有选择文件");//显示提示信息。 }//---------------------------------------------------------------------------// 浏览程序:void list1_mouseClicked(MouseEvent e)// 作用:FTP服务器文件目录改变 void list1_mouseClicked(MouseEvent e) { //单击列表框的操作 StringBuffer buf=new StringBuffer(); if(ftp==null) list1.removeAll(); if(list1.getRows()>0){ //选择列表框中单击选中的内容 String dir=list1.getSelectedItem().trim(); try { //如果是目录,则进入目录 ftp.cd(dir); //列表框需要重新刷新 ReloadList(); jTextArea1.append("进入目录: "); jTextArea1.append(dir); jTextArea1.append("\n"); } catch (IOException ex) { //否则,表明点击的内容是ftp上的一个文件 jTextArea1.append(dir); jTextArea1.append(":是一个文件\n"); } } }//---------------------------------------------------------------------------// 登录程序: void jButton1_actionPerformed(ActionEvent e)// 作用:登录连接到FTP服务器。 void jButton1_actionPerformed(ActionEvent e) { int ch; String hostname=jTextField1.getText(); //如果已经打开了FTP服务器,则先关闭FTP文件服务器 try { if (ftp!=null) ftp.closeServer(); } catch (IOException ex) { ex.printStackTrace(); } //连接到服务器 try { statusLabel.setText("正在连接,请等待....."); ftp= new FtpClient(hostname); //登录Ftp服务器 ftp.login(jTextField2.getText(),jTextField3.getText()); //使用二进制协议 ftp.binary(); } catch(FtpLoginException ex){ //没有主机的登录权限 statusLabel.setText("无权限与主机:"+hostname+"连接!"); } catch (IOException ex){ //连接主机失败 statusLabel.setText("连接主机:"+hostname+"失败!"); } catch(SecurityException ex) { //用户或者密码可能不对 statusLabel.setText("用户或者密码可能不对,无权限与主机:"+hostname+"连接!"); } //连接成功后的显示 statusLabel.setText("连接主机:"+hostname+"成功!"); //列表框需要重新刷新 ReloadList(); } void jButton2_actionPerformed(ActionEvent e) { try { //关闭FTP服务器连接 if (ftp!=null) { ftp.closeServer(); list1.removeAll(); } } catch (IOException ex) { ex.printStackTrace(); } statusLabel.setText("断开主机连接成功!"); } void jButton3_actionPerformed(ActionEvent e) { //上载本地文件到ftp服务器中 if(jTree2.isSelectionEmpty() ){ jTextArea1.append("没有选择本地文件上载\n"); }else{ File file1=new File(conPath()); //判断是否是文件,如果是目录则不与操作,以后的程序可能会考虑添加目录的操作 if(file1.isFile()){ String filename=jTree2.getSelectionPath().getLastPathComponent().toString(); int ch; try { File localFile = new File(conPath() ); RandomAccessFile sendFile = new RandomAccessFile( conPath() , "r"); //上载文件到ftp服务器中 sendFile.seek(0); TelnetOutputStream outs = ftp.put(filename); DataOutputStream outputs = new DataOutputStream(outs); //上载处理中...... while (sendFile.getFilePointer() < sendFile.length()) { ch = sendFile.read(); outputs.write(ch); } //上载完成,关闭输入输出流 outs.close(); sendFile.close(); } catch (IOException ex) { ex.printStackTrace(); } } } } void jButton4_actionPerformed(ActionEvent e) { //下载ftp文件到本地目录 if(jTree2.isSelectionEmpty()){ //如果没有选择目录和文件提示用户 jTextArea1.append("没有选择本地目录用于下载!!\n"); }else{ File file1=new File(conPath()); if(file1.isFile()){ //如果是文件也要提示用户选择的是文件 jTextArea1.append("没有选择本地目录用于下载"+jTree2.getSelectionPath().getLastPathComponent().toString()+"\n"); jTextArea1.append("而选择的是文件:"+conPath()+"\n"); }else{ //下载文件到指定的目录里 jTextArea1.append("选择的下载本地目录"+jTree2.getSelectionPath().getLastPathComponent().toString()+"\n"); jTextArea1.append("目录:"+conPath()+"\n"); //可以选择多个文件和目录 String [] filenames=list1.getSelectedItems(); for(int i=0;i<filenames.length;i++){ String filename=filenames[i]; boolean isfile=false; try { //判断是否为目录,如果是目录则在本地创建一个目录 ftp.cd(filename); ftp.cdUp(); File localDir = new File(conPath()+"\\"+filename) ; localDir.mkdir(); } catch (IOException ex) { isfile=true; } if(isfile){ //对文件的处理,即下载文件到本地硬盘中 int ch; try{ File localFile = new File(conPath()+"\\"+filename) ; RandomAccessFile getFile = new RandomAccessFile((conPath()+"\\"+filename),"rw"); getFile.seek(0); TelnetInputStream ins = ftp.get(filename); DataInputStream Inputs = new DataInputStream(ins); //下载处理...... while ((ch = Inputs.read()) >= 0) { getFile.write(ch); } //处理完成,关闭输入输出流 ins.close(); getFile.close(); } catch (IOException ex) { ex.printStackTrace(); } } } } } } String conPath( ){ //用于获取本地目录树中的文件路径 TreePath treePath1=jTree2.getSelectionPath(); String dir1,dir=treePath1.getPathComponent(0).toString(); int i,count=treePath1.getPathCount(); //对于象“c:\”之类的盘符路径需要特殊处理 if((dir.length())==3) dir1=dir.substring(0,2); else dir1=dir; for(i=1;i<count;i++){ dir1=dir1+"\\"+treePath1.getPathComponent(i); } return dir1; }}
0 0
- FTP协议Java实现
- java实现ftp上传
- Java实现FTP服务器
- java 实现FTP上传下载!
- java 实现ftp上传下载
- java 简单实现ftp
- java实现ftp上传下载
- Java实现FTP上传下载
- java 实现ftp上传
- JAVA 实现FTP实例
- JAVA实现FTP上传下载
- Java实现FTP上传下载
- java 实现ftp上传下载
- java实现FTP
- java实现ftp断点续传
- java实现FTP上传下载
- java实现ftp上传下载
- JAVA实现FTP
- Android NDK编译错误处理-- error: undefined reference to '__dso_handle'
- opencv生成高斯核
- 数据库简单备份脚本
- cocos2d-x 3.0开发笔记---Vector<T>深入学习
- Apache: 启用mod_expires为静态资源添加默认过期
- java实现FTP
- 测试c++代码运行时间方法收藏
- Github开源项目
- HttpClient 基础
- 指针强制转换
- 马伊琍父亲对文章出轨表态:放过我们的孩子吧
- jsp和servlet之间的关系+示例入门
- gethostbyname用法
- 【Unity技巧】调整画质(贴图)质量