1.确定ftp文件夹

 /** FTP文件夹 **/
private String ftpPath = "hshscrenncap" + "/" + DateUtils.convertDateToShortString(new Date());

2.连接服务器

   public static FTPClient ftpClient;

     // server:服务器名字
// user:用户名
// password:密码
// path:服务器上的路径
public static boolean connectServer(String ip, String user,
String password) {
if(ip!=null){
ip = ip.trim();
}
if(user!=null){
user = user.trim();
}
if(password!=null){
password = password.trim();
}
boolean is_connected;
try {
ftpClient = new FTPClient();
//连接服务器
try {
ftpClient.connect(ip);
} catch (UnknownHostException ex) {
throw new IOException("不能找到FTP服务:" + ip + "'");
}
//在连接尝试检查响应.
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
disconnect();
throw new IOException("不能连接到服务':" + ip + "'");
}
//登录.
if (!ftpClient.login(user, password)) {
is_connected = false;
disconnect();
throw new IOException("不能登录到FTP服务:'" + ip + "'");
} else {
is_connected = true;
}
Log.i(Constants.LOG_TAG, "ftp连接成功 result=" + is_connected);
} catch (IOException e) {
Log.i(Constants.LOG_TAG, "连接ftp服务器出错:" + e.getMessage());
is_connected = false;
return is_connected;
}
return is_connected;
}

