优化FtpFileInfoTask的代码逻辑

pull/617/head
laoyuyu 5 years ago
parent 8158c58d6e
commit b1a2f232fa
  1. 92
      FtpComponent/src/main/java/com/arialyy/aria/ftp/AbsFtpInfoTask.java
  2. 13
      FtpComponent/src/main/java/com/arialyy/aria/ftp/FtpTaskOption.java
  3. 58
      FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDFileInfoTask.java
  4. 59
      FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDGInfoTask.java
  5. 3
      FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDLoaderUtil.java
  6. 12
      FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDRecordHandler.java
  7. 3
      FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpSubDLoaderUtil.java
  8. 114
      FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpUFileInfoTask.java
  9. 42
      FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpULoader.java
  10. 3
      FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpULoaderUtil.java
  11. 112
      FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpURecordHandler.java
  12. 8
      PublicComponent/src/main/java/com/arialyy/aria/core/loader/NormalLoader.java

@ -37,7 +37,6 @@ import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.util.SSLContextUtil; import com.arialyy.aria.util.SSLContextUtil;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
@ -57,7 +56,6 @@ public abstract class AbsFtpInfoTask<ENTITY extends AbsEntity, TASK_WRAPPER exte
private int mConnectTimeOut; private int mConnectTimeOut;
protected long mSize = 0; protected long mSize = 0;
protected String charSet = "UTF-8"; protected String charSet = "UTF-8";
protected boolean isUpload = false;
protected Callback callback; protected Callback callback;
public AbsFtpInfoTask(TASK_WRAPPER taskWrapper) { public AbsFtpInfoTask(TASK_WRAPPER taskWrapper) {
@ -74,6 +72,16 @@ public abstract class AbsFtpInfoTask<ENTITY extends AbsEntity, TASK_WRAPPER exte
*/ */
protected abstract String getRemotePath(); protected abstract String getRemotePath();
/**
* 处理ftp列表信息
*
* @param client ftp 客户端对象
* @param files remotePath 对应的文件列表
* @param convertedRemotePath 已转换的可被服务器识别的remotePath
*/
protected abstract void handelFileInfo(FTPClient client, FTPFile[] files,
String convertedRemotePath) throws IOException;
@Override public void setCallback(Callback callback) { @Override public void setCallback(Callback callback) {
this.callback = callback; this.callback = callback;
} }
@ -89,67 +97,9 @@ public abstract class AbsFtpInfoTask<ENTITY extends AbsEntity, TASK_WRAPPER exte
ALog.e(TAG, String.format("任务【%s】失败", mTaskOption.getUrlEntity().url)); ALog.e(TAG, String.format("任务【%s】失败", mTaskOption.getUrlEntity().url));
return; return;
} }
String remotePath = CommonUtil.convertFtpChar(charSet, getRemotePath()); String convertedRemotePath = CommonUtil.convertFtpChar(charSet, getRemotePath());
FTPFile[] files = client.listFiles(convertedRemotePath);
FTPFile[] files = client.listFiles(remotePath); handelFileInfo(client, files, convertedRemotePath);
boolean isExist = files.length != 0;
if (!isExist && !isUpload) {
int i = remotePath.lastIndexOf(File.separator);
FTPFile[] files1;
if (i == -1) {
files1 = client.listFiles();
} else {
files1 = client.listFiles(remotePath.substring(0, i + 1));
}
if (files1.length > 0) {
ALog.i(TAG,
String.format("路径【%s】下的文件列表 ===================================", getRemotePath()));
for (FTPFile file : files1) {
ALog.d(TAG, file.toString());
}
ALog.i(TAG,
"================================= --end-- ===================================");
} else {
ALog.w(TAG, String.format("获取文件列表失败,msg:%s", client.getReplyString()));
}
closeClient(client);
failDownload(client,
String.format("文件不存在,url: %s, remotePath:%s", mTaskOption.getUrlEntity().url,
remotePath), null, false);
return;
}
// 处理拦截功能
if (!onInterceptor(client, files)) {
closeClient(client);
ALog.d(TAG, "拦截器处理完成任务");
return;
}
//为了防止编码错乱,需要使用原始字符串
if (isUpload && files.length == 0){
handleFile(getRemotePath(), null);
}else {
mSize = getFileSize(files, client, getRemotePath());
}
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
if (isUpload) {
//服务器上没有该文件路径,表示该任务为新的上传任务
mTaskWrapper.setNewTask(true);
} else {
closeClient(client);
failDownload(client, "获取文件信息错误,url: " + mTaskOption.getUrlEntity().url, null, true);
return;
}
}
mTaskWrapper.setCode(reply);
if (mSize != 0 && !isUpload) {
mEntity.setFileSize(mSize);
}
onPreComplete(reply);
mEntity.update();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
failDownload(client, "FTP错误信息", e, true); failDownload(client, "FTP错误信息", e, true);
@ -171,20 +121,6 @@ public abstract class AbsFtpInfoTask<ENTITY extends AbsEntity, TASK_WRAPPER exte
return true; return true;
} }
/**
* 检查文件是否存在
*
* @return {@code true}存在
*/
private boolean checkFileExist(FTPFile[] ftpFiles, String fileName) {
for (FTPFile ff : ftpFiles) {
if (ff.getName().equals(fileName)) {
return true;
}
}
return false;
}
protected void onPreComplete(int code) { protected void onPreComplete(int code) {
} }
@ -358,7 +294,7 @@ public abstract class AbsFtpInfoTask<ENTITY extends AbsEntity, TASK_WRAPPER exte
* *
* @throws IOException 字符串编码转换错误 * @throws IOException 字符串编码转换错误
*/ */
private long getFileSize(FTPFile[] files, FTPClient client, String dirName) throws IOException { protected long getFileSize(FTPFile[] files, FTPClient client, String dirName) throws IOException {
long size = 0; long size = 0;
String path = dirName + "/"; String path = dirName + "/";
for (FTPFile file : files) { for (FTPFile file : files) {

@ -72,11 +72,6 @@ public final class FtpTaskOption implements ITaskOption {
*/ */
private String activeExternalIPAddress; private String activeExternalIPAddress;
/**
* 服务器文件是否存在true 文件存在用于ftp上传
*/
private boolean serveFileIsExist = true;
//---------------- ftp client 配置信息 start //---------------- ftp client 配置信息 start
private String defaultDateFormatStr = null; private String defaultDateFormatStr = null;
private String recentDateFormatStr = null; private String recentDateFormatStr = null;
@ -86,14 +81,6 @@ public final class FtpTaskOption implements ITaskOption {
private String systemKey = FTPClientConfig.SYST_UNIX; private String systemKey = FTPClientConfig.SYST_UNIX;
//---------------- ftp client 配置信息 end //---------------- ftp client 配置信息 end
public boolean isServeFileIsExist() {
return serveFileIsExist;
}
public void setServeFileIsExist(boolean serveFileIsExist) {
this.serveFileIsExist = serveFileIsExist;
}
public String getActiveExternalIPAddress() { public String getActiveExternalIPAddress() {
return activeExternalIPAddress; return activeExternalIPAddress;
} }

@ -15,7 +15,9 @@
*/ */
package com.arialyy.aria.ftp.download; package com.arialyy.aria.ftp.download;
import aria.apache.commons.net.ftp.FTPClient;
import aria.apache.commons.net.ftp.FTPFile; import aria.apache.commons.net.ftp.FTPFile;
import aria.apache.commons.net.ftp.FTPReply;
import com.arialyy.aria.core.common.CompleteInfo; import com.arialyy.aria.core.common.CompleteInfo;
import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadEntity;
@ -23,6 +25,8 @@ import com.arialyy.aria.exception.AriaIOException;
import com.arialyy.aria.ftp.AbsFtpInfoTask; import com.arialyy.aria.ftp.AbsFtpInfoTask;
import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.FileUtil; import com.arialyy.aria.util.FileUtil;
import java.io.File;
import java.io.IOException;
/** /**
* Created by Aria.Lao on 2017/7/25. * Created by Aria.Lao on 2017/7/25.
@ -43,6 +47,60 @@ final class FtpDFileInfoTask extends AbsFtpInfoTask<DownloadEntity, DTaskWrapper
} }
} }
@Override
protected void handelFileInfo(FTPClient client, FTPFile[] files, String convertedRemotePath)
throws IOException {
boolean isExist = files.length != 0;
if (!isExist) {
int i = convertedRemotePath.lastIndexOf(File.separator);
FTPFile[] files1;
if (i == -1) {
files1 = client.listFiles();
} else {
files1 = client.listFiles(convertedRemotePath.substring(0, i + 1));
}
if (files1.length > 0) {
ALog.i(TAG,
String.format("路径【%s】下的文件列表 ===================================", getRemotePath()));
for (FTPFile file : files1) {
ALog.d(TAG, file.toString());
}
ALog.i(TAG,
"================================= --end-- ===================================");
} else {
ALog.w(TAG, String.format("获取文件列表失败,msg:%s", client.getReplyString()));
}
closeClient(client);
failDownload(client,
String.format("文件不存在,url: %s, remotePath:%s", mTaskOption.getUrlEntity().url,
getRemotePath()), null, false);
return;
}
// 处理拦截功能
if (!onInterceptor(client, files)) {
closeClient(client);
ALog.d(TAG, "拦截器处理完成任务");
return;
}
//为了防止编码错乱,需要使用原始字符串
mSize = getFileSize(files, client, getRemotePath());
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
closeClient(client);
failDownload(client, "获取文件信息错误,url: " + mTaskOption.getUrlEntity().url, null, true);
return;
}
mTaskWrapper.setCode(reply);
if (mSize != 0) {
mEntity.setFileSize(mSize);
}
onPreComplete(reply);
mEntity.update();
}
@Override protected String getRemotePath() { @Override protected String getRemotePath() {
return mTaskOption.getUrlEntity().remotePath; return mTaskOption.getUrlEntity().remotePath;
} }

@ -19,6 +19,7 @@ import android.net.Uri;
import android.text.TextUtils; import android.text.TextUtils;
import aria.apache.commons.net.ftp.FTPClient; import aria.apache.commons.net.ftp.FTPClient;
import aria.apache.commons.net.ftp.FTPFile; import aria.apache.commons.net.ftp.FTPFile;
import aria.apache.commons.net.ftp.FTPReply;
import com.arialyy.aria.core.FtpUrlEntity; import com.arialyy.aria.core.FtpUrlEntity;
import com.arialyy.aria.core.common.CompleteInfo; import com.arialyy.aria.core.common.CompleteInfo;
import com.arialyy.aria.core.download.DGTaskWrapper; import com.arialyy.aria.core.download.DGTaskWrapper;
@ -32,6 +33,8 @@ import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.util.RecordUtil; import com.arialyy.aria.util.RecordUtil;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
@ -52,6 +55,60 @@ final class FtpDGInfoTask extends AbsFtpInfoTask<DownloadGroupEntity, DGTaskWrap
} }
} }
@Override
protected void handelFileInfo(FTPClient client, FTPFile[] files, String convertedRemotePath)
throws IOException {
boolean isExist = files.length != 0;
if (!isExist) {
int i = convertedRemotePath.lastIndexOf(File.separator);
FTPFile[] files1;
if (i == -1) {
files1 = client.listFiles();
} else {
files1 = client.listFiles(convertedRemotePath.substring(0, i + 1));
}
if (files1.length > 0) {
ALog.i(TAG,
String.format("路径【%s】下的文件列表 ===================================", getRemotePath()));
for (FTPFile file : files1) {
ALog.d(TAG, file.toString());
}
ALog.i(TAG,
"================================= --end-- ===================================");
} else {
ALog.w(TAG, String.format("获取文件列表失败,msg:%s", client.getReplyString()));
}
closeClient(client);
failDownload(client,
String.format("文件不存在,url: %s, remotePath:%s", mTaskOption.getUrlEntity().url,
getRemotePath()), null, false);
return;
}
// 处理拦截功能
if (!onInterceptor(client, files)) {
closeClient(client);
ALog.d(TAG, "拦截器处理完成任务");
return;
}
//为了防止编码错乱,需要使用原始字符串
mSize = getFileSize(files, client, getRemotePath());
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
closeClient(client);
failDownload(client, "获取文件信息错误,url: " + mTaskOption.getUrlEntity().url, null, true);
return;
}
mTaskWrapper.setCode(reply);
if (mSize != 0) {
mEntity.setFileSize(mSize);
}
onPreComplete(reply);
mEntity.update();
}
/** /**
* 检查子任务的任务设置 * 检查子任务的任务设置
* *
@ -110,7 +167,7 @@ final class FtpDGInfoTask extends AbsFtpInfoTask<DownloadGroupEntity, DGTaskWrap
final FtpUrlEntity urlEntity = mTaskOption.getUrlEntity().clone(); final FtpUrlEntity urlEntity = mTaskOption.getUrlEntity().clone();
String url = String url =
urlEntity.scheme + "://" + urlEntity.hostName + ":" + urlEntity.port + "/" + remotePath; urlEntity.scheme + "://" + urlEntity.hostName + ":" + urlEntity.port + "/" + remotePath;
if (checkEntityExist(url)){ if (checkEntityExist(url)) {
ALog.w(TAG, "子任务已存在,取消子任务的添加,url = " + url); ALog.w(TAG, "子任务已存在,取消子任务的添加,url = " + url);
return; return;
} }

@ -23,7 +23,6 @@ import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.loader.NormalLoader; import com.arialyy.aria.core.loader.NormalLoader;
import com.arialyy.aria.core.loader.NormalThreadStateManager; import com.arialyy.aria.core.loader.NormalThreadStateManager;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.ftp.FtpRecordHandler;
import com.arialyy.aria.ftp.FtpTaskOption; import com.arialyy.aria.ftp.FtpTaskOption;
/** /**
@ -43,7 +42,7 @@ public final class FtpDLoaderUtil extends AbsNormalLoaderUtil {
public LoaderStructure BuildLoaderStructure() { public LoaderStructure BuildLoaderStructure() {
LoaderStructure structure = new LoaderStructure(); LoaderStructure structure = new LoaderStructure();
structure.addComponent(new FtpRecordHandler(getTaskWrapper())) structure.addComponent(new FtpDRecordHandler(getTaskWrapper()))
.addComponent(new NormalThreadStateManager(getListener())) .addComponent(new NormalThreadStateManager(getListener()))
.addComponent(new FtpDFileInfoTask((DTaskWrapper) getTaskWrapper())) .addComponent(new FtpDFileInfoTask((DTaskWrapper) getTaskWrapper()))
.addComponent(new FtpDTTBuilder(getTaskWrapper())); .addComponent(new FtpDTTBuilder(getTaskWrapper()));

@ -13,16 +13,16 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.arialyy.aria.ftp; package com.arialyy.aria.ftp.download;
import com.arialyy.aria.core.TaskRecord; import com.arialyy.aria.core.TaskRecord;
import com.arialyy.aria.core.ThreadRecord; import com.arialyy.aria.core.ThreadRecord;
import com.arialyy.aria.core.common.RecordHandler; import com.arialyy.aria.core.common.RecordHandler;
import com.arialyy.aria.core.common.RecordHelper; import com.arialyy.aria.core.common.RecordHelper;
import com.arialyy.aria.core.config.Configuration; import com.arialyy.aria.core.config.Configuration;
import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.loader.IRecordHandler; import com.arialyy.aria.core.loader.IRecordHandler;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.core.wrapper.ITaskWrapper; import com.arialyy.aria.core.wrapper.ITaskWrapper;
import com.arialyy.aria.util.RecordUtil; import com.arialyy.aria.util.RecordUtil;
import java.util.ArrayList; import java.util.ArrayList;
@ -31,17 +31,13 @@ import java.util.ArrayList;
* @Author lyy * @Author lyy
* @Date 2019-09-19 * @Date 2019-09-19
*/ */
public final class FtpRecordHandler extends RecordHandler { final class FtpDRecordHandler extends RecordHandler {
public FtpRecordHandler(AbsTaskWrapper wrapper) { FtpDRecordHandler(DTaskWrapper wrapper) {
super(wrapper); super(wrapper);
} }
@Override public void handlerTaskRecord(TaskRecord record) { @Override public void handlerTaskRecord(TaskRecord record) {
if (getWrapper().getRequestType() == ITaskWrapper.U_FTP) {
// 上传任务记录的处理交由FtpUFileInfoTask 完成
return;
}
RecordHelper helper = new RecordHelper(getWrapper(), record); RecordHelper helper = new RecordHelper(getWrapper(), record);
if (record.threadNum == 1) { if (record.threadNum == 1) {
helper.handleSingleThreadRecord(); helper.handleSingleThreadRecord();

@ -20,7 +20,6 @@ import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.group.AbsSubDLoadUtil; import com.arialyy.aria.core.group.AbsSubDLoadUtil;
import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.loader.SubLoader; import com.arialyy.aria.core.loader.SubLoader;
import com.arialyy.aria.ftp.FtpRecordHandler;
/** /**
* @Author lyy * @Author lyy
@ -47,7 +46,7 @@ final class FtpSubDLoaderUtil extends AbsSubDLoadUtil {
@Override protected LoaderStructure buildLoaderStructure() { @Override protected LoaderStructure buildLoaderStructure() {
LoaderStructure structure = new LoaderStructure(); LoaderStructure structure = new LoaderStructure();
structure.addComponent(new FtpRecordHandler(getWrapper())) structure.addComponent(new FtpDRecordHandler(getWrapper()))
.addComponent(new FtpDTTBuilder(getWrapper())) .addComponent(new FtpDTTBuilder(getWrapper()))
.addComponent(new FtpDFileInfoTask(getWrapper())); .addComponent(new FtpDFileInfoTask(getWrapper()));
structure.accept(getLoader()); structure.accept(getLoader());

@ -18,19 +18,15 @@ package com.arialyy.aria.ftp.upload;
import android.text.TextUtils; import android.text.TextUtils;
import aria.apache.commons.net.ftp.FTPClient; import aria.apache.commons.net.ftp.FTPClient;
import aria.apache.commons.net.ftp.FTPFile; import aria.apache.commons.net.ftp.FTPFile;
import com.arialyy.aria.core.TaskRecord; import aria.apache.commons.net.ftp.FTPReply;
import com.arialyy.aria.core.ThreadRecord;
import com.arialyy.aria.core.common.CompleteInfo; import com.arialyy.aria.core.common.CompleteInfo;
import com.arialyy.aria.core.processor.FtpInterceptHandler; import com.arialyy.aria.core.processor.FtpInterceptHandler;
import com.arialyy.aria.core.processor.IFtpUploadInterceptor; import com.arialyy.aria.core.processor.IFtpUploadInterceptor;
import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.core.upload.UTaskWrapper;
import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.ftp.AbsFtpInfoTask; import com.arialyy.aria.ftp.AbsFtpInfoTask;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.util.DbDataHelper;
import com.arialyy.aria.util.RecordUtil;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -43,10 +39,10 @@ final class FtpUFileInfoTask extends AbsFtpInfoTask<UploadEntity, UTaskWrapper>
static final int CODE_COMPLETE = 0xab1; static final int CODE_COMPLETE = 0xab1;
private boolean isComplete = false; private boolean isComplete = false;
private String remotePath; private String remotePath;
private FTPFile ftpFile;
FtpUFileInfoTask(UTaskWrapper taskEntity) { FtpUFileInfoTask(UTaskWrapper taskEntity) {
super(taskEntity); super(taskEntity);
isUpload = true;
} }
@Override protected String getRemotePath() { @Override protected String getRemotePath() {
@ -54,6 +50,30 @@ final class FtpUFileInfoTask extends AbsFtpInfoTask<UploadEntity, UTaskWrapper>
mTaskOption.getUrlEntity().remotePath + "/" + mEntity.getFileName() : remotePath; mTaskOption.getUrlEntity().remotePath + "/" + mEntity.getFileName() : remotePath;
} }
@Override
protected void handelFileInfo(FTPClient client, FTPFile[] files, String convertedRemotePath)
throws IOException {
// 处理拦截功能
if (!onInterceptor(client, files)) {
closeClient(client);
ALog.d(TAG, "拦截器处理完成任务");
return;
}
//为了防止编码错乱,需要使用原始字符串
if (files.length == 0) {
handleFile(getRemotePath(), null);
}
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
//服务器上没有该文件路径,表示该任务为新的上传任务
mTaskWrapper.setNewTask(true);
}
mTaskWrapper.setCode(reply);
onPreComplete(reply);
mEntity.update();
}
@Override protected boolean onInterceptor(FTPClient client, FTPFile[] ftpFiles) { @Override protected boolean onInterceptor(FTPClient client, FTPFile[] ftpFiles) {
// 旧任务将不做处理,否则断点续传上传将失效 // 旧任务将不做处理,否则断点续传上传将失效
//if (!mTaskWrapper.isNewTask()) { //if (!mTaskWrapper.isNewTask()) {
@ -86,7 +106,6 @@ final class FtpUFileInfoTask extends AbsFtpInfoTask<UploadEntity, UTaskWrapper>
ALog.d(TAG, ALog.d(TAG,
String.format("删除文件%s,code: %s, msg: %s", b ? "成功" : "失败", client.getReplyCode(), String.format("删除文件%s,code: %s, msg: %s", b ? "成功" : "失败", client.getReplyCode(),
client.getReplyString())); client.getReplyString()));
mTaskOption.setServeFileIsExist(false);
} else if (!TextUtils.isEmpty(interceptHandler.getNewFileName())) { } else if (!TextUtils.isEmpty(interceptHandler.getNewFileName())) {
ALog.i(TAG, String.format("远端已拥有同名文件,将修改remotePath,原文件名:%s,新文件名:%s", ALog.i(TAG, String.format("远端已拥有同名文件,将修改remotePath,原文件名:%s,新文件名:%s",
mEntity.getFileName(), interceptHandler.getNewFileName())); mEntity.getFileName(), interceptHandler.getNewFileName()));
@ -95,7 +114,6 @@ final class FtpUFileInfoTask extends AbsFtpInfoTask<UploadEntity, UTaskWrapper>
+ interceptHandler.getNewFileName(); + interceptHandler.getNewFileName();
mTaskOption.setNewFileName(interceptHandler.getNewFileName()); mTaskOption.setNewFileName(interceptHandler.getNewFileName());
mTaskOption.setServeFileIsExist(false);
closeClient(client); closeClient(client);
run(); run();
return false; return false;
@ -120,84 +138,16 @@ final class FtpUFileInfoTask extends AbsFtpInfoTask<UploadEntity, UTaskWrapper>
*/ */
@Override protected void handleFile(String remotePath, FTPFile ftpFile) { @Override protected void handleFile(String remotePath, FTPFile ftpFile) {
super.handleFile(remotePath, ftpFile); super.handleFile(remotePath, ftpFile);
if (ftpFile != null) { this.ftpFile = ftpFile;
//远程文件已完成 if (ftpFile != null && ftpFile.getSize() == mEntity.getFileSize()) {
if (ftpFile.getSize() == mEntity.getFileSize()) { isComplete = true;
isComplete = true;
ALog.d(TAG, "FTP服务器上已存在该文件【" + ftpFile.getName() + "】");
} else if (ftpFile.getSize() == 0) {
mTaskWrapper.setNewTask(true);
ALog.d(TAG, "FTP服务器上已存在该文件【" + ftpFile.getName() + "】,但文件长度为0,重新上传该文件");
} else {
ALog.w(TAG, "FTP服务器已存在未完成的文件【"
+ ftpFile.getName()
+ ",size: "
+ ftpFile.getSize()
+ "】"
+ "尝试从位置:"
+ (ftpFile.getSize() - 1)
+ "开始上传");
mTaskWrapper.setNewTask(false);
// 修改记录
TaskRecord record = DbDataHelper.getTaskRecord(mTaskWrapper.getKey(),
mTaskWrapper.getEntity().getTaskType());
if (record == null) {
record = new TaskRecord();
record.fileName = mEntity.getFileName();
record.filePath = mTaskWrapper.getKey();
record.threadRecords = new ArrayList<>();
}
ThreadRecord threadRecord;
if (record.threadRecords == null || record.threadRecords.isEmpty()) {
threadRecord = new ThreadRecord();
threadRecord.taskKey = record.filePath;
} else {
threadRecord = record.threadRecords.get(0);
}
//修改本地保存的停止地址为服务器上对应文件的大小
threadRecord.startLocation = ftpFile.getSize() - 1;
record.save();
threadRecord.save();
}
}else {
ALog.d(TAG, "FTP服务器上不存在该文件");
mTaskWrapper.setNewTask(false);
TaskRecord record = DbDataHelper.getTaskRecord(mTaskWrapper.getKey(),
mTaskWrapper.getEntity().getTaskType());
if (record != null){
ALog.w(TAG, String.format("任务记录【%s】已存在,将删除该记录", mTaskWrapper.getKey()));
delTaskRecord(record);
}
mTaskWrapper.setNewTask(true);
record = new TaskRecord();
record.fileName = mEntity.getFileName();
record.filePath = mTaskWrapper.getKey();
record.threadRecords = new ArrayList<>();
ThreadRecord threadRecord = new ThreadRecord();
threadRecord.taskKey = record.filePath;
threadRecord.startLocation = 0;
threadRecord.endLocation = mEntity.getFileSize();
threadRecord.isComplete = false;
record.save();
threadRecord.save();
}
}
private void delTaskRecord(TaskRecord record){
List<ThreadRecord> threadRecords = record.threadRecords;
if (threadRecords != null && !threadRecords.isEmpty()){
for (ThreadRecord tr : threadRecords){
tr.deleteData();
}
} }
record.deleteData();
} }
@Override protected void onPreComplete(int code) { @Override protected void onPreComplete(int code) {
super.onPreComplete(code); super.onPreComplete(code);
callback.onSucceed(mEntity.getKey(), CompleteInfo info = new CompleteInfo(isComplete ? CODE_COMPLETE : code, mTaskWrapper);
new CompleteInfo(isComplete ? CODE_COMPLETE : code, mTaskWrapper)); info.obj = ftpFile;
callback.onSucceed(mEntity.getKey(), info);
} }
} }

@ -15,19 +15,56 @@
*/ */
package com.arialyy.aria.ftp.upload; package com.arialyy.aria.ftp.upload;
import android.os.Handler;
import aria.apache.commons.net.ftp.FTPFile;
import com.arialyy.aria.core.common.AbsEntity; import com.arialyy.aria.core.common.AbsEntity;
import com.arialyy.aria.core.common.CompleteInfo; import com.arialyy.aria.core.common.CompleteInfo;
import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.loader.IInfoTask; import com.arialyy.aria.core.loader.IInfoTask;
import com.arialyy.aria.core.loader.IRecordHandler;
import com.arialyy.aria.core.loader.NormalLoader; import com.arialyy.aria.core.loader.NormalLoader;
import com.arialyy.aria.core.manager.ThreadTaskManager;
import com.arialyy.aria.core.task.IThreadTask;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.exception.BaseException; import com.arialyy.aria.exception.BaseException;
final class FtpULoader extends NormalLoader { final class FtpULoader extends NormalLoader {
private FTPFile ftpFile;
FtpULoader(AbsTaskWrapper wrapper, IEventListener listener) { FtpULoader(AbsTaskWrapper wrapper, IEventListener listener) {
super(wrapper, listener); super(wrapper, listener);
} }
@Override
protected void startThreadTask() {
// 检查记录
((FtpURecordHandler) mRecordHandler).setFtpFile(ftpFile);
if (mRecordHandler.checkTaskCompleted()) {
mRecord.deleteData();
isComplete = true;
getListener().onComplete();
return;
}
mRecord = mRecordHandler.getRecord(getFileSize());
// 初始化线程状态管理器
mStateManager.setLooper(mRecord, getLooper());
getTaskList().addAll(mTTBuilder.buildThreadTask(mRecord,
new Handler(getLooper(), mStateManager.getHandlerCallback())));
mStateManager.updateCurrentProgress(getEntity().getCurrentProgress());
if (mStateManager.getCurrentProgress() > 0) {
getListener().onResume(mStateManager.getCurrentProgress());
} else {
getListener().onStart(mStateManager.getCurrentProgress());
}
// 启动线程任务
for (IThreadTask threadTask : getTaskList()) {
ThreadTaskManager.getInstance().startThread(mTaskWrapper.getKey(), threadTask);
}
}
@Override public void addComponent(IInfoTask infoTask) { @Override public void addComponent(IInfoTask infoTask) {
mInfoTask = infoTask; mInfoTask = infoTask;
infoTask.setCallback(new IInfoTask.Callback() { infoTask.setCallback(new IInfoTask.Callback() {
@ -35,6 +72,7 @@ final class FtpULoader extends NormalLoader {
if (info.code == FtpUFileInfoTask.CODE_COMPLETE) { if (info.code == FtpUFileInfoTask.CODE_COMPLETE) {
getListener().onComplete(); getListener().onComplete();
} else { } else {
ftpFile = (FTPFile) info.obj;
startThreadTask(); startThreadTask();
} }
} }
@ -44,4 +82,8 @@ final class FtpULoader extends NormalLoader {
} }
}); });
} }
@Override public void addComponent(IRecordHandler recordHandler) {
mRecordHandler = recordHandler;
}
} }

@ -22,7 +22,6 @@ import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.loader.NormalThreadStateManager; import com.arialyy.aria.core.loader.NormalThreadStateManager;
import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.core.upload.UTaskWrapper;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.ftp.FtpRecordHandler;
import com.arialyy.aria.ftp.FtpTaskOption; import com.arialyy.aria.ftp.FtpTaskOption;
/** /**
@ -42,7 +41,7 @@ public final class FtpULoaderUtil extends AbsNormalLoaderUtil {
@Override public LoaderStructure BuildLoaderStructure() { @Override public LoaderStructure BuildLoaderStructure() {
LoaderStructure structure = new LoaderStructure(); LoaderStructure structure = new LoaderStructure();
structure.addComponent(new FtpRecordHandler(getTaskWrapper())) structure.addComponent(new FtpURecordHandler((UTaskWrapper) getTaskWrapper()))
.addComponent(new NormalThreadStateManager(getListener())) .addComponent(new NormalThreadStateManager(getListener()))
.addComponent(new FtpUFileInfoTask((UTaskWrapper) getTaskWrapper())) .addComponent(new FtpUFileInfoTask((UTaskWrapper) getTaskWrapper()))
.addComponent(new FtpUTTBuilder(getTaskWrapper())); .addComponent(new FtpUTTBuilder(getTaskWrapper()));

@ -0,0 +1,112 @@
/*
* 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.ftp.upload;
import aria.apache.commons.net.ftp.FTPFile;
import com.arialyy.aria.core.TaskRecord;
import com.arialyy.aria.core.ThreadRecord;
import com.arialyy.aria.core.common.RecordHandler;
import com.arialyy.aria.core.upload.UTaskWrapper;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.RecordUtil;
import java.util.ArrayList;
/**
* 上传任务记录处理器
*/
final class FtpURecordHandler extends RecordHandler {
private FTPFile ftpFile;
FtpURecordHandler(UTaskWrapper wrapper) {
super(wrapper);
}
void setFtpFile(FTPFile ftpFile) {
this.ftpFile = ftpFile;
}
@Override public void handlerTaskRecord(TaskRecord record) {
if (record.threadRecords == null || record.threadRecords.isEmpty()) {
record.threadRecords = new ArrayList<>();
record.threadRecords.add(
createThreadRecord(record, 0, ftpFile == null ? 0 : ftpFile.getSize(), getFileSize()));
}
if (ftpFile != null) {
//远程文件已完成
if (ftpFile.getSize() == getFileSize()) {
record.threadRecords.get(0).isComplete = true;
ALog.d(TAG, "FTP服务器上已存在该文件【" + ftpFile.getName() + "】");
} else if (ftpFile.getSize() == 0) {
getWrapper().setNewTask(true);
ALog.d(TAG, "FTP服务器上已存在该文件【" + ftpFile.getName() + "】,但文件长度为0,重新上传该文件");
} else {
ALog.w(TAG, "FTP服务器已存在未完成的文件【"
+ ftpFile.getName()
+ ",size: "
+ ftpFile.getSize()
+ "】"
+ "尝试从位置:"
+ (ftpFile.getSize() - 1)
+ "开始上传");
getWrapper().setNewTask(false);
// 修改记录
ThreadRecord threadRecord = record.threadRecords.get(0);
//修改本地保存的停止地址为服务器上对应文件的大小
threadRecord.startLocation = ftpFile.getSize() - 1;
}
} else {
ALog.d(TAG, "FTP服务器上不存在该文件");
getWrapper().setNewTask(true);
ThreadRecord tr = record.threadRecords.get(0);
tr.startLocation = 0;
tr.endLocation = getFileSize();
tr.isComplete = false;
}
}
@Override
public ThreadRecord createThreadRecord(TaskRecord record, int threadId, long startL, long endL) {
ThreadRecord tr;
tr = new ThreadRecord();
tr.taskKey = record.filePath;
tr.threadId = threadId;
tr.startLocation = startL;
tr.isComplete = false;
tr.threadType = getWrapper().getEntity().getTaskType();
tr.endLocation = getFileSize();
tr.blockLen = RecordUtil.getBlockLen(getFileSize(), threadId, record.threadNum);
return tr;
}
@Override public TaskRecord createTaskRecord(int threadNum) {
TaskRecord record = new TaskRecord();
record.fileName = getEntity().getFileName();
record.filePath = getEntity().getFilePath();
record.threadRecords = new ArrayList<>();
record.threadNum = threadNum;
record.isBlock = false;
record.taskType = getWrapper().getEntity().getTaskType();
record.isGroupRecord = getEntity().isGroupChild();
return record;
}
@Override public int initTaskThreadNum() {
return 1;
}
}

@ -36,7 +36,7 @@ import java.io.File;
*/ */
public class NormalLoader extends AbsNormalLoader { public class NormalLoader extends AbsNormalLoader {
private int startThreadNum; //启动的线程数 private int startThreadNum; //启动的线程数
private boolean isComplete = false; protected boolean isComplete = false;
private Looper looper; private Looper looper;
public NormalLoader(AbsTaskWrapper wrapper, IEventListener listener) { public NormalLoader(AbsTaskWrapper wrapper, IEventListener listener) {
@ -82,6 +82,10 @@ public class NormalLoader extends AbsNormalLoader {
// } // }
//} //}
protected Looper getLooper() {
return looper;
}
/** /**
* 启动单线程任务 * 启动单线程任务
*/ */
@ -103,6 +107,7 @@ public class NormalLoader extends AbsNormalLoader {
if (file.getParentFile() != null && !file.getParentFile().exists()) { if (file.getParentFile() != null && !file.getParentFile().exists()) {
FileUtil.createDir(file.getPath()); FileUtil.createDir(file.getPath());
} }
// 处理记录、初始化状态管理器
mRecord = mRecordHandler.getRecord(getFileSize()); mRecord = mRecordHandler.getRecord(getFileSize());
mStateManager.setLooper(mRecord, looper); mStateManager.setLooper(mRecord, looper);
getTaskList().addAll(mTTBuilder.buildThreadTask(mRecord, getTaskList().addAll(mTTBuilder.buildThreadTask(mRecord,
@ -116,6 +121,7 @@ public class NormalLoader extends AbsNormalLoader {
getListener().onStart(mStateManager.getCurrentProgress()); getListener().onStart(mStateManager.getCurrentProgress());
} }
// 启动线程任务
for (IThreadTask threadTask : getTaskList()) { for (IThreadTask threadTask : getTaskList()) {
ThreadTaskManager.getInstance().startThread(mTaskWrapper.getKey(), threadTask); ThreadTaskManager.getInstance().startThread(mTaskWrapper.getKey(), threadTask);
} }

Loading…
Cancel
Save