实现ftp单文件下载
This commit is contained in:
@ -228,7 +228,7 @@ class ConfigHelper extends DefaultHandler {
|
||||
}
|
||||
if (num < 1) {
|
||||
Log.e(TAG, "下载线程数不能小于 1");
|
||||
num = 3;
|
||||
num = 1;
|
||||
}
|
||||
if (isDownloadConfig) {
|
||||
mDownloadConfig.threadNum = num;
|
||||
|
@ -16,6 +16,7 @@
|
||||
package com.arialyy.aria.core.download;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.core.AriaManager;
|
||||
import com.arialyy.aria.core.inf.AbsEntity;
|
||||
import com.arialyy.aria.core.inf.AbsTask;
|
||||
|
@ -58,22 +58,19 @@ public class DownloadReceiver extends AbsReceiver {
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载Http下载功能
|
||||
* 加载Http、https
|
||||
*/
|
||||
public DownloadTarget load(@NonNull String downloadUrl) {
|
||||
CheckUtil.checkDownloadUrl(downloadUrl);
|
||||
return new DownloadTarget(downloadUrl, targetName);
|
||||
public DownloadTarget load(@NonNull String url) {
|
||||
CheckUtil.checkDownloadUrl(url);
|
||||
return new DownloadTarget(url, targetName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载Ftp下载功能
|
||||
*
|
||||
* @param serverIp ftp服务器地址
|
||||
* @param port ftp端口
|
||||
* @param filePath 需要从ftp服务器上下载的文件的路径
|
||||
* 加载ftp下载地址
|
||||
*/
|
||||
public FtpDownloadTarget load(String serverIp, String port, String filePath) {
|
||||
return new FtpDownloadTarget(serverIp, port, filePath, targetName);
|
||||
public FtpDownloadTarget loadFtp(@NonNull String url) {
|
||||
CheckUtil.checkDownloadUrl(url);
|
||||
return new FtpDownloadTarget(url, targetName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -30,12 +30,13 @@ import java.io.File;
|
||||
*/
|
||||
public class DownloadTarget
|
||||
extends AbsDownloadTarget<DownloadTarget, DownloadEntity, DownloadTaskEntity> {
|
||||
|
||||
protected String url;
|
||||
DownloadTarget(DownloadEntity entity, String targetName) {
|
||||
this(entity.getDownloadUrl(), targetName);
|
||||
}
|
||||
|
||||
DownloadTarget(String url, String targetName) {
|
||||
this.url = url;
|
||||
mTargetName = targetName;
|
||||
DownloadEntity entity = getEntity(url);
|
||||
mTaskEntity = DbEntity.findFirst(DownloadTaskEntity.class, "key=? and isGroupTask='false'",
|
||||
|
@ -30,6 +30,11 @@ public class DownloadTaskEntity extends AbsTaskEntity<DownloadEntity> {
|
||||
*/
|
||||
@Ignore public String userName, userPw, account;
|
||||
|
||||
/**
|
||||
* FTP服务器文件或文件夹路径
|
||||
*/
|
||||
public String remotePath;
|
||||
|
||||
/**
|
||||
* 下载类型
|
||||
* {@link AbsTaskEntity#HTTP}、{@link AbsTaskEntity#FTP}
|
||||
|
@ -21,7 +21,6 @@ import android.util.Log;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import java.io.File;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2016/12/5.
|
||||
@ -29,23 +28,22 @@ import java.nio.charset.Charset;
|
||||
*/
|
||||
public class FtpDownloadTarget extends DownloadTarget {
|
||||
private final String TAG = "FtpDownloadTarget";
|
||||
private String serverIp, remotePath;
|
||||
private int port;
|
||||
|
||||
/**
|
||||
* @param serverIp ftp服务器地址
|
||||
* @param port ftp端口号
|
||||
*/
|
||||
FtpDownloadTarget(String serverIp, String port, String filePath, String targetName) {
|
||||
this("ftp://" + serverIp + ":" + port + "/" + filePath, targetName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param url url 为 serverIp:port/filePath
|
||||
*/
|
||||
private FtpDownloadTarget(String url, String targetName) {
|
||||
FtpDownloadTarget(String url, String targetName) {
|
||||
super(url, targetName);
|
||||
String[] pp = url.split("/")[2].split(":");
|
||||
this.serverIp = pp[0];
|
||||
this.port = Integer.parseInt(pp[1]);
|
||||
mTaskEntity.downloadType = AbsTaskEntity.FTP;
|
||||
remotePath = url.substring(url.indexOf(pp[1]) + pp[1].length(), url.length());
|
||||
if (TextUtils.isEmpty(remotePath)) {
|
||||
throw new NullPointerException("ftp服务器地址不能为null");
|
||||
}
|
||||
int lastIndex = url.lastIndexOf("/");
|
||||
mTaskEntity.downloadType = AbsTaskEntity.FTP;
|
||||
mTargetName = targetName;
|
||||
mTaskEntity.remotePath = remotePath;
|
||||
mEntity.setFileName(url.substring(lastIndex + 1, url.length()));
|
||||
}
|
||||
|
||||
@ -117,6 +115,14 @@ public class FtpDownloadTarget extends DownloadTarget {
|
||||
mTaskEntity.userName = userName;
|
||||
mTaskEntity.userPw = password;
|
||||
mTaskEntity.account = account;
|
||||
//FtpClientHelp.getInstnce().login(serverIp, port, userName, password, account);
|
||||
return this;
|
||||
}
|
||||
|
||||
///**
|
||||
// * 当所有任务都完成时,需要登出ftp服务器
|
||||
// */
|
||||
//public void logout() {
|
||||
// FtpClientHelp.getInstnce().logout();
|
||||
//}
|
||||
}
|
||||
|
@ -16,6 +16,7 @@
|
||||
package com.arialyy.aria.core.download.downloader;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.util.SparseArray;
|
||||
import com.arialyy.aria.core.AriaManager;
|
||||
@ -33,6 +34,8 @@ import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import org.apache.commons.net.ftp.FTPClient;
|
||||
import org.apache.commons.net.ftp.FTPReply;
|
||||
|
||||
/**
|
||||
* Created by AriaL on 2017/7/1.
|
||||
@ -416,6 +419,7 @@ class Downloader implements Runnable, IDownloadUtil {
|
||||
private AbsThreadTask createThreadTask(SubThreadConfig config) {
|
||||
switch (mTaskEntity.downloadType) {
|
||||
case AbsTaskEntity.FTP:
|
||||
config.remotePath = mTaskEntity.remotePath;
|
||||
return new FtpThreadTask(mConstance, mListener, config);
|
||||
case AbsTaskEntity.HTTP:
|
||||
return new HttpThreadTask(mConstance, mListener, config);
|
||||
|
@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.arialyy.aria.core.download.downloader;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.core.AriaManager;
|
||||
import java.io.IOException;
|
||||
import org.apache.commons.net.ftp.FTPClient;
|
||||
import org.apache.commons.net.ftp.FTPReply;
|
||||
|
||||
/**
|
||||
* Created by Aria.Lao on 2017/7/26.
|
||||
*/
|
||||
public class FtpClientHelp {
|
||||
private final String TAG = "FtpClientHelp";
|
||||
private static volatile FtpClientHelp INSTANCE = null;
|
||||
|
||||
private FTPClient client;
|
||||
private String serverIp, user, pw, account;
|
||||
private int port;
|
||||
|
||||
private FtpClientHelp() {
|
||||
}
|
||||
|
||||
public static FtpClientHelp getInstnce() {
|
||||
if (INSTANCE == null) {
|
||||
synchronized (AriaManager.LOCK) {
|
||||
INSTANCE = new FtpClientHelp();
|
||||
}
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public FTPClient getClient() {
|
||||
if (client == null || !client.isConnected()) {
|
||||
createClient();
|
||||
}
|
||||
return client;
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录到FTP服务器,当客户端为null或客户端没有连接到FTP服务器时才会执行登录操作
|
||||
*/
|
||||
public void login(String serverIp, int port, String user, String pw, String account) {
|
||||
this.serverIp = serverIp;
|
||||
this.port = port;
|
||||
this.user = user;
|
||||
this.pw = pw;
|
||||
this.account = account;
|
||||
if (client == null || !client.isConnected()) {
|
||||
createClient();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 登出
|
||||
*/
|
||||
public void logout() {
|
||||
try {
|
||||
if (client != null && client.isConnected()) {
|
||||
client.logout();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
FTPClient createClient() {
|
||||
new Thread(new Runnable() {
|
||||
@Override public void run() {
|
||||
client = new FTPClient();
|
||||
try {
|
||||
client.connect(serverIp, port);
|
||||
if (!TextUtils.isEmpty(account)) {
|
||||
client.login(user, pw);
|
||||
} else {
|
||||
client.login(user, pw, account);
|
||||
}
|
||||
int reply = client.getReplyCode();
|
||||
if (!FTPReply.isPositiveCompletion(reply)) {
|
||||
client.disconnect();
|
||||
Log.e(TAG, "无法连接到ftp服务器,错误码为:" + reply);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.d(TAG, e.getMessage());
|
||||
} finally {
|
||||
synchronized (FtpClientHelp.this) {
|
||||
FtpClientHelp.this.notify();
|
||||
}
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
synchronized (FtpClientHelp.this) {
|
||||
try {
|
||||
wait();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return client;
|
||||
}
|
||||
}
|
@ -51,11 +51,8 @@ class FtpFileInfoThread implements Runnable {
|
||||
FTPClient client = null;
|
||||
try {
|
||||
client = new FTPClient();
|
||||
//ip和端口
|
||||
String[] temp = mEntity.getDownloadUrl().split("/");
|
||||
String[] pp = temp[2].split(":");
|
||||
//String dir = temp[temp.length - 2];
|
||||
String fileName = temp[temp.length - 1];
|
||||
String[] pp = mEntity.getDownloadUrl().split("/")[2].split(":");
|
||||
String fileName = mTaskEntity.remotePath;
|
||||
client.connect(pp[0], Integer.parseInt(pp[1]));
|
||||
if (!TextUtils.isEmpty(mTaskEntity.account)) {
|
||||
client.login(mTaskEntity.userName, mTaskEntity.userPw);
|
||||
|
@ -15,20 +15,14 @@
|
||||
*/
|
||||
package com.arialyy.aria.core.download.downloader;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.util.BufferedRandomAccessFile;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
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;
|
||||
|
||||
/**
|
||||
@ -57,12 +51,9 @@ class FtpThreadTask extends AbsThreadTask {
|
||||
+ ",结束位置:"
|
||||
+ mConfig.END_LOCATION
|
||||
+ "】");
|
||||
client = new FTPClient();
|
||||
//ip和端口
|
||||
String[] temp = mEntity.getDownloadUrl().split("/");
|
||||
String[] pp = temp[2].split(":");
|
||||
//String dir = temp[temp.length - 2];
|
||||
String fileName = temp[temp.length - 1];
|
||||
client = new FTPClient();
|
||||
client.connect(pp[0], Integer.parseInt(pp[1]));
|
||||
if (!TextUtils.isEmpty(mTaskEntity.account)) {
|
||||
client.login(mTaskEntity.userName, mTaskEntity.userPw);
|
||||
@ -79,14 +70,14 @@ class FtpThreadTask extends AbsThreadTask {
|
||||
client.enterLocalPassiveMode();
|
||||
client.setFileType(FTP.BINARY_FILE_TYPE);
|
||||
client.setRestartOffset(mConfig.START_LOCATION);
|
||||
is = client.retrieveFileStream(fileName);
|
||||
client.allocate(mBufSize);
|
||||
is = client.retrieveFileStream(mConfig.remotePath);
|
||||
file = new BufferedRandomAccessFile(mConfig.TEMP_FILE, "rwd", mBufSize);
|
||||
file.seek(mConfig.START_LOCATION);
|
||||
byte[] buffer = new byte[mBufSize];
|
||||
int len;
|
||||
//当前子线程的下载位置
|
||||
mChildCurrentLocation = mConfig.START_LOCATION;
|
||||
//TODO: 2017/7/25 隐藏问题,速度太快(20m/s)或文件太小,秒下,会出现 ftp Connection reset by peer 问题
|
||||
while ((len = is.read(buffer)) != -1) {
|
||||
if (STATE.isCancel) break;
|
||||
if (STATE.isStop) break;
|
||||
@ -102,7 +93,6 @@ class FtpThreadTask extends AbsThreadTask {
|
||||
}
|
||||
}
|
||||
if (STATE.isCancel || STATE.isStop) return;
|
||||
//if (client.completePendingCommand()) {
|
||||
Log.i(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】线程__" + mConfig.THREAD_ID + "__下载完毕");
|
||||
writeConfig(true, 1);
|
||||
STATE.COMPLETE_THREAD_NUM++;
|
||||
@ -120,14 +110,13 @@ class FtpThreadTask extends AbsThreadTask {
|
||||
failDownload(mChildCurrentLocation, "获取流失败", e);
|
||||
} finally {
|
||||
try {
|
||||
if (file != null){
|
||||
if (file != null) {
|
||||
file.close();
|
||||
}
|
||||
if (is != null){
|
||||
if (is != null) {
|
||||
is.close();
|
||||
}
|
||||
if (client != null && client.isConnected()) {
|
||||
//client.logout();
|
||||
client.disconnect();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
|
@ -2,6 +2,7 @@ package com.arialyy.aria.core.download.downloader;
|
||||
|
||||
import com.arialyy.aria.core.download.DownloadTaskEntity;
|
||||
import java.io.File;
|
||||
import org.apache.commons.net.ftp.FTPClient;
|
||||
|
||||
/**
|
||||
* 子线程下载信息类
|
||||
@ -21,4 +22,7 @@ class SubThreadConfig {
|
||||
String CONFIG_FILE_PATH;
|
||||
DownloadTaskEntity DOWNLOAD_TASK_ENTITY;
|
||||
boolean IS_SUPPORT_BREAK_POINT = true;
|
||||
FTPClient client;
|
||||
//远程地址
|
||||
String remotePath;
|
||||
}
|
Reference in New Issue
Block a user