3.判断ftp路径是否存在,没有则创建;

 /**
* 检查文件夹是否存在
*
* @param dir
* @param ftpClient
* @return
*/
private static Boolean isDirExist(String dir, FTPClient ftpClient) {
try {
return ftpClient.changeWorkingDirectory(dir);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
   /**
* 创建ftp文件
*/
public static boolean createFtpFile(String ip,
String userName, String userPassword, String path) { if (null != path) {
path = path.trim();
} boolean result = connectServer(ip, userName, userPassword);
if (!result) {
return result;
}
try {
ftpClient.enterLocalPassiveMode();
path = new String(path.getBytes("GBK"), "ISO-8859-1");
//通过远程命令 创建一个文件夹
if(isDirExist(path, ftpClient)){
return true;
}
boolean flag = ftpClient.makeDirectory(path);
if (flag) {
Log.i(Constants.LOG_TAG, "创建文件 " + path + " 成功!");
} else {
Log.i(Constants.LOG_TAG, "创建ftp文件失败," + path + " 文件不存在!");
}
return flag;
} catch (UnsupportedEncodingException e) {
Log.i(Constants.LOG_TAG, "创建ftp文件编码出错:" + e.getMessage());
return false;
} catch (IOException e) {
Log.i(Constants.LOG_TAG, "创建ftp文件出错出错:" + e.getMessage());
return false;
} finally {
if (null != ftpClient && ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (Exception e) {
Log.i(Constants.LOG_TAG, "关闭ftp连接出错:" + e.getMessage());
}
}
}
}

上传图片文件:

/**
* localFile 本地存储文件夹路径
* ip
* userName
* userPassword
* path ftp文件夹路径
**/
public static String uploadFileByApacheByBinary(String localFile,
String ip, String userName, String userPassword, String path) {
String resultMsg = "";
if (null != ip) {
path = path.trim();
}
//检查是否存在目录
createFtpFile(Constants.WS_FTP_IP
, Constants.WS_FTP_ACCOUNTNAME
, Constants.WS_FTP_ACCOUNTPASSWORD, path);
boolean result = connectServer(ip, userName, userPassword);
if (!result) {
resultMsg = Constants.HTTP_INTERVAL_ERROR_STATUS;
return resultMsg;
} else {
ftpClient.setBufferSize(1024);
//每次数据连接之前,FTP client告诉FTP server开通一个端口来传输数据。
// Use passive mode to pass firewalls.
ftpClient.enterLocalPassiveMode();
FileInputStream fis = null;
try {
File file = new File(localFile);
if(!file.exists()){
resultMsg = Constants.HTTP_ILLEGAL_ARGUMENT_STATUS;
return resultMsg;
}
fis = new FileInputStream(file);
String fileName = file.getName();
ftpClient.changeWorkingDirectory(path);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
Log.i(Constants.LOG_TAG, "upload--------begin");
boolean flag = ftpClient.storeFile(new String(fileName.getBytes("GBK"),
"iso-8859-1"), fis);
if (flag) {
Log.i(Constants.LOG_TAG, "ftp上传成功!");
resultMsg = Constants.HTTP_SUCCESS_STATUS;
} else {
Log.i(Constants.LOG_TAG, "ftp上传失败!");
resultMsg = Constants.HTTP_ERROR_STATUS;
}
fis.close();
return resultMsg;
}catch (Exception e) {
Log.i(Constants.LOG_TAG, "上传ftp文件时出错:" + e.getMessage());
resultMsg = Constants.HTTP_INTERVAL_ERROR_STATUS;
return resultMsg;
} finally {
if (null != ftpClient && ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (Exception e) {
Log.i(Constants.LOG_TAG, "关闭ftp连接出错:" + e.getMessage());
}
}
}
}
}

Android ftp 上传图片的更多相关文章

  1. nginx+memcached+ftp上传图片+iis

    nginx+memcached+ftp上传图片+iis 自毕业以来,一直在现在公司做订餐系统的开发,那会儿没有口碑,没有饿了么,更别说美团外卖,百度外卖了...因为规模都比较小,都是一个服务器包含数据 ...

  2. Android端上传图片到后台,存储到数据库中 详细代码

    首先点击头像弹出popwindow,点击相册,相机,调用手机自带的裁剪功能,然后异步任务类访问服务器,上传头像,保存到数据库中, 下面写出popwindow的代码 //设置popwindow publ ...

  3. Java乔晓松-android中上传图片到服务器Tomcat(Struts2)

    在做android开发的时候,有时你会用到图片的上传功能,在我的android项目中,我是选中图片,点击上传多张图片 android客户端上传图片部分的代码如下: package com.exampl ...

  4. 终于等到你---订餐系统之负载均衡(nginx+memcached+ftp上传图片+iis)

    又见毕业 对面工商大学的毕业生叕在拍毕业照了,一个个脸上都挂满了笑容,也许是满意自己四年的修行,也许是期待步入繁华的社会... 恰逢其时的连绵细雨与满天柳絮,似乎也是在映衬他们心中那些离别的忧伤,与对 ...

  5. 通过android 客户端上传图片到服务器

    昨天,(在我的上一篇博客中)写了通过浏览器上传图片到服务器(php),今天将这个功能付诸实践.(还完善了服务端的代码) 不试不知道,原来通过android 向服务端发送图片还真是挺麻烦的一件事. 上传 ...

  6. android 开发上传图片遇到返回 FileNotFoundException

    目的:带有参数上传图片 环境:客户端开发 android studio  ,服务器端:visual studio 2012 (c# asp.net) ************************* ...

  7. android ftp案例分析

    使用方法: FTPClient client = new FTPClient(); client.connect("ftp.host.com", 8021); client.log ...

  8. AlarmManager的使用和七牛云android SDK上传图片

    一学期的课程设计又开始了,虽然以后不搞安卓,但是课设还是想好好完成的,因为之前做过地图开发,所以选了一个跟 这个相关的题目,其实有一个校车系统也可以选,但是之前做过一个相似度接近80%的东西,不想混混 ...

  9. android一个上传图片的样例,包含怎样终止上传过程,假设在上传的时候更新进度条(一)

    先上效果图: Layout为: <? xml version="1.0" encoding="utf-8"?> <LinearLayout x ...

随机推荐

  1. Jmeter测试JDBC

    Datebase Driver class Database URL MySQL com.mysql.jdbc.Driver jdbc:mysql://host:port/{dbname} Postg ...

  2. select count(*)和select count(1)哪个性能高

    select count(*).count(数字).count(字段名)在相同的条件下是没有性能差别的,一般我们在统计行数的时候都会把NULL值统计在内的,所以这样的话,最好就是使用COUNT(*) ...

  3. react.js 多个组件集成示例

    这个看得有点懵, 可能要结合其它实例看. html <!DOCTYPE html> <html> <head> <script src="http: ...

  4. 攻城狮在路上(叁)Linux(二十三)--- linux磁盘参数修改(设备代码、设备名)

    一.mknod:设置设备代码 linux中,所有的设备都是用文件来表示,文件通过major与minor数值来判断. major为主设备代码,minor为设备代码(需要查询),示例如下: /dev/hd ...

  5. hdu 4007 暴力or线段树 ***

    尼玛,INF不能定义太大,找标程对拍了好久 #include<cstdio> #include<iostream> #include<algorithm> #inc ...

  6. shell判断文件是否存在

    转自:http://www.cnblogs.com/sunyubo/archive/2011/10/17/2282047.html 1. shell判断文件,目录是否存在或者具有权限 2. #!/bi ...

  7. Arduino101学习笔记(四)—— 数字IO

    1.设置管脚模式 //***************************************************************************************** ...

  8. Quartz框架简介

    一.Quartz概述: Quartz是一个完全由Java编写的开源任务调度的框架,通过触发器设置作业定时运行规则,控制作业的运行时间.其中quartz集群通过故障切换和负载平衡的功能,能给调度器带来高 ...

  9. hdu5642 数位dp

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5642 题意:一个长度为n的序列,合法序列为字符中不能出现长度大于3的连续相等的字符,求一共 ...

  10. SPOJ SUBST1 后缀数组

    题目链接:http://www.spoj.com/problems/SUBST1/en/ 题意:给定一个字符串,求不相同的子串个数. 思路:直接根据09年oi论文<<后缀数组——出来字符串 ...