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());
}
}
}
}
}