• 使用JFTP上传下载


    JFTP是一个用JAVA写的FTP客户端程序。功能强大不仅支持FTP,还支持其它协议如SMB, SFTP, NFS, HTTP等。在传输文件的同时还可以浏览FTP服务器上的资源,也可以浏览局域网上的Windows共享资源等
    由于项目需要用到ftp的类库,比较了很多觉得JFTP不错,简单实用。

    上传:

    java 代码
     
    1. import net.sf.jftp.net.ConnectionHandler;  
    2. import net.sf.jftp.net.ConnectionListener;  
    3. import net.sf.jftp.net.DataConnection;  
    4. import net.sf.jftp.net.FtpConnection;  
    5. import net.sf.jftp.net.BasicConnection;  
    6. import net.sf.jftp.config.Settings;  
    7.   
    8. import java.io.*;  
    9.   
    10. import org.apache.commons.lang.StringUtils;  
    11.   
    12. /** 
    13. * See FtpDownload.java for comments. 
    14. */  
    15. public class FtpUpload implements ConnectionListener  
    16. {  
    17.       
    18.     private boolean isThere = false;  
    19.       
    20.     private ConnectionHandler handler = new ConnectionHandler();  
    21.       
    22.     private String host;  
    23.     private int port = 21;  
    24.     private String user;  
    25.     private String passwd;  
    26.       
    27.     public FtpUpload(String host, String user, String passwd){  
    28.         this.host = host;  
    29.         this.user = user;  
    30.         this.passwd = passwd;  
    31.     }  
    32.       
    33.     public FtpUpload(String host, int port, String user, String passwd){  
    34.         this.host = host;  
    35.         this.port = port;  
    36.         this.user = user;  
    37.         this.passwd = passwd;  
    38.     }  
    39.       
    40.     public int upload(String dir, String file){  
    41.         FtpConnection con = new FtpConnection(host, port, "/");  
    42.            
    43.         con.addConnectionListener(this);  
    44.            
    45.         con.setConnectionHandler(handler);  
    46.            
    47.         con.login(user, passwd);  
    48.            
    49.         while(!isThere)  
    50.         {  
    51.             try { Thread.sleep(10); }  
    52.             catch(Exception ex) { ex.printStackTrace(); }  
    53.         }  
    54.           
    55.         //make dirs  
    56.         String path = "";  
    57.         String[] paths = StringUtils.split(dir, "/");  
    58.         for(int i = 0; i < paths.length; i++){  
    59.             path += "/" + paths[i];  
    60.             if(!con.chdir(path)){ con.mkdir(path); }  
    61.         }  
    62.         con.chdir(dir);  
    63.         return con.upload(file);  
    64.     }  
    65.     public static void main(String argv[])  
    66.     {  
    67.         if(argv.length == 3)  
    68.         {   
    69.             FtpUpload f = new FtpUpload(argv[0], argv[2], argv[1]);   
    70.         }  
    71.         else   
    72.         {  
    73.             FtpUpload g =   
    74.                 new FtpUpload("192.168.1.10"2009"test","test");  
    75.             g.upload("/""C:/test.jpg");  
    76.         }  
    77.     }  
    78.       
    79.       
    80.     public void updateRemoteDirectory(BasicConnection con)  
    81.     {  
    82.         System.out.println("new path is: " + con.getPWD());  
    83.     }  
    84.        
    85.     public void connectionInitialized(BasicConnection con)  
    86.     {  
    87.         isThere = true;  
    88.     }  
    89.        
    90.     public void updateProgress(String file, String type, long bytes) {}  
    91.        
    92.     public void connectionFailed(BasicConnection con, String why) {System.out.println("connection failed!");}  
    93.       
    94.     public void actionFinished(BasicConnection con) {}  
    95. }  



    下载:

    java 代码
     
    1. import net.sf.jftp.net.ConnectionHandler;  
    2. import net.sf.jftp.net.ConnectionListener;  
    3. import net.sf.jftp.net.DataConnection;  
    4. import net.sf.jftp.net.FtpConnection;  
    5. import net.sf.jftp.net.BasicConnection;  
    6. import net.sf.jftp.config.Settings;  
    7.   
    8. import java.io.*;  
    9.   
    10. public class FtpDownload implements ConnectionListener  
    11. {  
    12.     // is the connection established?  
    13.     private boolean isThere = false;  
    14.       
    15.     public static long time = 0;  
    16.       
    17.     // connection pool, not necessary but you should take a look at this class  
    18.     // if you want to use multiple event based ftp transfers.  
    19.     private ConnectionHandler handler = new ConnectionHandler();  
    20.   
    21.     private String host;  
    22.     private int port = 21;  
    23.     private String user;  
    24.     private String passwd;  
    25.       
    26.     public FtpDownload(String host, int port, String user, String passwd){  
    27.         this.host = host;  
    28.         this.port = port;  
    29.         this.user = user;  
    30.         this.passwd = passwd;  
    31.     }  
    32.       
    33.     //creates a FtpConnection and downloads a file  
    34.     public byte[] downloadToBinary(String file)  
    35.     {  
    36.         // the ftp client default is very small, you may want to increase this  
    37.         Settings.bufferSize = 16384;   
    38.           
    39.         long current = System.currentTimeMillis();  
    40.         //System.out.println("1) "+(System.currentTimeMillis()-current)+"ms.");  
    41.           
    42.         // create a FtpConnection - note that it does *not* connect instantly  
    43.         FtpConnection con = new FtpConnection(host);  
    44.   
    45.         // set updatelistener, interface methods are below  
    46.         con.addConnectionListener(this);  
    47.           
    48.         // set handler  
    49.         con.setConnectionHandler(handler);  
    50.           
    51.         // connect and login. this is from where connectionFailed() may be called for example  
    52.         con.login(user, passwd);  
    53.           
    54.         // login calls connectionInitialized() below which sets isThere to true  
    55.         while(!isThere)  
    56.         {  
    57.             try { Thread.sleep(10); }  
    58.             catch(Exception ex) { ex.printStackTrace(); }  
    59.         }  
    60.   
    61.         // get download input stream  
    62.         byte[] bytes = null;  
    63.         try{  
    64.             InputStream is =  con.getDownloadInputStream(file);  
    65.             ByteArrayOutputStream bais = new ByteArrayOutputStream();  
    66.             int bit = 0;  
    67.             while((bit = is.read()) != -1){  
    68.                 bais.write(bit);  
    69.             }  
    70.             bytes = bais.toByteArray();  
    71.         }catch(Exception e){}  
    72.         time = (System.currentTimeMillis()-current);  
    73.       
    74.         System.out.println("Download took "+time+"ms.");  
    75.           
    76.         return bytes;  
    77.     }  
    78.   
    79.     // download welcome.msg from sourceforge or any other given file  
    80.     public static void main(String argv[])  
    81.     {  
    82.         FtpDownload f = new FtpDownload("192.168.1.10", 2009"test","test");  
    83.         byte[] bs = f.downloadToBinary("/aaa.jpg");  
    84.     }  
    85.   
    86. // ------------------ needed by ConnectionListener interface -----------------  
    87.   
    88.     // called if the remote directory has changed  
    89.     public void updateRemoteDirectory(BasicConnection con)  
    90.     {  
    91.         System.out.println("new path is: " + con.getPWD());  
    92.     }  
    93.   
    94.     // called if a connection has been established  
    95.     public void connectionInitialized(BasicConnection con)  
    96.     {  
    97.         isThere = true;  
    98.     }  
    99.    
    100.     // called every few kb by DataConnection during the trnsfer (interval can be changed in Settings)  
    101.     public void updateProgress(String file, String type, long bytes) {}  
    102.   
    103.     // called if connection fails  
    104.     public void connectionFailed(BasicConnection con, String why) {System.out.println("connection failed!");}  
    105.   
    106.     // up- or download has finished  
    107.     public void actionFinished(BasicConnection con) {}  
    108. }  




    顺便宣传一下我们的项目:
    www.youmonitor.us
    提供免费网站监测服务,有兴趣的朋友可以去看看。

    • j-ftp-1.50.tar.gz (3.6 MB)
    • 描述: 如果jar包里的log4j与你自己的有冲突,可以直接把jar包里删掉或者下载我改好的
    • 下载次数: 915
  • 相关阅读:
    Android TextView 跑马灯效果
    Android Studio 3.1.3正式版的新坑。。。
    日常 2018年6月11日21:07:22 周一
    Android Spinner 设置setOnItemSelectedListener时,竟会默认触发一次事件!
    String类的简要概述(1)
    Scanner(键盘录入)
    如何保存或读取数据(到android的data目录)利用context获取常见目录可优化代码
    测试修改的概念
    java object
    Rendering Problems Failed to load platform rendering library 为何打开布局页面时手机预览页面显示不出来?
  • 原文地址:https://www.cnblogs.com/chen1987lei/p/1867671.html
Copyright © 2020-2023  润新知