FTP Uploading files to Android

Source: Internet
Author: User

Android client implementation of FTP files (including pictures) upload should be no difficulty. Write it down just to record, hope can help the novice.

A commons-net-3.0.1.jar is required and a jar package is attached behind it.

Directly on the code:

/**  * 通过ftp上传文件  * @param url ftp服务器地址 如: 192.168.1.110  * @param port 端口如 : 21  * @param username  登录名  * @param password   密码  * @param remotePath  上到ftp服务器的磁盘路径  * @param fileNamePath  要上传的文件路径  * @param fileName      要上传的文件名  * @return  */public String ftpUpload(String url, String port, String username,String password, String remotePath, String fileNamePath,String fileName) {   FTPClient ftpClient = newFTPClient();   FileInputStream fis = null String returnMessage = "0" try      ftpClient.connect(url, Integer.parseInt(port));       booleanloginResult = ftpClient.login(username, password);       intreturnCode = ftpClient.getReplyCode();       if(loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登录成功           ftpClient.makeDirectory(remotePath);           // 设置上传目录           ftpClient.changeWorkingDirectory(remotePath);           ftpClient.setBufferSize(1024);           ftpClient.setControlEncoding("UTF-8");           ftpClient.enterLocalPassiveMode();                   fis = newFileInputStream(fileNamePath + fileName);           ftpClient.storeFile(fileName, fis);                      returnMessage = "1";   //上传成功             } else{// 如果登录失败           returnMessage = "0"                           } catch(IOException e) {       e.printStackTrace();       thrownewRuntimeException("FTP客户端出错!", e);   } finally     //IOUtils.closeQuietly(fis);   try     ftpClient.disconnect();   } catch(IOException e) {          e.printStackTrace();          thrownewRuntimeException("关闭FTP连接发生异常!", e);        returnreturnMessage; 

Jar Package Address: http://download.csdn.net/detail/baolong47/4096104

Ext.: http://blog.csdn.net/baolong47/article/details/7301394

Http://www.cnblogs.com/gzggyy/archive/2012/10/16/2725928.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.