zoukankan      html  css  js  c++  java
  • (转)【Java FTP及FTP服务器搭建】

    转至 http://blog.csdn.net/studyvcmfc/article/details/8147052

    目录(?)[+]

    -【Java FTP及FTP服务器搭建】


    一:本文采用apache项目组的

    Apache Commons Net™ library


    项目地址:http://commons.apache.org/net/


    如下图:可见FTP只是其中一个支持的协议,还有很多其他,如有需要的同学,可参考官方网站。

    Features

    Supported protocols include:

    • FTP/FTPS
    • FTP over HTTP (experimental)
    • NNTP
    • SMTP(S)
    • POP3(S)
    • IMAP(S)
    • Telnet
    • TFTP
    • Finger
    • Whois
    • rexec/rcmd/rlogin
    • Time (rdate) and Daytime
    • Echo
    • Discard
    • NTP/SNTP

    二:搭建ftp服务器


    1:下载filezilla

    http://filezilla-project.org/


    如图


    2:安装到windows


    双击,下一步,完成!


    3:启动ftp服务器

    双击桌面图标,输入PC的密码

    登录成功


    4:ftp添加 一个用户,并设置共享文件夹




    5:测试


    简单的ftp server完成。


    三:Java代码


    FTPClientFTPClient encapsulates all the functionality necessary to store and retrieve files from an FTP server.


    上传:

    1. public class MyFtp {  
    2.   
    3.     public static void main(String[] args) {  
    4.           
    5.         try {  
    6.               
    7.             FTPClient ftp = new FTPClient();  
    8.               
    9.             ftp.connect("127.0.0.1", 21);  
    10.               
    11.             boolean isLogin = ftp.login("a", "a");  
    12.               
    13.             System.out.println("登录:"+isLogin);  
    14.               
    15.             ftp.setFileType(FTP.BINARY_FILE_TYPE);  
    16.               
    17.             boolean isStore = ftp.storeFile("note.txt", new FileInputStream("d:/note.txt"));  
    18.               
    19.             ftp.storeFile("1.png", new FileInputStream("d:/1.png"));  
    20.               
    21.             System.out.println("上传:"+isStore);  
    22.               
    23.         } catch (Exception e) {  
    24.             e.printStackTrace();  
    25.         }  
    26.           
    27.           
    28.     }  
    29. }  
    1. public class MyFtp {  
    2.   
    3.     public static void main(String[] args) {  
    4.           
    5.         try {  
    6.               
    7.             FTPClient ftp = new FTPClient();  
    8.               
    9.             ftp.connect("127.0.0.1", 21);  
    10.               
    11.             boolean isLogin = ftp.login("a", "a");  
    12.               
    13.             System.out.println("登录:"+isLogin);  
    14.               
    15.             ftp.setFileType(FTP.BINARY_FILE_TYPE);  
    16.               
    17.             boolean isStore = ftp.storeFile("note.txt", new FileInputStream("d:/note.txt"));  
    18.               
    19.             ftp.storeFile("1.png", new FileInputStream("d:/1.png"));  
    20.               
    21.             System.out.println("上传:"+isStore);  
    22.               
    23.         } catch (Exception e) {  
    24.             e.printStackTrace();  
    25.         }  
    26.           
    27.           
    28.     }  
    29. }  

    下载:


    1. public class MyFtp {  
    2.   
    3.     public static void main(String[] args) {  
    4.           
    5.         try {  
    6.               
    7.             FTPClient ftp = new FTPClient();  
    8.               
    9.             ftp.connect("127.0.0.1", 21);  
    10.               
    11.             boolean isLogin = ftp.login("a", "a");  
    12.               
    13.             System.out.println("登录:"+isLogin);  
    14.               
    15. //          ftp.setFileType(FTP.BINARY_FILE_TYPE);   
    16. //             
    17. //          boolean isStore = ftp.storeFile("note.txt", new FileInputStream("d:/note.txt"));   
    18. //             
    19. //          ftp.storeFile("1.png", new FileInputStream("d:/1.png"));   
    20. //             
    21. //          System.out.println("上传:"+isStore);   
    22.               
    23.             boolean isDown = ftp.retrieveFile("note.txt", new FileOutputStream("d:/TDDOWNLOAD/note.txt"));  
    24.             isDown = ftp.retrieveFile("1.png", new FileOutputStream("d:/TDDOWNLOAD/note.png"));  
    25.             System.out.println("下载:"+isDown);  
    26.         } catch (Exception e) {  
    27.             e.printStackTrace();  
    28.         }  
    29.           
    30.           
    31.     }  
  • 相关阅读:
    SOAP webserivce 和 RESTful webservice 对比及区别(转载)
    JavaWeb工程中web.xml基本配置(转载学习)
    iframe 自适应
    SQL分组求每组最大值问题的解决方法收集 (转载)
    关于试用jquery的jsonp实现ajax跨域请求数据的问题
    解决Mysql连接池被关闭 ,hibernate尝试连接不能连接的问题。 (默认mysql连接池可以访问的时间为8小时,如果超过8小时没有连接,mysql会自动关闭连接池。系统发布第二天访问链接关闭问题。
    Hadoop编译源码(面试重点)
    Hadoop学习(二)自己编译Hadoop安装包
    代理模式实现方式及优缺点对比
    zookeeper
  • 原文地址:https://www.cnblogs.com/s648667069/p/6369492.html
Copyright © 2011-2022 走看看