ftp 断点上传
This commit is contained in:
@ -99,18 +99,17 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
|
||||
mConstance.THREAD_NUM = mThreadNum;
|
||||
handleNoSupportBP();
|
||||
} else {
|
||||
mThreadNum = isNewTask ? (getThreadNum()) : mRealThreadNum;
|
||||
mThreadNum = isNewTask ? (getNewTaskThreadNum()) : mRealThreadNum;
|
||||
mConstance.THREAD_NUM = mThreadNum;
|
||||
mFixedThreadPool = Executors.newFixedThreadPool(mThreadNum);
|
||||
handleBreakpoint();
|
||||
}
|
||||
startTimer();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取线程数
|
||||
* 设置新任务的最大线程数
|
||||
*/
|
||||
protected int getThreadNum() {
|
||||
protected int getNewTaskThreadNum() {
|
||||
return mEntity.getFileSize() <= SUB_LEN || mTaskEntity.requestType == AbsTaskEntity.FTP_DIR ? 1
|
||||
: AriaManager.getInstance(mContext).getDownloadConfig().getThreadNum();
|
||||
}
|
||||
@ -292,25 +291,6 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
|
||||
return selectThreadTask(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动单线程下载任务
|
||||
*/
|
||||
private void startSingleTask(int[] recordL) {
|
||||
if (mConstance.CURRENT_LOCATION > 0) {
|
||||
mListener.onResume(mConstance.CURRENT_LOCATION);
|
||||
} else {
|
||||
mListener.onStart(mConstance.CURRENT_LOCATION);
|
||||
}
|
||||
mFixedThreadPool = Executors.newFixedThreadPool(recordL.length);
|
||||
for (int l : recordL) {
|
||||
if (l == -1) continue;
|
||||
Runnable task = mTask.get(l);
|
||||
if (task != null) {
|
||||
mFixedThreadPool.execute(task);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理断点
|
||||
*/
|
||||
@ -358,6 +338,28 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
|
||||
startSingleTask(recordL);
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动单线程下载任务
|
||||
*/
|
||||
private void startSingleTask(int[] recordL) {
|
||||
if (mConstance.CURRENT_LOCATION > 0) {
|
||||
mListener.onResume(mConstance.CURRENT_LOCATION);
|
||||
} else {
|
||||
mListener.onStart(mConstance.CURRENT_LOCATION);
|
||||
}
|
||||
mFixedThreadPool = Executors.newFixedThreadPool(recordL.length);
|
||||
for (int l : recordL) {
|
||||
if (l == -1) continue;
|
||||
Runnable task = mTask.get(l);
|
||||
if (task != null) {
|
||||
mFixedThreadPool.execute(task);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理新任务
|
||||
*/
|
||||
protected abstract void handleNewTask();
|
||||
|
||||
/**
|
||||
@ -377,6 +379,7 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
|
||||
AbsThreadTask task = selectThreadTask(config);
|
||||
if (task == null) return;
|
||||
mTask.put(0, task);
|
||||
mFixedThreadPool = Executors.newFixedThreadPool(1);
|
||||
mFixedThreadPool.execute(task);
|
||||
mListener.onStart(0);
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ import com.arialyy.aria.core.AriaManager;
|
||||
import com.arialyy.aria.core.inf.AbsEntity;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
import com.arialyy.aria.core.inf.IEventListener;
|
||||
import com.arialyy.aria.core.upload.UploadEntity;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@ -134,7 +135,7 @@ public abstract class AbsThreadTask<ENTITY extends AbsEntity, TASK_ENTITY extend
|
||||
if (configFile.exists()) {
|
||||
configFile.delete();
|
||||
}
|
||||
if (mConfig.TEMP_FILE.exists()) {
|
||||
if (mConfig.TEMP_FILE.exists() && !(mEntity instanceof UploadEntity)) {
|
||||
mConfig.TEMP_FILE.delete();
|
||||
}
|
||||
Log.d(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】已取消");
|
||||
|
@ -30,7 +30,7 @@ import java.lang.ref.WeakReference;
|
||||
/**
|
||||
* 下载监听类
|
||||
*/
|
||||
class BaseListener<ENTITY extends AbsEntity, TASK extends AbsTask<ENTITY>>
|
||||
class BaseDListener<ENTITY extends AbsEntity, TASK extends AbsTask<ENTITY>>
|
||||
implements IDownloadListener {
|
||||
private WeakReference<Handler> outHandler;
|
||||
private long mLastLen = 0; //上一次发送长度
|
||||
@ -40,7 +40,7 @@ class BaseListener<ENTITY extends AbsEntity, TASK extends AbsTask<ENTITY>>
|
||||
private boolean isConvertSpeed = false;
|
||||
boolean isWait = false;
|
||||
|
||||
BaseListener(TASK task, Handler outHandler) {
|
||||
BaseDListener(TASK task, Handler outHandler) {
|
||||
this.outHandler = new WeakReference<>(outHandler);
|
||||
this.mTask = new WeakReference<>(task).get();
|
||||
this.mEntity = this.mTask.getEntity();
|
@ -16,14 +16,13 @@
|
||||
package com.arialyy.aria.core.download;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.core.download.downloader.IDownloadGroupListener;
|
||||
|
||||
/**
|
||||
* Created by Aria.Lao on 2017/7/20.
|
||||
* 任务组下载事件
|
||||
*/
|
||||
class DownloadGroupListener extends BaseListener<DownloadGroupEntity, DownloadGroupTask>
|
||||
class DownloadGroupListener extends BaseDListener<DownloadGroupEntity, DownloadGroupTask>
|
||||
implements IDownloadGroupListener {
|
||||
private final String TAG = "DownloadGroupListener";
|
||||
|
||||
|
@ -22,7 +22,7 @@ import com.arialyy.aria.core.inf.IDownloadListener;
|
||||
* Created by Aria.Lao on 2017/7/20.
|
||||
* 普通任务下载的事件监听器
|
||||
*/
|
||||
class DownloadListener extends BaseListener<DownloadEntity, DownloadTask>
|
||||
class DownloadListener extends BaseDListener<DownloadEntity, DownloadTask>
|
||||
implements IDownloadListener {
|
||||
DownloadListener(DownloadTask task, Handler outHandler) {
|
||||
super(task, outHandler);
|
||||
|
@ -139,14 +139,12 @@ public class DownloadTask extends AbsNormalTask<DownloadEntity> {
|
||||
* 取消下载
|
||||
*/
|
||||
@Override public void cancel() {
|
||||
if (!mEntity.isComplete()) {
|
||||
if (!mUtil.isRunning()) {
|
||||
if (mOutHandler != null) {
|
||||
mOutHandler.obtainMessage(ISchedulers.CANCEL, this).sendToTarget();
|
||||
}
|
||||
if (!mUtil.isRunning()) {
|
||||
if (mOutHandler != null) {
|
||||
mOutHandler.obtainMessage(ISchedulers.CANCEL, this).sendToTarget();
|
||||
}
|
||||
mUtil.cancel();
|
||||
}
|
||||
mUtil.cancel();
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
|
@ -37,7 +37,6 @@ class HttpFileInfoThread implements Runnable {
|
||||
private int mConnectTimeOut;
|
||||
private OnFileInfoCallback onFileInfoListener;
|
||||
|
||||
|
||||
HttpFileInfoThread(DownloadTaskEntity taskEntity, OnFileInfoCallback callback) {
|
||||
this.mTaskEntity = taskEntity;
|
||||
mEntity = taskEntity.getEntity();
|
||||
@ -73,7 +72,8 @@ class HttpFileInfoThread implements Runnable {
|
||||
private void handleConnect(HttpURLConnection conn) throws IOException {
|
||||
long len = conn.getContentLength();
|
||||
if (len < 0) {
|
||||
len = Long.parseLong(conn.getHeaderField(mTaskEntity.contentLength));
|
||||
String temp = conn.getHeaderField(mTaskEntity.contentLength);
|
||||
len = TextUtils.isEmpty(temp) ? -1 : Long.parseLong(temp);
|
||||
}
|
||||
int code = conn.getResponseCode();
|
||||
boolean isComplete = false;
|
||||
|
@ -16,7 +16,6 @@
|
||||
|
||||
package com.arialyy.aria.core.download.downloader;
|
||||
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.core.common.IUtil;
|
||||
import com.arialyy.aria.core.download.DownloadTaskEntity;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
@ -71,7 +70,6 @@ public class SimpleDownloadUtil implements IUtil, Runnable {
|
||||
* 多线程断点续传下载文件,开始下载
|
||||
*/
|
||||
@Override public void start() {
|
||||
mListener.onPre();
|
||||
new Thread(this).start();
|
||||
}
|
||||
|
||||
@ -84,11 +82,11 @@ public class SimpleDownloadUtil implements IUtil, Runnable {
|
||||
}
|
||||
|
||||
private void failDownload(String msg) {
|
||||
Log.e(TAG, msg);
|
||||
mListener.onFail();
|
||||
}
|
||||
|
||||
@Override public void run() {
|
||||
mListener.onPre();
|
||||
if (mTaskEntity.getEntity().getFileSize() <= 1) {
|
||||
new Thread(createInfoThread()).start();
|
||||
} else {
|
||||
|
@ -153,7 +153,7 @@ public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEnti
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始下载
|
||||
* 开始任务
|
||||
*/
|
||||
@Override public void start() {
|
||||
AriaManager.getInstance(AriaManager.APP)
|
||||
@ -162,7 +162,7 @@ public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEnti
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止下载
|
||||
* 停止任务
|
||||
*
|
||||
* @see #stop()
|
||||
*/
|
||||
@ -177,7 +177,7 @@ public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEnti
|
||||
}
|
||||
|
||||
/**
|
||||
* 恢复下载
|
||||
* 恢复任务
|
||||
*/
|
||||
@Override public void resume() {
|
||||
AriaManager.getInstance(AriaManager.APP)
|
||||
@ -186,7 +186,7 @@ public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEnti
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消下载
|
||||
* 删除任务
|
||||
*/
|
||||
@Override public void cancel() {
|
||||
AriaManager.getInstance(AriaManager.APP)
|
||||
@ -194,6 +194,19 @@ public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEnti
|
||||
.exe();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除任务
|
||||
*
|
||||
* @param removeFile {@code true} 不仅删除任务数据库记录,还会删除已经删除完成的文件
|
||||
* {@code false}如果任务已经完成,只删除任务数据库记录,
|
||||
*/
|
||||
public void cancel(boolean removeFile) {
|
||||
mTaskEntity.removeFile = removeFile;
|
||||
AriaManager.getInstance(AriaManager.APP)
|
||||
.setCmd(CommonUtil.createCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_CANCEL))
|
||||
.exe();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建文件名,如果url链接有后缀名,则使用url中的后缀名
|
||||
*
|
||||
|
@ -24,37 +24,37 @@ public interface IReceiver<ENTITY extends IEntity> {
|
||||
/**
|
||||
* Receiver 销毁
|
||||
*/
|
||||
public void destroy();
|
||||
void destroy();
|
||||
|
||||
/**
|
||||
* 移除事件回调
|
||||
*/
|
||||
public void removeSchedulerListener();
|
||||
void removeSchedulerListener();
|
||||
|
||||
/**
|
||||
* 移除观察者
|
||||
*/
|
||||
public void unRegister();
|
||||
void unRegister();
|
||||
|
||||
/**
|
||||
* 停止所有任务
|
||||
*/
|
||||
public void stopAllTask();
|
||||
void stopAllTask();
|
||||
|
||||
/**
|
||||
* 删除所有任务
|
||||
*/
|
||||
public void removeAllTask(boolean removeFile);
|
||||
void removeAllTask(boolean removeFile);
|
||||
|
||||
/**
|
||||
* 任务是否存在
|
||||
*
|
||||
* @param key 下载时为下载路径,上传时为文件路径
|
||||
*/
|
||||
public boolean taskExists(String key);
|
||||
boolean taskExists(String key);
|
||||
|
||||
/**
|
||||
* 获取任务列表
|
||||
*/
|
||||
public List<ENTITY> getSimpleTaskList();
|
||||
List<ENTITY> getSimpleTaskList();
|
||||
}
|
||||
|
@ -23,8 +23,4 @@ import com.arialyy.aria.core.inf.IEventListener;
|
||||
*/
|
||||
public interface IUploadListener extends IEventListener {
|
||||
|
||||
/**
|
||||
* 上传完成,服务器返回的状态码
|
||||
*/
|
||||
void onComplete(int state);
|
||||
}
|
||||
|
@ -25,45 +25,45 @@ public interface ISchedulerListener<TASK extends ITask> {
|
||||
* 预处理,有时有些地址链接比较慢,这时可以先在这个地方出来一些界面上的UI,如按钮的状态。
|
||||
* 在这个回调中,任务是获取不到文件大小,下载速度等参数
|
||||
*/
|
||||
public void onPre(TASK task);
|
||||
void onPre(TASK task);
|
||||
|
||||
/**
|
||||
* 任务预加载完成
|
||||
*/
|
||||
public void onTaskPre(TASK task);
|
||||
void onTaskPre(TASK task);
|
||||
|
||||
/**
|
||||
* 任务恢复下载
|
||||
*/
|
||||
public void onTaskResume(TASK task);
|
||||
void onTaskResume(TASK task);
|
||||
|
||||
/**
|
||||
* 任务开始
|
||||
*/
|
||||
public void onTaskStart(TASK task);
|
||||
void onTaskStart(TASK task);
|
||||
|
||||
/**
|
||||
* 任务停止
|
||||
*/
|
||||
public void onTaskStop(TASK task);
|
||||
void onTaskStop(TASK task);
|
||||
|
||||
/**
|
||||
* 任务取消
|
||||
*/
|
||||
public void onTaskCancel(TASK task);
|
||||
void onTaskCancel(TASK task);
|
||||
|
||||
/**
|
||||
* 任务下载失败
|
||||
*/
|
||||
public void onTaskFail(TASK task);
|
||||
void onTaskFail(TASK task);
|
||||
|
||||
/**
|
||||
* 任务完成
|
||||
*/
|
||||
public void onTaskComplete(TASK task);
|
||||
void onTaskComplete(TASK task);
|
||||
|
||||
/**
|
||||
* 任务执行中
|
||||
*/
|
||||
public void onTaskRunning(TASK task);
|
||||
void onTaskRunning(TASK task);
|
||||
}
|
@ -0,0 +1,138 @@
|
||||
/*
|
||||
* 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.upload;
|
||||
|
||||
import android.os.Handler;
|
||||
import com.arialyy.aria.core.AriaManager;
|
||||
import com.arialyy.aria.core.inf.AbsEntity;
|
||||
import com.arialyy.aria.core.inf.AbsTask;
|
||||
import com.arialyy.aria.core.inf.IEntity;
|
||||
import com.arialyy.aria.core.inf.IUploadListener;
|
||||
import com.arialyy.aria.core.scheduler.ISchedulers;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
/**
|
||||
* 下载监听类
|
||||
*/
|
||||
class BaseUListener<ENTITY extends AbsEntity, TASK extends AbsTask<ENTITY>>
|
||||
implements IUploadListener {
|
||||
private WeakReference<Handler> outHandler;
|
||||
private long mLastLen = 0; //上一次发送长度
|
||||
private boolean isFirst = true;
|
||||
protected ENTITY mEntity;
|
||||
protected TASK mTask;
|
||||
private boolean isConvertSpeed = false;
|
||||
boolean isWait = false;
|
||||
|
||||
BaseUListener(TASK task, Handler outHandler) {
|
||||
this.outHandler = new WeakReference<>(outHandler);
|
||||
this.mTask = new WeakReference<>(task).get();
|
||||
this.mEntity = this.mTask.getEntity();
|
||||
final AriaManager manager = AriaManager.getInstance(AriaManager.APP);
|
||||
isConvertSpeed = manager.getDownloadConfig().isConvertSpeed();
|
||||
mLastLen = mEntity.getCurrentProgress();
|
||||
}
|
||||
|
||||
@Override public void onPre() {
|
||||
saveData(IEntity.STATE_PRE, -1);
|
||||
sendInState2Target(ISchedulers.PRE);
|
||||
}
|
||||
|
||||
@Override public void onStart(long startLocation) {
|
||||
saveData(IEntity.STATE_RUNNING, startLocation);
|
||||
sendInState2Target(ISchedulers.START);
|
||||
}
|
||||
|
||||
@Override public void onResume(long resumeLocation) {
|
||||
saveData(IEntity.STATE_RUNNING, resumeLocation);
|
||||
sendInState2Target(ISchedulers.RESUME);
|
||||
}
|
||||
|
||||
@Override public void onProgress(long currentLocation) {
|
||||
mEntity.setCurrentProgress(currentLocation);
|
||||
long speed = currentLocation - mLastLen;
|
||||
if (isFirst) {
|
||||
speed = 0;
|
||||
isFirst = false;
|
||||
}
|
||||
handleSpeed(speed);
|
||||
sendInState2Target(ISchedulers.RUNNING);
|
||||
mLastLen = currentLocation;
|
||||
}
|
||||
|
||||
@Override public void onStop(long stopLocation) {
|
||||
saveData(isWait ? IEntity.STATE_WAIT : IEntity.STATE_STOP, stopLocation);
|
||||
handleSpeed(0);
|
||||
sendInState2Target(ISchedulers.STOP);
|
||||
}
|
||||
|
||||
@Override public void onCancel() {
|
||||
saveData(IEntity.STATE_CANCEL, -1);
|
||||
handleSpeed(0);
|
||||
sendInState2Target(ISchedulers.CANCEL);
|
||||
}
|
||||
|
||||
@Override public void onComplete() {
|
||||
saveData(IEntity.STATE_COMPLETE, mEntity.getFileSize());
|
||||
handleSpeed(0);
|
||||
sendInState2Target(ISchedulers.COMPLETE);
|
||||
}
|
||||
|
||||
@Override public void onFail() {
|
||||
mEntity.setFailNum(mEntity.getFailNum() + 1);
|
||||
saveData(IEntity.STATE_FAIL, mEntity.getCurrentProgress());
|
||||
handleSpeed(0);
|
||||
sendInState2Target(ISchedulers.FAIL);
|
||||
}
|
||||
|
||||
private void handleSpeed(long speed) {
|
||||
if (isConvertSpeed) {
|
||||
mEntity.setConvertSpeed(CommonUtil.formatFileSize(speed < 0 ? 0 : speed) + "/s");
|
||||
} else {
|
||||
mEntity.setSpeed(speed < 0 ? 0 : speed);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将任务状态发送给下载器
|
||||
*
|
||||
* @param state {@link ISchedulers#START}
|
||||
*/
|
||||
private void sendInState2Target(int state) {
|
||||
if (outHandler.get() != null) {
|
||||
outHandler.get().obtainMessage(state, mTask).sendToTarget();
|
||||
}
|
||||
}
|
||||
|
||||
private void saveData(int state, long location) {
|
||||
mEntity.setComplete(state == IEntity.STATE_COMPLETE);
|
||||
if (state == IEntity.STATE_CANCEL) {
|
||||
mEntity.deleteData();
|
||||
} else if (state == IEntity.STATE_COMPLETE) {
|
||||
mEntity.setState(state);
|
||||
mEntity.setCompleteTime(System.currentTimeMillis());
|
||||
mEntity.setCurrentProgress(mEntity.getFileSize());
|
||||
mEntity.update();
|
||||
} else {
|
||||
mEntity.setState(state);
|
||||
if (location != -1) {
|
||||
mEntity.setCurrentProgress(location);
|
||||
}
|
||||
mEntity.update();
|
||||
}
|
||||
}
|
||||
}
|
@ -52,8 +52,4 @@ class UploadListener implements IUploadListener {
|
||||
@Override public void onFail() {
|
||||
|
||||
}
|
||||
|
||||
@Override public void onComplete(int state) {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ import java.util.Set;
|
||||
* 上传功能接收器
|
||||
*/
|
||||
public class UploadReceiver extends AbsReceiver<UploadEntity> {
|
||||
private static final String TAG = "DownloadReceiver";
|
||||
private static final String TAG = "UploadReceiver";
|
||||
public ISchedulerListener<UploadTask> listener;
|
||||
|
||||
/**
|
||||
@ -62,7 +62,6 @@ public class UploadReceiver extends AbsReceiver<UploadEntity> {
|
||||
* 通过上传路径获取上传实体
|
||||
*/
|
||||
public UploadEntity getUploadEntity(String filePath) {
|
||||
CheckUtil.checkUploadPath(filePath);
|
||||
return DbEntity.findFirst(UploadEntity.class, "filePath=?", filePath);
|
||||
}
|
||||
|
||||
|
@ -39,6 +39,7 @@ public class UploadTarget extends AbsUploadTarget<UploadTarget, UploadEntity, Up
|
||||
if (mTaskEntity.entity == null) {
|
||||
mTaskEntity.entity = getUploadEntity(filePath);
|
||||
}
|
||||
mEntity = mTaskEntity.entity;
|
||||
File file = new File(filePath);
|
||||
mEntity.setFileSize(file.length());
|
||||
mEntity = mTaskEntity.entity;
|
||||
|
@ -35,11 +35,11 @@ import org.apache.commons.net.ftp.FTPReply;
|
||||
|
||||
/**
|
||||
* Created by Aria.Lao on 2017/7/28.
|
||||
* FTP 单线程下载器
|
||||
* FTP 单线程上传任务,需要FTP 服务器给用户打开删除和读入IO的权限
|
||||
*/
|
||||
class FtpThreadTask extends AbsThreadTask<UploadEntity, UploadTaskEntity> {
|
||||
private final String TAG = "FtpThreadTask";
|
||||
private String remotePath, charSet;
|
||||
private String dir, remotePath, charSet;
|
||||
|
||||
FtpThreadTask(StateConstance constance, IEventListener listener,
|
||||
SubThreadConfig<UploadTaskEntity> info) {
|
||||
@ -64,8 +64,8 @@ class FtpThreadTask extends AbsThreadTask<UploadEntity, UploadTaskEntity> {
|
||||
mChildCurrentLocation = mConfig.START_LOCATION;
|
||||
client = createClient();
|
||||
if (client == null) return;
|
||||
client.makeDirectory(remotePath);
|
||||
client.changeWorkingDirectory(remotePath);
|
||||
client.makeDirectory(dir);
|
||||
client.changeWorkingDirectory(dir);
|
||||
client.setRestartOffset(mConfig.START_LOCATION);
|
||||
file = new BufferedRandomAccessFile(mConfig.TEMP_FILE, "rwd", mBufSize);
|
||||
file.seek(mConfig.START_LOCATION);
|
||||
@ -154,9 +154,8 @@ class FtpThreadTask extends AbsThreadTask<UploadEntity, UploadTaskEntity> {
|
||||
String[] pp = url.split("/")[2].split(":");
|
||||
String serverIp = pp[0];
|
||||
int port = Integer.parseInt(pp[1]);
|
||||
remotePath = url.substring(url.indexOf(pp[1]) + pp[1].length(), url.length())
|
||||
+ "/"
|
||||
+ mEntity.getFileName();
|
||||
dir = url.substring(url.indexOf(pp[1]) + pp[1].length(), url.length());
|
||||
remotePath = dir + "/" + mEntity.getFileName();
|
||||
FTPClient client = new FTPClient();
|
||||
client.connect(serverIp, port);
|
||||
if (!TextUtils.isEmpty(mTaskEntity.account)) {
|
||||
|
@ -49,7 +49,7 @@ class HttpThreadTask extends AbsThreadTask<UploadEntity, UploadTaskEntity> {
|
||||
private long mCurrentLocation = 0;
|
||||
private OutputStream mOutputStream;
|
||||
|
||||
public HttpThreadTask(StateConstance constance, IUploadListener listener,
|
||||
HttpThreadTask(StateConstance constance, IUploadListener listener,
|
||||
SubThreadConfig<UploadTaskEntity> uploadInfo) {
|
||||
super(constance, listener, uploadInfo);
|
||||
}
|
||||
@ -89,7 +89,6 @@ class HttpThreadTask extends AbsThreadTask<UploadEntity, UploadTaskEntity> {
|
||||
for (String key : keys) {
|
||||
addFormField(writer, key, mTaskEntity.formFields.get(key));
|
||||
}
|
||||
mListener.onStart(0);
|
||||
uploadFile(writer, mTaskEntity.attachment, uploadFile);
|
||||
Log.d(TAG, finish(writer) + "");
|
||||
} catch (IOException e) {
|
||||
@ -101,6 +100,7 @@ class HttpThreadTask extends AbsThreadTask<UploadEntity, UploadTaskEntity> {
|
||||
private void fail() {
|
||||
try {
|
||||
mListener.onFail();
|
||||
STATE.isRunning = false;
|
||||
if (mOutputStream != null) {
|
||||
mOutputStream.close();
|
||||
}
|
||||
@ -167,9 +167,11 @@ class HttpThreadTask extends AbsThreadTask<UploadEntity, UploadTaskEntity> {
|
||||
writer.flush();
|
||||
if (STATE.isCancel) {
|
||||
mListener.onCancel();
|
||||
STATE.isRunning = false;
|
||||
return;
|
||||
}
|
||||
mListener.onComplete();
|
||||
STATE.isRunning = false;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -195,7 +197,7 @@ class HttpThreadTask extends AbsThreadTask<UploadEntity, UploadTaskEntity> {
|
||||
mHttpConn.disconnect();
|
||||
} else {
|
||||
Log.w(TAG, "state_code = " + status);
|
||||
mListener.onFail();
|
||||
fail();
|
||||
}
|
||||
|
||||
writer.flush();
|
||||
|
@ -23,17 +23,17 @@ import com.arialyy.aria.util.CheckUtil;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2017/2/9.
|
||||
* 简单的http文件上传工具
|
||||
* 简单的文件上传工具
|
||||
*/
|
||||
public class SimpleHttpUploadUtil implements IUtil, Runnable {
|
||||
private static final String TAG = "SimpleHttpUploadUtil";
|
||||
public class SimpleUploadUtil implements IUtil, Runnable {
|
||||
private static final String TAG = "SimpleUploadUtil";
|
||||
|
||||
private UploadEntity mUploadEntity;
|
||||
private UploadTaskEntity mTaskEntity;
|
||||
private IUploadListener mListener;
|
||||
private Uploader mUploader;
|
||||
|
||||
public SimpleHttpUploadUtil(UploadTaskEntity taskEntity, IUploadListener listener) {
|
||||
public SimpleUploadUtil(UploadTaskEntity taskEntity, IUploadListener listener) {
|
||||
mTaskEntity = taskEntity;
|
||||
CheckUtil.checkTaskEntity(taskEntity);
|
||||
mUploadEntity = taskEntity.getEntity();
|
||||
@ -45,6 +45,7 @@ public class SimpleHttpUploadUtil implements IUtil, Runnable {
|
||||
}
|
||||
|
||||
@Override public void run() {
|
||||
mListener.onPre();
|
||||
mUploader.start();
|
||||
}
|
||||
|
||||
@ -69,7 +70,7 @@ public class SimpleHttpUploadUtil implements IUtil, Runnable {
|
||||
}
|
||||
|
||||
@Override public void start() {
|
||||
mUploader.start();
|
||||
new Thread(this).start();
|
||||
}
|
||||
|
||||
@Override public void resume() {
|
@ -47,14 +47,14 @@ class Uploader extends AbsFileer<UploadEntity, UploadTaskEntity> {
|
||||
* 5、不支持断点,则是新任务
|
||||
*/
|
||||
protected void checkTask() {
|
||||
if (!mTaskEntity.isSupportBP) {
|
||||
isNewTask = true;
|
||||
return;
|
||||
}
|
||||
mConfigFile = new File(mContext.getFilesDir().getPath()
|
||||
+ AriaManager.UPLOAD_TEMP_DIR
|
||||
+ mEntity.getFileName()
|
||||
+ ".properties");
|
||||
if (!mTaskEntity.isSupportBP) {
|
||||
isNewTask = true;
|
||||
return;
|
||||
}
|
||||
if (!mConfigFile.exists()) { //记录文件被删除,则重新下载
|
||||
isNewTask = true;
|
||||
CommonUtil.createFile(mConfigFile.getPath());
|
||||
@ -70,7 +70,7 @@ class Uploader extends AbsFileer<UploadEntity, UploadTaskEntity> {
|
||||
|
||||
}
|
||||
|
||||
@Override protected int getThreadNum() {
|
||||
@Override protected int getNewTaskThreadNum() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -150,7 +150,7 @@ public class CommonUtil {
|
||||
/**
|
||||
* 删除上传任务的配置,包括
|
||||
*
|
||||
* @param removeFile {@code true} 不仅删除任务数据库记录,还会删除已经下载完成的文件
|
||||
* @param removeFile {@code true} 不仅删除任务数据库记录,还会删除已经删除完成的文件
|
||||
* {@code false}如果任务已经完成,只删除任务数据库记录
|
||||
*/
|
||||
public static void delUploadTaskConfig(boolean removeFile, UploadTaskEntity tEntity) {
|
||||
@ -160,15 +160,8 @@ public class CommonUtil {
|
||||
if (file.exists()) {
|
||||
file.delete();
|
||||
}
|
||||
} else {
|
||||
if (!uEntity.isComplete()) {
|
||||
if (file.exists()) {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
File config = new File(
|
||||
AriaManager.APP.getFilesDir().getPath() + "/temp/" + uEntity.getFileName() + ".properties");
|
||||
File config = new File(getFileConfig(false, uEntity.getFileName()));
|
||||
if (config.exists()) {
|
||||
config.delete();
|
||||
}
|
||||
@ -197,8 +190,7 @@ public class CommonUtil {
|
||||
}
|
||||
}
|
||||
|
||||
File config = new File(
|
||||
AriaManager.APP.getFilesDir().getPath() + "/temp/" + dEntity.getFileName() + ".properties");
|
||||
File config = new File(getFileConfig(true, dEntity.getFileName()));
|
||||
if (config.exists()) {
|
||||
config.delete();
|
||||
}
|
||||
|
Reference in New Issue
Block a user