From 53128b42ddb1b51271835e45baec918409ef8b3e Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Mon, 24 Jul 2017 18:19:46 +0800 Subject: [PATCH] ftp --- .../aria/core/download/DownloadGroupTask.java | 2 +- .../aria/core/download/DownloadTask.java | 4 +- .../downloader/ftp/FtpConfigEntity.java | 38 ++++++++ .../downloader/{ => ftp}/FtpDownloadUtil.java | 36 +++++-- .../downloader/ftp/FtpThreadTask.java | 96 +++++++++++++++++++ .../{ => http}/ChildThreadConfigEntity.java | 2 +- .../downloader/{ => http}/ConnectionHelp.java | 2 +- .../{ => http}/DownloadGroupUtil.java | 5 +- .../downloader/{ => http}/Downloader.java | 7 +- .../downloader/{ => http}/FileInfoThread.java | 2 +- .../{ => http}/SimpleDownloadUtil.java | 4 +- .../{ => http}/SingleThreadTask.java | 3 +- .../downloader/{ => http}/StateConstance.java | 2 +- 13 files changed, 181 insertions(+), 22 deletions(-) create mode 100644 Aria/src/main/java/com/arialyy/aria/core/download/downloader/ftp/FtpConfigEntity.java rename Aria/src/main/java/com/arialyy/aria/core/download/downloader/{ => ftp}/FtpDownloadUtil.java (52%) create mode 100644 Aria/src/main/java/com/arialyy/aria/core/download/downloader/ftp/FtpThreadTask.java rename Aria/src/main/java/com/arialyy/aria/core/download/downloader/{ => http}/ChildThreadConfigEntity.java (89%) rename Aria/src/main/java/com/arialyy/aria/core/download/downloader/{ => http}/ConnectionHelp.java (98%) rename Aria/src/main/java/com/arialyy/aria/core/download/downloader/{ => http}/DownloadGroupUtil.java (98%) rename Aria/src/main/java/com/arialyy/aria/core/download/downloader/{ => http}/Downloader.java (98%) rename Aria/src/main/java/com/arialyy/aria/core/download/downloader/{ => http}/FileInfoThread.java (99%) rename Aria/src/main/java/com/arialyy/aria/core/download/downloader/{ => http}/SimpleDownloadUtil.java (93%) rename Aria/src/main/java/com/arialyy/aria/core/download/downloader/{ => http}/SingleThreadTask.java (98%) rename Aria/src/main/java/com/arialyy/aria/core/download/downloader/{ => http}/StateConstance.java (96%) diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java index 6c668ad2..361c8e46 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java @@ -17,7 +17,7 @@ package com.arialyy.aria.core.download; import android.os.Handler; import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.download.downloader.DownloadGroupUtil; +import com.arialyy.aria.core.download.downloader.http.DownloadGroupUtil; import com.arialyy.aria.core.download.downloader.IDownloadUtil; import com.arialyy.aria.core.inf.AbsGroupTask; import com.arialyy.aria.core.scheduler.ISchedulers; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java index 0f255636..2dcf3420 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java @@ -19,9 +19,9 @@ 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.download.downloader.FtpDownloadUtil; +import com.arialyy.aria.core.download.downloader.ftp.FtpDownloadUtil; import com.arialyy.aria.core.download.downloader.IDownloadUtil; -import com.arialyy.aria.core.download.downloader.SimpleDownloadUtil; +import com.arialyy.aria.core.download.downloader.http.SimpleDownloadUtil; import com.arialyy.aria.core.inf.AbsNormalTask; import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.IEntity; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ftp/FtpConfigEntity.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ftp/FtpConfigEntity.java new file mode 100644 index 00000000..36bcdcf9 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ftp/FtpConfigEntity.java @@ -0,0 +1,38 @@ +/* + * 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.ftp; + +import com.arialyy.aria.core.download.DownloadTaskEntity; + +/** + * Created by Aria.Lao on 2017/7/24. + * ftp下载信息实体 + */ +class FtpConfigEntity { + //下载文件大小 + long FILE_SIZE; + //子线程启动下载位置 + long START_LOCATION; + //下载路径 + String PATH; + DownloadTaskEntity TASK_ENTITY; + //FTP 服务器地址 + String SERVER_IP; + //FTP 服务器端口 + String PORT; + //FTP服务器地址 + String SERVER_FILE_PATH; +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDownloadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ftp/FtpDownloadUtil.java similarity index 52% rename from Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDownloadUtil.java rename to Aria/src/main/java/com/arialyy/aria/core/download/downloader/ftp/FtpDownloadUtil.java index 88051be0..f01f752c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDownloadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ftp/FtpDownloadUtil.java @@ -1,9 +1,34 @@ -package com.arialyy.aria.core.download.downloader; +/* + * 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.ftp; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; +import com.arialyy.aria.core.download.downloader.IDownloadListener; +import com.arialyy.aria.core.download.downloader.IDownloadUtil; +import com.arialyy.aria.util.BufferedRandomAccessFile; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +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.FTPFileEntryParser; +import org.apache.commons.net.ftp.FTPListParseEngine; import org.apache.commons.net.ftp.FTPReply; /** @@ -63,15 +88,6 @@ public class FtpDownloadUtil implements IDownloadUtil, Runnable { } private void test() throws IOException { - FTPClient client = new FTPClient(); - client.connect(mEntity.getDownloadUrl()); - client.login(mTaskEntity.userName, mTaskEntity.userPw); - int reply = client.getReplyCode(); - if (!FTPReply.isPositiveCompletion(reply)) { - client.disconnect(); - failDownload("无法连接到ftp服务器,错误码为:" + reply); - return; - } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ftp/FtpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ftp/FtpThreadTask.java new file mode 100644 index 00000000..176ba7fa --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ftp/FtpThreadTask.java @@ -0,0 +1,96 @@ +///* +// * 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.ftp; +// +//import com.arialyy.aria.core.AriaManager; +//import com.arialyy.aria.core.download.DownloadTaskEntity; +//import com.arialyy.aria.core.download.downloader.IDownloadListener; +//import com.arialyy.aria.util.BufferedRandomAccessFile; +//import java.io.File; +//import java.io.IOException; +//import java.io.InputStream; +//import java.math.BigDecimal; +//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; +// +///** +// * Created by Aria.Lao on 2017/7/24. +// * Ftp下载任务 +// */ +//class FtpThreadTask implements Runnable { +// +// private FtpConfigEntity mConfig; +// private IDownloadListener mListener; +// private DownloadTaskEntity mTaskEntity; +// private int mBufSize; +// private long mSleepTime = 0; +// +// FtpThreadTask(FtpConfigEntity config, IDownloadListener listener) { +// AriaManager manager = AriaManager.getInstance(AriaManager.APP); +// mConfig = config; +// mListener = listener; +// +// mBufSize = manager.getDownloadConfig().getBuffSize(); +// setMaxSpeed(AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMsxSpeed()); +// } +// +// void setMaxSpeed(double maxSpeed) { +// if (-0.9999 < maxSpeed && maxSpeed < 0.00001) { +// mSleepTime = 0; +// } else { +// BigDecimal db = new BigDecimal(((mBufSize / 1024) / maxSpeed) * 1000); +// mSleepTime = db.setScale(0, BigDecimal.ROUND_HALF_UP).longValue(); +// } +// } +// +// @Override public void run() { +// try { +// +// FTPClient 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.connect(pp[0], Integer.parseInt(pp[1])); +// client.login(mTaskEntity.userName, mTaskEntity.userPw); +// int reply = client.getReplyCode(); +// if (!FTPReply.isPositiveCompletion(reply)) { +// client.disconnect(); +// //failDownload("无法连接到ftp服务器,错误码为:" + reply); +// return; +// } +// client.enterLocalPassiveMode(); +// client.setFileType(FTP.BINARY_FILE_TYPE); +// FTPFile[] files = client.listFiles(fileName); +// files[0].getSize(); +// BufferedRandomAccessFile file = +// new BufferedRandomAccessFile(new File(mConfig.PATH), "rwd", 8192); +// InputStream is = client.retrieveFileStream(fileName); +// +// byte[] buffer = new byte[8192]; +// int len; +// //当前子线程的下载位置 +// while ((len = is.read(buffer)) != -1) { +// file.write(buffer, 0, len); +// } +// }catch (IOException e){ +// +// } +// } +//} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ChildThreadConfigEntity.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/ChildThreadConfigEntity.java similarity index 89% rename from Aria/src/main/java/com/arialyy/aria/core/download/downloader/ChildThreadConfigEntity.java rename to Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/ChildThreadConfigEntity.java index 94b865ea..b6145d8e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ChildThreadConfigEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/ChildThreadConfigEntity.java @@ -1,4 +1,4 @@ -package com.arialyy.aria.core.download.downloader; +package com.arialyy.aria.core.download.downloader.http; import com.arialyy.aria.core.download.DownloadTaskEntity; import java.io.File; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/ConnectionHelp.java similarity index 98% rename from Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java rename to Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/ConnectionHelp.java index 028db87c..4970e5cc 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/ConnectionHelp.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.downloader; +package com.arialyy.aria.core.download.downloader.http; import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.util.SSLContextUtil; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/DownloadGroupUtil.java similarity index 98% rename from Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java rename to Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/DownloadGroupUtil.java index 5250f610..66062d32 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/DownloadGroupUtil.java @@ -13,12 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.downloader; +package com.arialyy.aria.core.download.downloader.http; import android.util.SparseArray; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; +import com.arialyy.aria.core.download.downloader.IDownloadGroupListener; +import com.arialyy.aria.core.download.downloader.IDownloadListener; +import com.arialyy.aria.core.download.downloader.IDownloadUtil; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.CommonUtil; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/Downloader.java similarity index 98% rename from Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java rename to Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/Downloader.java index 172731e1..8126d989 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/Downloader.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.downloader; +package com.arialyy.aria.core.download.downloader.http; import android.content.Context; import android.util.Log; @@ -21,6 +21,8 @@ import android.util.SparseArray; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; +import com.arialyy.aria.core.download.downloader.IDownloadListener; +import com.arialyy.aria.core.download.downloader.IDownloadUtil; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.BufferedRandomAccessFile; import com.arialyy.aria.util.CommonUtil; @@ -65,7 +67,8 @@ class Downloader implements Runnable, IDownloadUtil { mConstance = new StateConstance(); } - void setMaxSpeed(double maxSpeed) { + @Override + public void setMaxSpeed(double maxSpeed) { for (int i = 0; i < mThreadNum; i++) { SingleThreadTask task = (SingleThreadTask) mTask.get(i); if (task != null) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FileInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/FileInfoThread.java similarity index 99% rename from Aria/src/main/java/com/arialyy/aria/core/download/downloader/FileInfoThread.java rename to Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/FileInfoThread.java index 71fbbc27..45ca5220 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FileInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/FileInfoThread.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.downloader; +package com.arialyy.aria.core.download.downloader.http; import android.text.TextUtils; import android.util.Log; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/SimpleDownloadUtil.java similarity index 93% rename from Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java rename to Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/SimpleDownloadUtil.java index bd22f7aa..4b0b6311 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/SimpleDownloadUtil.java @@ -14,11 +14,13 @@ * limitations under the License. */ -package com.arialyy.aria.core.download.downloader; +package com.arialyy.aria.core.download.downloader.http; import android.text.TextUtils; import android.util.Log; import com.arialyy.aria.core.download.DownloadTaskEntity; +import com.arialyy.aria.core.download.downloader.IDownloadListener; +import com.arialyy.aria.core.download.downloader.IDownloadUtil; /** * Created by lyy on 2015/8/25. diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SingleThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/SingleThreadTask.java similarity index 98% rename from Aria/src/main/java/com/arialyy/aria/core/download/downloader/SingleThreadTask.java rename to Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/SingleThreadTask.java index ea7666c2..5d44e97c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SingleThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/SingleThreadTask.java @@ -13,12 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.downloader; +package com.arialyy.aria.core.download.downloader.http; import android.os.Build; import android.text.TextUtils; import android.util.Log; import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.download.downloader.IDownloadListener; import com.arialyy.aria.util.BufferedRandomAccessFile; import com.arialyy.aria.util.CommonUtil; import java.io.File; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/StateConstance.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/StateConstance.java similarity index 96% rename from Aria/src/main/java/com/arialyy/aria/core/download/downloader/StateConstance.java rename to Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/StateConstance.java index 323f65ff..a937a725 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/StateConstance.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/http/StateConstance.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.downloader; +package com.arialyy.aria.core.download.downloader.http; /** * Created by lyy on 2017/1/18.