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 ...
随机推荐
- C#学习笔记-----C#枚举中的位运算权限分配
一.基础知识 什么是位运算? 用二进制来计算,1&2:这就是位运算,其实它是将0001与0010做位预算 得到的结果是 0011,也就是3 2.位预算有多少种?(我们就将几种我们权限中会 ...
- SVN 搭建
http://www.blogjava.net/jasmine214--love/archive/2010/09/26/332989.html http://hunan.iteye.com/blog/ ...
- execl一个工作薄中有几个个工作表,将这几个个工作表分别保存到不同execl文件中
用宏运行: Sub QEJebel() Dim sh As Worksheet Dim Pa As String Pa = ThisWorkbook.Path For Each ...
- wifi基础知识整理
转自 :http://blog.chinaunix.net/uid-9525959-id-3326047.html WIFI基本知识整理 这里对wifi的802.11协议中比较常见的知识做一个基本的总 ...
- Codeforces Round #363 Fix a Tree(树 拓扑排序)
先做拓扑排序,再bfs处理 #include<cstdio> #include<iostream> #include<cstdlib> #include<cs ...
- go-martini 简单分析之一
env.go 环境变量 const ( Dev string = "development" Prod string = "production" Test s ...
- C语言判断文件是否存在(转)
int access(const char *filename, int amode); amode参数为0时表示检查文件的存在性,如果文件存在,返回0,不存在,返回-1. 这个函 ...
- Boost练习程序(multi_index_container)
代码来自:http://blog.csdn.net/whuqin/article/details/8482547 该容器能实现多列索引,挺好. #include <string> #inc ...
- HDU 5869 Different GCD Subarray Query 离线+树状数组
Different GCD Subarray Query Problem Description This is a simple problem. The teacher gives Bob a ...
- Java优化之输出十万以内的质数
(1)未经优化时所耗费的时间: public class PrimeNumber { public static void main(String[] args) { long start = Sys ...