package com.du.ftp;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class FTPContinue {
// 连接配置文件
public FTPClient ftpClient = new FTPClient();
private static final String ftpHost = "113.105.139.145";
private static final int ftpPort = 21;
private static final String ftpUserName = "此处填写开发者FTP服务器的账号";
private static final String ftpPassword = "此处填写FTP服务器密码";
private Thread mThread;
String remote; // 远程文件
String local; // 本地文件
// 控件
private Context mContext;
private TextView mTextView;
private Button mButton;
private long process;
private Handler mParentHandler;
private static final int DOWN_UPDATE = 1;
private static final int DOWN_OVER = 2;
private static final int DOWN_BREAK = 3;
private boolean interceptFlag = false; // 中断标记
public FTPContinue(Context context, Handler handler) {
mContext = context;
mParentHandler = handler;
// 设置将过程中使用到的命令输出到控制台
this.ftpClient.addProtocolCommandListener(new PrintCommandListener(
new PrintWriter(System.out)));
}
public boolean connect() throws IOException {
ftpClient.connect(ftpHost, ftpPort);
ftpClient.setControlEncoding("GBK");
if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
if (ftpClient.login(ftpUserName, ftpPassword)) {
return true;
}
}
disconnect();
return false;
}
public void download(String strRemote, String strLocal) throws IOException {
this.remote = strRemote;
this.local = strLocal;
mThread = new Thread(mRunnable);
mThread.start();
}
// 在进程中执行导出操作
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
try {
// 设置被动模式
ftpClient.enterLocalPassiveMode();
// 设置以二进制方式传输
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// DownloadStatus result;
// 检查远程文件是否存在
FTPFile[] files = ftpClient.listFiles(new String(remote
.getBytes("GBK"), "iso-8859-1"));
if (files.length != 1) {
System.out.println("远程文件不存在");
// return DownloadStatus.Remote_File_Noexist;
}
long lRemoteSize = files[0].getSize();
File f = new File(local);
// 本地存在文件,进行断点下载
if (f.exists()) {
long localSize = f.length();
// 判断本地文件大小是否大于远程文件大小
if (localSize >= lRemoteSize) {
System.out.println("本地文件大于远程文件,下载中止");
// return DownloadStatus.Local_Bigger_Remote;
}
// 进行断点续传,并记录状态
FileOutputStream out = new FileOutputStream(f, true);
ftpClient.setRestartOffset(localSize);
InputStream in = ftpClient.retrieveFileStream(new String(
remote.getBytes("GBK"), "iso-8859-1"));
byte[] bytes = new byte[1024];
long step = lRemoteSize / 100;
process = localSize / step;
int c;
while ((c = in.read(bytes)) != -1) {
if (interceptFlag) {
break;
}
out.write(bytes, 0, c);
localSize += c;
long nowProcess = localSize / step;
if (nowProcess > process) {
process = nowProcess;
if (process % 1 == 0)
System.out.println("下载进度:" + process);
mHandler.sendEmptyMessage(DOWN_UPDATE);
}
}
in.close();
out.close();
if (c <= 0) {
mHandler.sendEmptyMessage(DOWN_OVER);
}
boolean isDo = ftpClient.completePendingCommand();
if (isDo) {
// result = DownloadStatus.Download_From_Break_Success;
mHandler.sendEmptyMessage(DOWN_OVER);
} else {
// result = DownloadStatus.Download_From_Break_Failed;
mHandler.sendEmptyMessage(DOWN_BREAK);
}
} else {
/* 如果本地文件不存在则进行远程全部下载 */
OutputStream out = new FileOutputStream(f);
InputStream in = ftpClient.retrieveFileStream(new String(
remote.getBytes("GBK"), "iso-8859-1"));
byte[] bytes = new byte[1024];
long step = lRemoteSize / 100;
process = 0;
long localSize = 0L;
int c;
while ((c = in.read(bytes)) != -1) {
if (interceptFlag) {
break;
}
out.write(bytes, 0, c);
localSize += c;
long nowProcess = localSize / step;
if (nowProcess > process) {
process = nowProcess;
if (process % 1 == 0)
System.out.println("下载进度:" + process);
mHandler.sendEmptyMessage(DOWN_UPDATE);
}
}
in.close();
out.close();
if (c <= 0) {
mHandler.sendEmptyMessage(DOWN_OVER);
}
boolean isDo = ftpClient.completePendingCommand();
if (isDo) {
// result = DownloadStatus.Download_From_Break_Success;
mHandler.sendEmptyMessage(DOWN_OVER);
} else {
// result = DownloadStatus.Download_From_Break_Failed;
mHandler.sendEmptyMessage(DOWN_BREAK);
}
}
// return result;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
public void disconnect() {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case DOWN_OVER:
downloadOver();
break;
case DOWN_BREAK:
disconnect();
Toast.makeText(mContext, "下载已经取消", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
};
};
protected void downloadOver() {
mParentHandler.sendEmptyMessage(DOWN_OVER);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
FTPDownload.rar (45个子文件)
FTPDownload
.project 847B
project.properties 361B
proguard.cfg 1KB
src
com
du
ftp
FTPContinue.java 6KB
MainActivity.java 2KB
AndroidManifest.xml 883B
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
chat_bottom_bg.9.png 218B
drawable-mdpi
ic_launcher.png 3KB
layout
main.xml 1KB
values
strings.xml 181B
.settings
org.eclipse.jdt.core.prefs 177B
assets
gen
com
du
ftp
BuildConfig.java 152B
R.java 897B
libs
commons-net-3.1.jar 267KB
.classpath 538B
bin
resources.ap_ 11KB
classes
com
du
ftp
R$layout.class 370B
BuildConfig.class 329B
R$drawable.class 421B
R$attr.class 316B
FTPContinue$2.class 1KB
R$id.class 419B
MainActivity$2.class 761B
R.class 475B
FTPContinue$1.class 4KB
R$string.class 403B
MainActivity$1.class 748B
MainActivity$3.class 911B
MainActivity.class 3KB
FTPContinue.class 3KB
ContinueFTP2.apk 136KB
AndroidManifest.xml 883B
ContinueFTP.apk 138KB
dexedLibs
commons-net-3.1-c1091048012f315bddaffe7291157420.jar 129KB
commons-net-3.1-8d9ae55c6cff186e9b9d471c9348fead.jar 129KB
jarlist.cache 120B
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 2KB
crunch
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 2KB
classes.dex 346KB
共 45 条
- 1
资源评论
- yjqldj2018-10-17垃圾 不能用 骗分的百叶天空2019-11-12只是不符合您的需求吧,本人之前做项目是用的这个呢
百叶天空
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功