修复部分ftp服务器下载文件失败的问题

This commit is contained in:
e
2018-02-01 20:06:35 +08:00
parent f3cb7b601a
commit 88dfa7eedc
11 changed files with 99 additions and 55 deletions

View File

@@ -23,8 +23,8 @@ dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile project(':AriaAnnotations')
compile 'com.arialyy.aria:aria-ftp-plug:1.0.3'
// compile 'com.arialyy.aria:aria-ftp-plug:1.0.3'
// compile project(':AriaFtpPlug')
compile project(':AriaFtpPlug')
}
apply from: 'bintray-release.gradle'

View File

@@ -69,8 +69,6 @@ import org.xml.sax.SAXException;
public static final String DOWNLOAD_TEMP_DIR = "/Aria/temp/download/";
public static final String UPLOAD_TEMP_DIR = "/Aria/temp/upload/";
@SuppressLint("StaticFieldLeak") private static volatile AriaManager INSTANCE = null;
private Map<String, AbsReceiver> mReceivers = new ConcurrentHashMap<>();
public static Context APP;

View File

@@ -23,6 +23,7 @@ import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.Regular;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.regex.Matcher;
@@ -77,11 +78,18 @@ public abstract class AbsFtpInfoThread<ENTITY extends AbsEntity, TASK_ENTITY ext
String remotePath =
new String(setRemotePath().getBytes(charSet), AbsFtpThreadTask.SERVER_CHARSET);
FTPFile[] files = client.listFiles(remotePath);
String s = client.getReplyString();
ALog.w(TAG, s);
boolean isExist = files.length != 0;
if (!isExist && !isUpload) {
failDownload("文件不存在,任务链接【" + mTaskEntity.urlEntity.url + "", false);
FTPFile[] files1 = client.listFiles();
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, "路径【" + setRemotePath() + "】下的文件列表 ===================================");
for (FTPFile file : files1) {
@@ -89,7 +97,7 @@ public abstract class AbsFtpInfoThread<ENTITY extends AbsEntity, TASK_ENTITY ext
}
ALog.i(TAG,
"================================= --end-- ===================================");
}else {
} else {
String msg = client.getReplyString();
ALog.w(TAG, msg);
}
@@ -105,7 +113,7 @@ public abstract class AbsFtpInfoThread<ENTITY extends AbsEntity, TASK_ENTITY ext
mTaskEntity.isNewTask = true;
} else {
client.disconnect();
failDownload("获取文件信息错误,错误码为:" + reply, true);
failDownload("获取文件信息错误,错误码为:" + reply + "msg:" + client.getReplyString(), true);
return;
}
}
@@ -196,13 +204,14 @@ public abstract class AbsFtpInfoThread<ENTITY extends AbsEntity, TASK_ENTITY ext
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
failDownload("无法连接到ftp服务器错误码为" + reply, true);
failDownload("无法连接到ftp服务器错误码为" + reply + "msg:" + client.getReplyString(), true);
return null;
}
// 开启服务器对UTF-8的支持如果服务器支持就用UTF-8编码
charSet = "UTF-8";
if (!TextUtils.isEmpty(mTaskEntity.charSet) || !FTPReply.isPositiveCompletion(
client.sendCommand("OPTS UTF8", "ON"))) {
reply = client.sendCommand("OPTS UTF8", "ON");
if (!TextUtils.isEmpty(mTaskEntity.charSet) || (!FTPReply.isPositiveCompletion(reply)
&& reply != FTPReply.COMMAND_OK)) {
ALog.i(TAG, "FTP 服务器不支持开启UTF8编码尝试使用Aria手动设置的编码");
charSet = mTaskEntity.charSet;
}

View File

@@ -72,7 +72,7 @@ public abstract class AbsFtpThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTI
}
}
if (client == null){
if (client == null) {
return null;
}
@@ -87,7 +87,8 @@ public abstract class AbsFtpThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTI
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
fail(STATE.CURRENT_LOCATION, "无法连接到ftp服务器错误码为" + reply, null);
fail(STATE.CURRENT_LOCATION,
"无法连接到ftp服务器错误码为" + reply + "msg:" + client.getReplyString(), null);
return null;
}
// 开启服务器对UTF-8的支持如果服务器支持就用UTF-8编码

