android ftp下载代码,android ftp 下载问题

package lemote.test.mid.util;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import lemote.test.mid.properties.DefaultValue;

import org.apache.commons.net.ftp.FTP;

import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.net.ftp.FTPClientConfig;

import org.apache.commons.net.ftp.FTPReply;

import android.os.Environment;

public class FtpUnit {

private FTPClient ftpClient = null;

private String SDPATH;

public FtpUnit(){

SDPATH =Environment.getExternalStorageDirectory()+"/";

}

/**

* 连接Ftp服务器

*/

public  void connectServer(){

if(ftpClient == null){

int reply;

try{

ftpClient = new FTPClient();

ftpClient.setDefaultPort(21);

ftpClient.configure(getFtpConfig());

ftpClient.connect("172.16.18.175");

ftpClient.login("anonymous","");

ftpClient.setDefaultPort(21);

reply = ftpClient.getReplyCode();

System.out.println(reply+"----");

if (!FTPReply.isPositiveCompletion(reply)) {

ftpClient.disconnect();

System.err.println("FTP server refused connection.");

}

ftpClient.enterLocalPassiveMode();

ftpClient.setControlEncoding("gbk");

}catch(Exception e){

e.printStackTrace();

}

}

}

/**

* 上传文件

* @param localFilePath--本地文件路径

* @param newFileName--新的文件名

*/

public void uploadFile(String localFilePath,String newFileName){

connectServer();

//上传文件

BufferedInputStream buffIn=null;

try{

buffIn=new BufferedInputStream(new FileInputStream(SDPATH+"/"+localFilePath));

System.out.println(SDPATH+"/"+localFilePath);

System.out.println("start="+System.currentTimeMillis());

ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

ftpClient.storeFile("a1.mp3", buffIn);

System.out.println("end="+System.currentTimeMillis());

}catch(Exception e){

e.printStackTrace();

}finally{

try{

if(buffIn!=null)

buffIn.close();

}catch(Exception e){

e.printStackTrace();

}

}

}

/**

* 下载文件

* @param remoteFileName --服务器上的文件名

* @param localFileName--本地文件名

*/

public  void loadFile(String remoteFileName,String localFileName){

connectServer();

System.out.println("==============="+localFileName);

//下载文件

BufferedOutputStream buffOut=null;

try{

buffOut=new BufferedOutputStream(new FileOutputStream(SDPATH+localFileName));

long start = System.currentTimeMillis();

ftpClient.retrieveFile(remoteFileName, buffOut);

long end = System.currentTimeMillis();

System.out.println(end-start);

}catch(Exception e){

e.printStackTrace();

}finally{

try{

if(buffOut!=null)

buffOut.close();

}catch(Exception e){

e.printStackTrace();

}

}

}

/**

* 设置FTP客服端的配置--一般可以不设置

* @return

*/

private static FTPClientConfig getFtpConfig(){

FTPClientConfig ftpConfig=new FTPClientConfig(FTPClientConfig.SYST_UNIX);

ftpConfig.setServerLanguageCode(FTP.DEFAULT_CONTROL_ENCODING);

return ftpConfig;

}

/**

* 关闭连接

*/

public  void closeConnect(){

try{

if(ftpClient!=null){

ftpClient.logout();

ftpClient.disconnect();

}

}catch(Exception e){

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值