ftp 上傳下載

  1. package lemote.test.mid.util;

  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;

  6. import lemote.test.mid.properties.DefaultValue;

  7. import org.apache.commons.net.ftp.FTP;
  8. import org.apache.commons.net.ftp.FTPClient;
  9. import org.apache.commons.net.ftp.FTPClientConfig;
  10. import org.apache.commons.net.ftp.FTPReply;

  11. import android.os.Environment;

  12. public class FtpUnit {
  13.         private FTPClient ftpClient = null;
  14.         private String SDPATH;
  15.         public FtpUnit(){
  16.                 SDPATH =Environment.getExternalStorageDirectory()+"/";
  17.         }
  18.         
  19.         /**
  20.          * 鏈接Ftp服務器
  21.          */
  22.         public  void connectServer(){
  23.                 if(ftpClient == null){
  24.                         int reply;
  25.                         try{
  26.                                 ftpClient = new FTPClient();
  27.                                 ftpClient.setDefaultPort(21);
  28.                                 ftpClient.configure(getFtpConfig());
  29.                                 ftpClient.connect("172.16.18.175");
  30.                                 ftpClient.login("anonymous","");
  31.                                 ftpClient.setDefaultPort(21);                                
  32.                                 reply = ftpClient.getReplyCode();
  33.                                 System.out.println(reply+"----");
  34.                 if (!FTPReply.isPositiveCompletion(reply)) {
  35.                      ftpClient.disconnect();
  36.                      System.err.println("FTP server refused connection.");
  37.                  }
  38.                 ftpClient.enterLocalPassiveMode();
  39.                 ftpClient.setControlEncoding("gbk");
  40.                         }catch(Exception e){
  41.                                 e.printStackTrace();
  42.                         }
  43.                 }
  44.         }
  45.         
  46.         /**
  47.      * 上傳文件
  48.      * @param localFilePath--本地文件路徑
  49.      * @param newFileName--新的文件名
  50.     */
  51.    public void uploadFile(String localFilePath,String newFileName){
  52.         connectServer();
  53.        //上傳文件
  54.         BufferedInputStream buffIn=null;
  55.        try{
  56.             buffIn=new BufferedInputStream(new FileInputStream(SDPATH+"/"+localFilePath));
  57.             System.out.println(SDPATH+"/"+localFilePath);
  58.             System.out.println("start="+System.currentTimeMillis());
  59.             ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
  60.             ftpClient.storeFile("a1.mp3", buffIn);
  61.             System.out.println("end="+System.currentTimeMillis());
  62.         }catch(Exception e){
  63.             e.printStackTrace();
  64.         }finally{
  65.            try{
  66.                if(buffIn!=null)
  67.                     buffIn.close();
  68.             }catch(Exception e){
  69.                 e.printStackTrace();
  70.             }
  71.         }
  72.     }
  73.    
  74.    /**
  75.     * 下載文件
  76.     * @param remoteFileName --服務器上的文件名
  77.     * @param localFileName--本地文件名
  78.    */
  79.   public  void loadFile(String remoteFileName,String localFileName){
  80.        connectServer();
  81.        System.out.println("==============="+localFileName);
  82.       //下載文件
  83.        BufferedOutputStream buffOut=null;
  84.       try{
  85.            buffOut=new BufferedOutputStream(new FileOutputStream(SDPATH+localFileName));
  86.            long start = System.currentTimeMillis();
  87.            ftpClient.retrieveFile(remoteFileName, buffOut);
  88.            long end = System.currentTimeMillis();
  89.            System.out.println(end-start);
  90.        }catch(Exception e){
  91.            e.printStackTrace();
  92.        }finally{
  93.           try{
  94.               if(buffOut!=null)
  95.                    buffOut.close();
  96.            }catch(Exception e){
  97.                e.printStackTrace();
  98.            }
  99.        }
  100.    }
  101.   
  102.         /**
  103.      * 設置FTP客服端的配置--通常能夠不設置
  104.      * @return
  105.      */
  106.    private static FTPClientConfig getFtpConfig(){
  107.         FTPClientConfig ftpConfig=new FTPClientConfig(FTPClientConfig.SYST_UNIX);
  108.         ftpConfig.setServerLanguageCode(FTP.DEFAULT_CONTROL_ENCODING);
  109.        return ftpConfig;
  110.     }
  111.    
  112.    /**
  113.     * 關閉鏈接
  114.    */
  115.   public  void closeConnect(){
  116.       try{
  117.           if(ftpClient!=null){
  118.                ftpClient.logout();
  119.                ftpClient.disconnect();
  120.            }
  121.        }catch(Exception e){
  122.            e.printStackTrace();
  123.        }
  124.    }
  125.   
  126. }
相關文章
相關標籤/搜索