View File

@@ -59,17 +59,30 @@ class FtpThreadTask extends AbsFtpThreadTask<DownloadEntity, DownloadTaskEntity>
+ "");
client = createClient();
if (client == null) return;
client.setRestartOffset(mConfig.START_LOCATION);
if (mConfig.START_LOCATION > 0) {
client.setRestartOffset(mConfig.START_LOCATION);
}
//发送第二次指令时,还需要再做一次判断
int reply = client.getReplyCode();
if (reply == FTPReply.SYNTAX_ERROR_IN_ARGUMENTS) {
ALog.e(TAG, "FTP没有打开写权限断点续传失败将从0开始读文件");
} else if (!FTPReply.isPositivePreliminary(reply) && reply != FTPReply.COMMAND_OK) {
client.disconnect();
fail(mChildCurrentLocation, "获取文件信息错误,错误码为:" + reply + "msg" + client.getReplyString(),
null);
return;
}
String remotePath =
new String(mTaskEntity.urlEntity.remotePath.getBytes(charSet), SERVER_CHARSET);
is = client.retrieveFileStream(remotePath);
//发送第二次指令时,还需要再做一次判断
int reply = client.getReplyCode();
reply = client.getReplyCode();
if (!FTPReply.isPositivePreliminary(reply)) {
client.disconnect();
fail(mChildCurrentLocation, "获取文件信息错误,错误码为:" + reply, null);
fail(mChildCurrentLocation, "获取流失败,错误码为:" + reply + "msg" + client.getReplyString(),
null);
return;
}
file = new BufferedRandomAccessFile(mConfig.TEMP_FILE, "rwd", mBufSize);
file.seek(mConfig.START_LOCATION);
byte[] buffer = new byte[mBufSize];

View File

@@ -61,9 +61,28 @@ class FtpThreadTask extends AbsFtpThreadTask<UploadEntity, UploadTaskEntity> {
client = createClient();
if (client == null) return;
initPath();
client.makeDirectory(dir);
client.changeWorkingDirectory(dir);
if (client.makeDirectory(dir)) {
fail(mChildCurrentLocation, "创建文件夹失败", null);
return;
}
if (client.changeWorkingDirectory(dir)) {
fail(mChildCurrentLocation, "选择工作文件夹失败", null);
return;
}
client.setRestartOffset(mConfig.START_LOCATION);
int reply = client.getReplyCode();
if (reply == FTPReply.SYNTAX_ERROR_IN_ARGUMENTS) {
client.disconnect();
fail(mChildCurrentLocation,
"上传失败FTP没有打开写权限错误码为" + reply + "msg" + client.getReplyString(), null);
return;
} else if (!FTPReply.isPositivePreliminary(reply) && reply != FTPReply.COMMAND_OK) {
client.disconnect();
fail(mChildCurrentLocation,
"上传失败FTP没有打开写权限错误码为" + reply + "msg" + client.getReplyString(), null);
return;
}
file = new BufferedRandomAccessFile(mConfig.TEMP_FILE, "rwd", mBufSize);
if (mConfig.START_LOCATION != 0) {
//file.skipBytes((int) mConfig.START_LOCATION);
@@ -82,7 +101,7 @@ class FtpThreadTask extends AbsFtpThreadTask<UploadEntity, UploadTaskEntity> {
STATE.isRunning = false;
mListener.onComplete();
}
if (STATE.isFail()){
if (STATE.isFail()) {
STATE.isRunning = false;
mListener.onFail(false);
}

View File

@@ -103,7 +103,8 @@ public class CommonUtil {
entity.hostName = str;
}
entity.port = m.group(3);
entity.remotePath = TextUtils.isEmpty(m.group(4)) ? "/" : "/" + m.group(4);
//entity.remotePath = TextUtils.isEmpty(m.group(4)) ? "/" : "/" + m.group(4);
entity.remotePath = TextUtils.isEmpty(m.group(4)) ? "/" : m.group(4);
}
return entity;
}