Android FTP服务器上传文件攻略(代码详解)
程序员文章站
2022-06-20 13:15:13
1.前言在开发中,会遇到向ftp服务器上传文件的需求,首先要导入commons-net-3.3.jar 然后利用api进行相关操作,具体功能如下:ftp相关代码import android.util....
1.前言
在开发中,会遇到向ftp服务器上传文件的需求,首先要导入
commons-net-3.3.jar 然后利用api进行相关操作,具体功能如下:
ftp相关代码
import android.util.log; import org.apache.commons.net.ftp.ftp; import org.apache.commons.net.ftp.ftpclient; import org.apache.commons.net.ftp.ftpreply; import java.io.fileinputstream; public class ftpclientutils { private static final string tag = "mainactivity"; private ftpclient ftpclient = null; // ftp客户端 /** * 连接到ftp服务器 * * @param host ftp服务器域名 * @param username 访问用户名 * @param password 访问密码 * @param port 端口 * @return 是否连接成功 */ public boolean ftpconnect(string host, string username, string password, int port) { try { ftpclient = new ftpclient(); ftpclient.connect(host,port); // 根据返回的状态码,判断链接是否建立成功 if (ftpreply.ispositivecompletion(ftpclient.getreplycode())) { boolean status = ftpclient.login(username, password); /* * 设置文件传输模式 * 避免一些可能会出现的问题,在这里必须要设定文件的传输格式。 * 在这里我们使用binary_file_type来传输文本、图像和压缩文件。 */ ftpclient.setfiletype(ftp.binary_file_type); ftpclient.enterlocalpassivemode(); return status; } } catch (exception e) { e.printstacktrace(); } return false; } /** * 断开ftp服务器连接 * * @return 断开结果 */ public boolean ftpdisconnect() { // 判断空指针 if (ftpclient == null) { return true; } // 断开ftp服务器连接 try { ftpclient.logout(); ftpclient.disconnect(); return true; } catch (exception e) { e.printstacktrace(); } return false; } /** * ftp 文件上传 * * @param srcfilepath 源文件目录 * @param desfilename 文件名称 * @return 文件上传结果 */ public boolean ftpupload(string srcfilepath, string desfilename) { boolean status = false; try { fileinputstream srcfilestream = new fileinputstream(srcfilepath); status = ftpclient.storefile(desfilename, srcfilestream); srcfilestream.close(); return status; } catch (exception e) { e.printstacktrace(); } return status; } /** * ftp 更改目录 * * @param path 更改的路径 * @return 更改是否成功 */ public boolean ftpchangepath(string path) { boolean status = false; try { status = ftpclient.changeworkingdirectory(path); } catch (exception e) { e.printstacktrace(); } return status; } }
2.调用api
boolean isconnect = mftpclient.ftpconnect("服务器host", "用户名", "密码", 21);//默认端口号是21 if (isconnect) { boolean issuccessful = mftpclient.ftpupload("/sdcard/" + foldername + "/" + mpicturename, "/htdocs/pics/" + mpicturename); if (issuccessful) { mftpclient.ftpdisconnect(); //上传成功 } else { //上传失败 } } else { //服务器连接失败 }
附录:自己之前做项目的时候写过的ftp上传代码:
package com.kandao.yunbell.videocall; import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.ioexception; import java.io.unsupportedencodingexception; import java.net.socketexception; import org.apache.commons.net.ftp.ftp; import org.apache.commons.net.ftp.ftpclient; import org.apache.commons.net.ftp.ftpreply; import com.kandao.yunbell.common.sysapplication; import android.content.context; import android.util.log; public class myuploadthread extends thread { private string filename;// 文件名字 private string filepath;// 文件本地路径 private string filestoragepath;// 文件服务器存储路径 private string serveraddress;// 服务器地址 private string ftpusername;// ftp账号 private string ftppassword;// ftp密码 private context mcontext; public myuploadthread() { super(); // todo auto-generated constructor stub } public myuploadthread(context mcontext,string filename, string filepath, string filestoragepath,string serveraddress,string ftpusername,string ftppassword) { super(); this.filename = filename; this.filepath = filepath; this.filestoragepath = filestoragepath; this.serveraddress = serveraddress; this.ftpusername = ftpusername; this.ftppassword = ftppassword; this.mcontext=mcontext; } @override public void run() { super.run(); try { fileinputstream fis=null; ftpclient ftpclient = new ftpclient(); string[] idport = serveraddress.split(":"); ftpclient.connect(idport[0], integer.parseint(idport[1])); int returncode = ftpclient.getreplycode(); log.i("caohai", "returncode,upload:"+returncode); boolean loginresult = ftpclient.login(ftpusername, ftppassword); log.i("caohai", "loginresult:"+loginresult); if (loginresult && ftpreply.ispositivecompletion(returncode)) {// 如果登录成功 // 设置上传目录 if (((sysapplication) mcontext).getisvideo()) { ((sysapplication) mcontext).setisvideo(false); boolean ff=ftpclient.changeworkingdirectory(filestoragepath + "/video/"); log.i("caohai", "ff:"+ff); }else{ boolean ee=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 = new fileinputstream(filepath + "/" + filename); log.i("caohai", "filestoragepath00000:"+filestoragepath); string[] path = filestoragepath.split("visitorrecord"); boolean fs = ftpclient.storefile(new string((path[1] + "/photo/" + filename).getbytes(), "iso-8859-1"), fis); log.i("caohai", "shifoushangchuanchenggong:"+fs); fis.close(); ftpclient.logout(); //ftpclient.disconnect(); } else {// 如果登录失败 ftpclient.disconnect(); } } catch (numberformatexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (socketexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (unsupportedencodingexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
总结
到此这篇关于android ftp服务器上传文件攻略的文章就介绍到这了,更多相关android ftp服务器上传内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 芒果怎样催熟,你都了解吗