package com.ftp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class CustomApacheFTPManage {
public static String hostName = "";
public static int serverPort = 1200;
public static String userName = "";
public static String password = "";
private static FTPClient ftpClient = null;
public static final String FTP_CONNECT_SUCCESSS = "FTP_CONNECT_SUCCESSS";
public static final String FTP_CONNECT_FAIL = "FTP_CONNECT_FAIL";
public static final String FTP_DISCONNECT_SUCCESS = "FTP_DISCONNECT_SUCCESS";
public static final String FTP_FILE_NOTEXISTS = "FTP_FILE_NOTEXISTS";
public static final String FTP_UPLOAD_SUCCESS = "FTP_UPLOAD_SUCCESS";
public static final String FTP_UPLOAD_FAIL = "FTP_UPLOAD_FAIL";
public static final String FTP_UPLOAD_LOADING = "FTP_UPLOAD_LOADING";
public static final String FTP_DOWN_LOADING = "FTP_DOWN_LOADING";
public static final String FTP_DOWN_SUCCESS = "FTP_DOWN_SUCCESS";
public static final String FTP_DOWN_FAIL = "FTP_DOWN_FAIL";
public static final String FTP_DELETEFILE_SUCCESS = "FTP_DELETEFILE_SUCCESS";
public static final String FTP_DELETEFILE_FAIL = "FTP_DELETEFILE_FAIL";
//用于单例模式
public static CustomApacheFTPManage mCustomFTPManage = null;
//单例模式获取实例
public static CustomApacheFTPManage GetInstance() {
if (mCustomFTPManage == null || ftpClient == null ) {
mCustomFTPManage = new CustomApacheFTPManage();
ftpClient = new FTPClient();
}
return mCustomFTPManage;
}
public CustomApacheFTPManage(){
}
public void setParameter(String strhostName,int nserverPort ,String struserName,String strpassword) {
hostName = strhostName;
serverPort = nserverPort;
userName = struserName;
password = strpassword;
}
// -------------------------------------------------------文件上传方法------------------------------------------------
/**
* 上传单个文件.
*
* @param localFile
* 本地文件
* @param remotePath
* FTP目录
* @param listener
* 监听器
* @throws IOException
*/
public void uploadSingleFile(File singleFile, String remotePath,CurrentTipListener listener) throws IOException {
// 上传之前初始化
this.uploadBeforeOperate(remotePath, listener);
boolean flag;
flag = uploadingSingle(singleFile, listener);
// 上传完成之后关闭连接
this.uploadAfterOperate(listener);
}
/**
* 上传多个文件.
*
* @param localFile
* 本地文件
* @param remotePath
* FTP目录
* @param listener
* 监听器
* @throws IOException
*/
public void uploadMultiFile(LinkedList<File> fileList, String remotePath,CurrentTipListener listener) throws IOException {
// 上传之前初始化
if (uploadBeforeOperate(remotePath, listener)) {
boolean flag;
int n = 0;
for (File singleFile : fileList) {
flag = uploadingSingle(singleFile, listener);
if (flag) {
n ++;
if (n == fileList.size())
listener.onCurrentTipCallBack(FTP_UPLOAD_SUCCESS);
} else {
listener.onCurrentTipCallBack(FTP_UPLOAD_FAIL);
}
}
// 上传完成之后关闭连接
this.uploadAfterOperate(listener);
};
}
/**
* 上传单个文件.
*
* @param localFile
* 本地文件
* @return true上传成功, false上传失败
* @throws IOException
*/
private boolean uploadingSingle(File localFile,CurrentTipListener listener) throws IOException {
boolean flag = true;
// 不带进度条的方式
// 创建输入流
InputStream inputStream = new FileInputStream(localFile);
// 上传单个文件
flag = ftpClient.storeFile(localFile.getName(), inputStream);
// 关闭文件流
inputStream.close();
// 带有进度的方式
// BufferedInputStream buffIn = new BufferedInputStream(
// new FileInputStream(localFile));
// ProgressInputStream progressInput = new ProgressInputStream(buffIn,
// listener, localFile);
// flag = ftpClient.storeFile(localFile.getName(), progressInput);
// buffIn.close();
return flag;
}
/**
* 上传文件之前初始化相关参数
*
* @param remotePath
* FTP目录
* @param listener
* 监听器
* @throws IOException
*/
private boolean uploadBeforeOperate(String remotePath,CurrentTipListener listener) throws IOException {
// 打开FTP服务
try {
this.openConnect();
listener.onCurrentTipCallBack(FTP_CONNECT_SUCCESSS);
} catch (IOException e1) {
e1.printStackTrace();
listener.onCurrentTipCallBack(FTP_CONNECT_FAIL);
return false;
}
// 设置模式
ftpClient.setFileTransferMode(org.apache.commons.net.ftp.FTP.STREAM_TRANSFER_MODE);
// FTP下创建文件夹
ftpClient.makeDirectory(remotePath);
// 改变FTP目录
ftpClient.changeWorkingDirectory(remotePath);
return true;
}
/**
* 上传完成之后关闭连接
*
* @param listener
* @throws IOException
*/
private void uploadAfterOperate(CurrentTipListener listener)throws IOException {
this.closeConnect();
}
// -------------------------------------------------------文件下载方法------------------------------------------------
/**
* 下载单个文件,可实现断点下载.
*
* @param serverPath
* Ftp目录及文件路径
* @param localPath
* 本地目录
* @param fileName
* 下载之后的文件名称
* @param listener
* 监听器
* @throws IOException
*/
public void downloadSingleFile(String serverPath, String localPath, String fileName, DownLoadProgressListener listener)
throws Exception {
// 打开FTP服务
try {
this.openConnect();
listener.onDownLoadProgress(FTP_CONNECT_SUCCESSS, 0, null);
} catch (IOException e1) {
e1.printStackTrace();
listener.onDownLoadProgress(FTP_CONNECT_FAIL, 0, null);
return;
}
// 先判断服务器文件是否存在
FTPFile[] files = ftpClient.listFiles(serverPath);
if (files.length == 0) {
listener.onDownLoadProgress(FTP_FILE_NOTEXISTS, 0, null);
return;
}
//创建本地文件夹
File mkFile = new File(localPath);
if (!mkFile.exists()) {
mkFile.mkdirs();
}
localPath = localPath + fileName;
// 接着判断下载的文件是否能断点下载
long serverSize = files[0].getSize(); // 获取远程文件的长度
File localFile = new File(localPath);
long localSize = 0;
if (localFile.exists()) {
localSize = localFile.length(); // 如果本地文件存在,获取本地文件的长度
if (localSize >= serverSize) {
File file = new File(localPath);
file.delete();
}
}
// 进度
long step = serverSize / 100;
long process = 0;
long currentSize = 0;
// 开始准备下载文件
OutputStream out = new FileOutputStream(localFile, true);
ftpClient.setRestartOffset(localSize);
InputStream input = ftpClient.retrieveFileStream(serverPath);
byte[] b = new byte[1024];
int length = 0;
while ((length = input.read(b)) != -1) {
out.write(b, 0, length);
currentSize = currentSize + length;
if (currentSize / step != process) {
process = currentSize / step;
if (process % 5 == 0) { //每隔%5的进度返回一次
listener.onDownLoadProgress(FTP_DOWN_LOADING, process, null);
}
}
}
out.flush();
out.close();
inpu
没有合适的资源?快使用搜索试试~ 我知道了~
android 开源库上传FTP
共3个文件
rar:1个
jar:1个
java:1个
需积分: 31 62 下载量 117 浏览量
2016-07-24
10:48:50
上传
评论 1
收藏 620KB RAR 举报
温馨提示
开源库的包名是这个org.apache.commons.net.ftp.FTPClient;是属于局域网的ftp上传,要有ip、端口、用户名以及密码。我根据网上的下载demo,自已研究了一番,又封装了一个类自已使用。欢迎访问博客:http://blog.csdn.net/qq_16064871
资源推荐
资源详情
资源评论
收起资源包目录
CustomApacheFTPManage.rar (3个子文件)
CustomApacheFTPManage
CustomApacheFTPManage.java 12KB
FTPClientProgress2.rar 432KB
commons-net-2.2.jar 207KB
共 3 条
- 1
资源评论
mmsx
- 粉丝: 715
- 资源: 92
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功