Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1395162
  • 博文数量: 188
  • 博客积分: 1784
  • 博客等级: 上尉
  • 技术积分: 2772
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-05 22:20
个人简介

发上等愿,结中等缘,享下等福;择高处立,就平处坐,向宽处行。

文章分类

全部博文(188)

文章存档

2020年(12)

2019年(11)

2018年(4)

2017年(3)

2016年(11)

2015年(22)

2014年(19)

2013年(25)

2012年(32)

2011年(49)

分类: 嵌入式

2012-04-12 11:47:11

  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. }
复制代码
这是我的ftp上传下载的代码,在java程序中完全正常使用,但移到模拟器上,只能上传不能下载了,请大侠们给我看看,呵呵
WARN/System.err(317): org.apache.commons.net.io.CopyStreamException: IOException caught while copying.
10-25 06:19:26.485: WARN/System.err(317):     at org.apache.commons.net.io.Util.copyStream(Util.java:129)
10-25 06:19:26.485: WARN/System.err(317):     at org.apache.commons.net.ftp.FTPClient.retrieveFile(FTPClient.java:1286)
报的错误如下!
阅读(3603) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~