修复ftp上传完成后,删除服务器端的文件,无法重新下载的问题

This commit is contained in:
laoyuyu
2020-01-06 19:59:17 +08:00
parent e14928e260
commit 8158c58d6e
29 changed files with 290 additions and 74 deletions

View File

@@ -91,7 +91,7 @@ public abstract class AbsFtpInfoTask<ENTITY extends AbsEntity, TASK_WRAPPER exte
}
String remotePath = CommonUtil.convertFtpChar(charSet, getRemotePath());
FTPFile[] files = client.listFiles(getRemotePath());
FTPFile[] files = client.listFiles(remotePath);
boolean isExist = files.length != 0;
if (!isExist && !isUpload) {
int i = remotePath.lastIndexOf(File.separator);
@@ -128,7 +128,11 @@ public abstract class AbsFtpInfoTask<ENTITY extends AbsEntity, TASK_WRAPPER exte
}
//为了防止编码错乱,需要使用原始字符串
mSize = getFileSize(files, client, getRemotePath());
if (isUpload && files.length == 0){
handleFile(getRemotePath(), null);
}else {
mSize = getFileSize(files, client, getRemotePath());
}
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
if (isUpload) {

View File

@@ -38,15 +38,21 @@ public final class FtpRecordHandler extends RecordHandler {
}
@Override public void handlerTaskRecord(TaskRecord record) {
if (getWrapper().getRequestType() == ITaskWrapper.U_FTP) {
// 上传任务记录的处理交由FtpUFileInfoTask 完成
return;
}
RecordHelper helper = new RecordHelper(getWrapper(), record);
if (getWrapper().isSupportBP()) {
if (record.isBlock) {
helper.handleBlockRecord();
} else {
helper.handleMultiRecord();
}
} else if (record.threadNum == 1) {
if (record.threadNum == 1) {
helper.handleSingleThreadRecord();
} else {
if (getWrapper().isSupportBP()) {
if (record.isBlock) {
helper.handleBlockRecord();
} else {
helper.handleMultiRecord();
}
}
}
}

View File

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

View File

@@ -28,6 +28,8 @@ import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.ftp.AbsFtpInfoTask;
import com.arialyy.aria.ftp.FtpTaskOption;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.util.RecordUtil;
import java.nio.charset.Charset;
@@ -43,13 +45,27 @@ final class FtpDGInfoTask extends AbsFtpInfoTask<DownloadGroupEntity, DGTaskWrap
}
@Override public void run() {
if (mTaskWrapper.getEntity().getFileSize() > 1) {
if (mTaskWrapper.getEntity().getFileSize() > 1 && checkSubOption()) {
callback.onSucceed(mEntity.getKey(), new CompleteInfo(200, mTaskWrapper));
} else {
super.run();
}
}
/**
* 检查子任务的任务设置
*
* @return true 子任务任务设置成功false 子任务任务设置失败
*/
private boolean checkSubOption() {
for (DTaskWrapper wrapper : mTaskWrapper.getSubTaskWrapper()) {
if (wrapper.getTaskOption() == null) {
return false;
}
}
return true;
}
@Override protected String getRemotePath() {
return mTaskOption.getUrlEntity().remotePath;
}
@@ -92,9 +108,14 @@ final class FtpDGInfoTask extends AbsFtpInfoTask<DownloadGroupEntity, DGTaskWrap
*/
private void addEntity(String remotePath, FTPFile ftpFile) {
final FtpUrlEntity urlEntity = mTaskOption.getUrlEntity().clone();
String url =
urlEntity.scheme + "://" + urlEntity.hostName + ":" + urlEntity.port + "/" + remotePath;
if (checkEntityExist(url)){
ALog.w(TAG, "子任务已存在取消子任务的添加url = " + url);
return;
}
DownloadEntity entity = new DownloadEntity();
entity.setUrl(
urlEntity.scheme + "://" + urlEntity.hostName + ":" + urlEntity.port + "/" + remotePath);
entity.setUrl(url);
entity.setFilePath(mEntity.getDirPath() + "/" + remotePath);
int lastIndex = remotePath.lastIndexOf("/");
String fileName = lastIndex < 0 ? CommonUtil.keyToHashKey(remotePath)
@@ -106,8 +127,9 @@ final class FtpDGInfoTask extends AbsFtpInfoTask<DownloadGroupEntity, DGTaskWrap
entity.setConvertFileSize(CommonUtil.formatFileSize(ftpFile.getSize()));
entity.setFileSize(ftpFile.getSize());
//if(CheckUtil.checkDPathConflicts(mTaskWrapper.is))
if (DbEntity.checkDataExist(DownloadEntity.class, "downloadPath=?", entity.getFilePath())) {
DbEntity.deleteData(DownloadEntity.class, "downloadPath=?", entity.getFilePath());
}
entity.insert();
DTaskWrapper subWrapper = new DTaskWrapper(entity);
@@ -139,6 +161,21 @@ final class FtpDGInfoTask extends AbsFtpInfoTask<DownloadGroupEntity, DGTaskWrap
subWrapper.setTaskOption(subOption);
}
/**
* 检查子任务是否已经存在,如果子任务存在,取消添加操作
*
* @param key url
* @return true 子任务已存在false 子任务不存在
*/
private boolean checkEntityExist(String key) {
for (DTaskWrapper wrapper : mTaskWrapper.getSubTaskWrapper()) {
if (wrapper.getKey().equals(key)) {
return true;
}
}
return false;
}
@Override
protected void failDownload(FTPClient client, String msg, Exception e, boolean needRetry) {
super.failDownload(client, msg, e, needRetry);

View File

@@ -26,9 +26,11 @@ import com.arialyy.aria.core.processor.IFtpUploadInterceptor;
import com.arialyy.aria.core.upload.UTaskWrapper;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.ftp.AbsFtpInfoTask;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.util.DbDataHelper;
import com.arialyy.aria.util.RecordUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@@ -61,7 +63,7 @@ final class FtpUFileInfoTask extends AbsFtpInfoTask<UploadEntity, UTaskWrapper>
try {
IFtpUploadInterceptor interceptor = mTaskOption.getUploadInterceptor();
if (interceptor != null) {
/**
/*
* true 使用拦截器false 不使用拦截器
*/
List<String> files = new ArrayList<>();
@@ -84,6 +86,7 @@ final class FtpUFileInfoTask extends AbsFtpInfoTask<UploadEntity, UTaskWrapper>
ALog.d(TAG,
String.format("删除文件%scode: %s msg: %s", b ? "成功" : "失败", client.getReplyCode(),
client.getReplyString()));
mTaskOption.setServeFileIsExist(false);
} else if (!TextUtils.isEmpty(interceptHandler.getNewFileName())) {
ALog.i(TAG, String.format("远端已拥有同名文件将修改remotePath原文件名%s新文件名%s",
mEntity.getFileName(), interceptHandler.getNewFileName()));
@@ -92,6 +95,7 @@ final class FtpUFileInfoTask extends AbsFtpInfoTask<UploadEntity, UTaskWrapper>
+ interceptHandler.getNewFileName();
mTaskOption.setNewFileName(interceptHandler.getNewFileName());
mTaskOption.setServeFileIsExist(false);
closeClient(client);
run();
return false;
@@ -156,9 +160,41 @@ final class FtpUFileInfoTask extends AbsFtpInfoTask<UploadEntity, UTaskWrapper>
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) {
super.onPreComplete(code);
callback.onSucceed(mEntity.getKey(),

View File

@@ -130,9 +130,10 @@ final class FtpUThreadTaskAdapter extends BaseFtpThreadTaskAdapter {
if (isTimeOut) {
fail(new AriaIOException(TAG, "socket连接失败该问题一般出现于网络断开客户端重新连接"
+ "但是服务器端无法创建socket缺没有返回错误码的情况。"), false);
}
if (fa != null) {
fa.close();
if (fa != null) {
fa.close();
}
closeTimer();
}
isTimeOut = true;
} catch (IOException e) {
@@ -143,6 +144,7 @@ final class FtpUThreadTaskAdapter extends BaseFtpThreadTaskAdapter {
}
private void closeTimer() {
ALog.d(TAG, "closeTimer");
if (timer != null && !timer.isShutdown()) {
timer.shutdown();
}
@@ -158,6 +160,7 @@ final class FtpUThreadTaskAdapter extends BaseFtpThreadTaskAdapter {
fa = new FtpFISAdapter(bis);
storeFail = false;
startTimer();
final long fileSize = getThreadConfig().tempFile.length();
try {
ALog.d(TAG, String.format("remotePath: %s", remotePath));
client.storeFile(remotePath, fa, new OnFtpInputStreamListener() {
@@ -170,6 +173,7 @@ final class FtpUThreadTaskAdapter extends BaseFtpThreadTaskAdapter {
if (getThreadTask().isBreak() && !isStoped) {
isStoped = true;
client.abor();
return;
}
if (mSpeedBandUtil != null) {
mSpeedBandUtil.limitNextBytes(bytesTransferred);
@@ -178,19 +182,12 @@ final class FtpUThreadTaskAdapter extends BaseFtpThreadTaskAdapter {
} catch (IOException e) {
e.printStackTrace();
storeFail = true;
try {
fa.close();
} catch (IOException e1) {
e1.printStackTrace();
}
closeClient(client);
}
}
});
} catch (IOException e) {
String msg = String.format("文件上传错误,错误码为:%s, msg%s, filePath: %s", client.getReply(),
client.getReplyString(), getEntity().getFilePath());
closeClient(client);
e.printStackTrace();
if (e.getMessage().contains("AriaIOException caught while copying")) {
e.printStackTrace();
@@ -198,11 +195,14 @@ final class FtpUThreadTaskAdapter extends BaseFtpThreadTaskAdapter {
fail(new AriaIOException(TAG, msg, e), !storeFail);
}
return false;
} finally {
fa.close();
closeClient(client);
}
if (storeFail) {
return false;
}
int reply = client.getReply();
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
if (reply != FTPReply.TRANSFER_ABORTED) {
fail(new AriaIOException(TAG,