Android FTP服务器上传文件攻略(代码详解)
1.前言
在开发中,会遇到向FTP服务器上传文件的需求,首先要导入
commons-net-3.3.jar然后利用api进行相关操作,具体功能如下:
Ftp相关代码
importandroid.util.Log;
importorg.apache.commons.net.ftp.FTP;
importorg.apache.commons.net.ftp.FTPClient;
importorg.apache.commons.net.ftp.FTPReply;
importjava.io.FileInputStream;
publicclassFTPClientUtils{
privatestaticfinalStringTAG="MainActivity";
privateFTPClientftpClient=null;//FTP客户端
/**
*连接到FTP服务器
*
*@paramhostftp服务器域名
*@paramusername访问用户名
*@parampassword访问密码
*@paramport端口
*@return是否连接成功
*/
publicbooleanftpConnect(Stringhost,Stringusername,Stringpassword,intport){
try{
ftpClient=newFTPClient();
ftpClient.connect(host,port);
//根据返回的状态码,判断链接是否建立成功
if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
booleanstatus=ftpClient.login(username,password);
/*
*设置文件传输模式
*避免一些可能会出现的问题,在这里必须要设定文件的传输格式。
*在这里我们使用BINARY_FILE_TYPE来传输文本、图像和压缩文件。
*/
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
returnstatus;
}
}catch(Exceptione){
e.printStackTrace();
}
returnfalse;
}
/**
*断开ftp服务器连接
*
*@return断开结果
*/
publicbooleanftpDisconnect(){
//判断空指针
if(ftpClient==null){
returntrue;
}
//断开ftp服务器连接
try{
ftpClient.logout();
ftpClient.disconnect();
returntrue;
}catch(Exceptione){
e.printStackTrace();
}
returnfalse;
}
/**
*ftp文件上传
*
*@paramsrcFilePath源文件目录
*@paramdesFileName文件名称
*@return文件上传结果
*/
publicbooleanftpUpload(StringsrcFilePath,StringdesFileName){
booleanstatus=false;
try{
FileInputStreamsrcFileStream=newFileInputStream(srcFilePath);
status=ftpClient.storeFile(desFileName,srcFileStream);
srcFileStream.close();
returnstatus;
}catch(Exceptione){
e.printStackTrace();
}
returnstatus;
}
/**
*ftp更改目录
*
*@parampath更改的路径
*@return更改是否成功
*/
publicbooleanftpChangePath(Stringpath){
booleanstatus=false;
try{
status=ftpClient.changeWorkingDirectory(path);
}catch(Exceptione){
e.printStackTrace();
}
returnstatus;
}
}
2.调用api
booleanisConnect=mFtpClient.ftpConnect("服务器host","用户名","密码",21);//默认端口号是21
if(isConnect){
booleanisSuccessful=mFtpClient.ftpUpload("/sdcard/"+folderName+"/"+mPicturename,"/htdocs/pics/"+mPicturename);
if(isSuccessful){
mFtpClient.ftpDisconnect();
//上传成功
}else{
//上传失败
}
}else{
//服务器连接失败
}
附录:自己之前做项目的时候写过的FTP上传代码:
packagecom.kandao.yunbell.videocall;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.io.UnsupportedEncodingException;
importjava.net.SocketException;
importorg.apache.commons.net.ftp.FTP;
importorg.apache.commons.net.ftp.FTPClient;
importorg.apache.commons.net.ftp.FTPReply;
importcom.kandao.yunbell.common.SysApplication;
importandroid.content.Context;
importandroid.util.Log;
publicclassMyUploadThreadextendsThread{
privateStringfileName;//文件名字
privateStringfilePath;//文件本地路径
privateStringfileStoragePath;//文件服务器存储路径
privateStringserverAddress;//服务器地址
privateStringftpUserName;//ftp账号
privateStringftpPassword;//ftp密码
privateContextmContext;
publicMyUploadThread(){
super();
//TODOAuto-generatedconstructorstub
}
publicMyUploadThread(ContextmContext,StringfileName,StringfilePath,
StringfileStoragePath,StringserverAddress,StringftpUserName,StringftpPassword){
super();
this.fileName=fileName;
this.filePath=filePath;
this.fileStoragePath=fileStoragePath;
this.serverAddress=serverAddress;
this.ftpUserName=ftpUserName;
this.ftpPassword=ftpPassword;
this.mContext=mContext;
}
@Override
publicvoidrun(){
super.run();
try{
FileInputStreamfis=null;
FTPClientftpClient=newFTPClient();
String[]idPort=serverAddress.split(":");
ftpClient.connect(idPort[0],Integer.parseInt(idPort[1]));
intreturnCode=ftpClient.getReplyCode();
Log.i("caohai","returnCode,upload:"+returnCode);
booleanloginResult=ftpClient.login(ftpUserName,ftpPassword);
Log.i("caohai","loginResult:"+loginResult);
if(loginResult&&FTPReply.isPositiveCompletion(returnCode)){//如果登录成功
//设置上传目录
if(((SysApplication)mContext).getIsVideo()){
((SysApplication)mContext).setIsVideo(false);
booleanff=ftpClient.changeWorkingDirectory(fileStoragePath+"/video/");
Log.i("caohai","ff:"+ff);
}else{
booleanee=ftpClient.changeWorkingDirectory(fileStoragePath+"/photo/");
Log.i("caohai","ee:"+ee);
}
ftpClient.setBufferSize(1024);
//ftpClient.setControlEncoding("iso-8859-1");
//ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
fis=newFileInputStream(filePath+"/"
+fileName);
Log.i("caohai","fileStoragePath00000:"+fileStoragePath);
String[]path=fileStoragePath.split("visitorRecord");
booleanfs=ftpClient.storeFile(newString((path[1]
+"/photo/"+fileName).getBytes(),"iso-8859-1"),fis);
Log.i("caohai","shifoushangchuanchenggong:"+fs);
fis.close();
ftpClient.logout();
//ftpClient.disconnect();
}else{//如果登录失败
ftpClient.disconnect();
}
}catch(NumberFormatExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(SocketExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(UnsupportedEncodingExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
总结
到此这篇关于AndroidFTP服务器上传文件攻略的文章就介绍到这了,更多相关AndroidFTP服务器上传内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!