Android ftp 上传图片
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 上传图片的更多相关文章
- nginx+memcached+ftp上传图片+iis
nginx+memcached+ftp上传图片+iis 自毕业以来,一直在现在公司做订餐系统的开发,那会儿没有口碑,没有饿了么,更别说美团外卖,百度外卖了...因为规模都比较小,都是一个服务器包含数据 ...
- Android端上传图片到后台,存储到数据库中 详细代码
首先点击头像弹出popwindow,点击相册,相机,调用手机自带的裁剪功能,然后异步任务类访问服务器,上传头像,保存到数据库中, 下面写出popwindow的代码 //设置popwindow publ ...
- Java乔晓松-android中上传图片到服务器Tomcat(Struts2)
在做android开发的时候,有时你会用到图片的上传功能,在我的android项目中,我是选中图片,点击上传多张图片 android客户端上传图片部分的代码如下: package com.exampl ...
- 终于等到你---订餐系统之负载均衡(nginx+memcached+ftp上传图片+iis)
又见毕业 对面工商大学的毕业生叕在拍毕业照了,一个个脸上都挂满了笑容,也许是满意自己四年的修行,也许是期待步入繁华的社会... 恰逢其时的连绵细雨与满天柳絮,似乎也是在映衬他们心中那些离别的忧伤,与对 ...
- 通过android 客户端上传图片到服务器
昨天,(在我的上一篇博客中)写了通过浏览器上传图片到服务器(php),今天将这个功能付诸实践.(还完善了服务端的代码) 不试不知道,原来通过android 向服务端发送图片还真是挺麻烦的一件事. 上传 ...
- android 开发上传图片遇到返回 FileNotFoundException
目的:带有参数上传图片 环境:客户端开发 android studio ,服务器端:visual studio 2012 (c# asp.net) ************************* ...
- android ftp案例分析
使用方法: FTPClient client = new FTPClient(); client.connect("ftp.host.com", 8021); client.log ...
- AlarmManager的使用和七牛云android SDK上传图片
一学期的课程设计又开始了,虽然以后不搞安卓,但是课设还是想好好完成的,因为之前做过地图开发,所以选了一个跟 这个相关的题目,其实有一个校车系统也可以选,但是之前做过一个相似度接近80%的东西,不想混混 ...
- android一个上传图片的样例,包含怎样终止上传过程,假设在上传的时候更新进度条(一)
先上效果图: Layout为: <? xml version="1.0" encoding="utf-8"?> <LinearLayout x ...
随机推荐
- MVC公开课 – 1.基础 (2013-3-15广州传智MVC公开课)
1.MVC设计模式 Model 是指 要处理的业务代码和数据操作代码 View 视图 主要是指的 跟用户打交道 并能够展示数据 Controller 看成是 Model和View的桥梁 优点: 1 ...
- 【翻译三】java-并发之线程对象和实现
Thread Objects Each thread is associated with an instance of the class Thread. There are two basic s ...
- 【JAVA多线程概述】
一.多线程概述 一个进程中至少有一个线程,每一个线程都有自己运行的内容,这个内容可以称为线程要执行的任务. 不能没一个问题都使用多线程,能使用单线程解决的问题就不要使用多线程解决. 使用多线程的弊端: ...
- C#调用ArcGIS REST服务
ArcGIS REST API提供了简单.开放的接口来访问和使用ArcGIS Server发布的服务.使用ArcGIS REST API通过URL可以获取和操作每一个服务中的所有资源和操作. 1.使用 ...
- 分佈式事務故障處理暨ORA-24756: transaction does not exist處理
ORA-24756处理 看到警告日誌一直報ORA-24756錯誤 Errors in file /oracle/admin/NHMIX01/bdump/nhmix01_reco_4959.trc: O ...
- go-martini 简单分析之一
env.go 环境变量 const ( Dev string = "development" Prod string = "production" Test s ...
- RTP 与RTCP 解释. 含同步时间戳
转自:http://blog.csdn.net/wudebao5220150/article/details/13816225 RTP协议是real-time transport protocol的缩 ...
- HR外包系统 - 薪资项目分类
序号 薪资项目编码规则 6到9开头1 普通工资项目加项 7开头三位,7XX,不够时,从71XX开始2 普通工资项目减项 8开头三位,8XX,不够时,从81XX开始3 ...
- C语言面试
最全的C语言试题总结 第一部分:基本概念及其它问答题 1.关键字static的作用是什么? 这个简单的问题很少有人能回答完全.在C语言中,关键字static有三个明显的作用: 1). 在函数体,一个被 ...
- android 消息推送
android 消息推送 极光推送百度云推送(语音)友盟消息推送