兼容某些ftp服务器,自动修复url中的//
This commit is contained in:
@@ -22,8 +22,8 @@ dependencies {
|
||||
compile fileTree(include: ['*.jar'], dir: 'libs')
|
||||
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 project(':AriaAnnotations')
|
||||
compile 'com.arialyy.aria:aria-ftp-plug:1.0.3'
|
||||
|
||||
compile project(':AriaFtpPlug')
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ public abstract class AbsFtpInfoThread<ENTITY extends AbsEntity, TASK_ENTITY ext
|
||||
new String(setRemotePath().getBytes(charSet), AbsFtpThreadTask.SERVER_CHARSET);
|
||||
FTPFile[] files = client.listFiles(remotePath);
|
||||
String s = client.getReplyString();
|
||||
ALog.w(TAG, s);
|
||||
ALog.i(TAG, s);
|
||||
boolean isExist = files.length != 0;
|
||||
if (!isExist && !isUpload) {
|
||||
failDownload("文件不存在,任务链接【" + mTaskEntity.urlEntity.url + "】", false);
|
||||
|
||||
@@ -108,7 +108,7 @@ public class DownloadReceiver extends AbsReceiver {
|
||||
* </pre>
|
||||
*/
|
||||
@Deprecated public DownloadTarget load(@NonNull String url, boolean refreshInfo) {
|
||||
CheckUtil.checkUrl(url);
|
||||
url = CheckUtil.checkUrl(url);
|
||||
return new DownloadTarget(url, targetName, refreshInfo);
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@ public class DownloadReceiver extends AbsReceiver {
|
||||
* @param refreshInfo 是否刷新下载信息
|
||||
*/
|
||||
public FtpDownloadTarget loadFtp(@NonNull String url, boolean refreshInfo) {
|
||||
CheckUtil.checkUrl(url);
|
||||
url = CheckUtil.checkUrl(url);
|
||||
return new FtpDownloadTarget(url, targetName, refreshInfo);
|
||||
}
|
||||
|
||||
@@ -183,7 +183,7 @@ public class DownloadReceiver extends AbsReceiver {
|
||||
* 加载ftp文件夹下载地址
|
||||
*/
|
||||
public FtpDirDownloadTarget loadFtpDir(@NonNull String dirUrl) {
|
||||
CheckUtil.checkUrl(dirUrl);
|
||||
dirUrl = CheckUtil.checkUrl(dirUrl);
|
||||
return new FtpDirDownloadTarget(dirUrl, targetName);
|
||||
}
|
||||
|
||||
@@ -239,7 +239,7 @@ public class DownloadReceiver extends AbsReceiver {
|
||||
* 通过下载链接获取下载实体
|
||||
*/
|
||||
public DownloadEntity getDownloadEntity(String downloadUrl) {
|
||||
CheckUtil.checkUrl(downloadUrl);
|
||||
downloadUrl = CheckUtil.checkUrl(downloadUrl);
|
||||
return DbEntity.findFirst(DownloadEntity.class, "url=? and isGroupChild='false'", downloadUrl);
|
||||
}
|
||||
|
||||
@@ -247,7 +247,7 @@ public class DownloadReceiver extends AbsReceiver {
|
||||
* 通过下载链接获取保存在数据库的下载任务实体
|
||||
*/
|
||||
public DownloadTaskEntity getDownloadTask(String downloadUrl) {
|
||||
CheckUtil.checkUrl(downloadUrl);
|
||||
downloadUrl = CheckUtil.checkUrl(downloadUrl);
|
||||
DownloadEntity entity = getDownloadEntity(downloadUrl);
|
||||
if (entity == null || TextUtils.isEmpty(entity.getDownloadPath())) return null;
|
||||
return DbEntity.findFirst(DownloadTaskEntity.class, "key=? and isGroupTask='false'",
|
||||
|
||||
@@ -34,7 +34,7 @@ import org.apache.commons.net.ftp.FTPReply;
|
||||
* Ftp下载任务
|
||||
*/
|
||||
class FtpThreadTask extends AbsFtpThreadTask<DownloadEntity, DownloadTaskEntity> {
|
||||
private final String TAG = "FtpThreadTask";
|
||||
private final String TAG = "FtpDownloadThreadTask";
|
||||
|
||||
FtpThreadTask(StateConstance constance, IDownloadListener listener,
|
||||
SubThreadConfig<DownloadTaskEntity> downloadInfo) {
|
||||
@@ -64,22 +64,21 @@ class FtpThreadTask extends AbsFtpThreadTask<DownloadEntity, DownloadTaskEntity>
|
||||
}
|
||||
//发送第二次指令时,还需要再做一次判断
|
||||
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();
|
||||
if (!FTPReply.isPositivePreliminary(reply) && reply != FTPReply.COMMAND_OK) {
|
||||
fail(mChildCurrentLocation, "获取文件信息错误,错误码为:" + reply + ",msg:" + client.getReplyString(),
|
||||
null);
|
||||
client.disconnect();
|
||||
return;
|
||||
}
|
||||
String remotePath =
|
||||
new String(mTaskEntity.urlEntity.remotePath.getBytes(charSet), SERVER_CHARSET);
|
||||
ALog.i(TAG, "remotePath【" + remotePath + "】");
|
||||
is = client.retrieveFileStream(remotePath);
|
||||
reply = client.getReplyCode();
|
||||
if (!FTPReply.isPositivePreliminary(reply)) {
|
||||
client.disconnect();
|
||||
fail(mChildCurrentLocation, "获取流失败,错误码为:" + reply + ",msg:" + client.getReplyString(),
|
||||
null);
|
||||
client.disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,12 +16,12 @@
|
||||
package com.arialyy.aria.core.download.downloader;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.core.AriaManager;
|
||||
import com.arialyy.aria.core.common.OnFileInfoCallback;
|
||||
import com.arialyy.aria.core.download.DownloadEntity;
|
||||
import com.arialyy.aria.core.download.DownloadTaskEntity;
|
||||
import com.arialyy.aria.util.ALog;
|
||||
import com.arialyy.aria.util.CheckUtil;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
@@ -130,12 +130,14 @@ class HttpFileInfoThread implements Runnable {
|
||||
private void handle302Turn(HttpURLConnection conn) throws IOException {
|
||||
String newUrl = conn.getHeaderField(mTaskEntity.redirectUrlKey);
|
||||
ALog.d(TAG, "30x跳转,location【 " + mTaskEntity.redirectUrlKey + "】" + "新url为【" + newUrl + "】");
|
||||
if (TextUtils.isEmpty(newUrl) || newUrl.equalsIgnoreCase("null")) {
|
||||
if (TextUtils.isEmpty(newUrl) || newUrl.equalsIgnoreCase("null") || !newUrl.startsWith(
|
||||
"http")) {
|
||||
if (onFileInfoListener != null) {
|
||||
onFileInfoListener.onFail(mEntity.getUrl(), "获取重定向链接失败", false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
newUrl = CheckUtil.checkUrl(newUrl);
|
||||
String cookies = conn.getHeaderField("Set-Cookie");
|
||||
conn = (HttpURLConnection) new URL(newUrl).openConnection();
|
||||
conn = ConnectionHelp.setConnectParam(mTaskEntity, conn);
|
||||
|
||||
@@ -35,7 +35,7 @@ public abstract class AbsUploadTarget<TARGET extends AbsUploadTarget, ENTITY ext
|
||||
* @param uploadUrl 上传路径
|
||||
*/
|
||||
public TARGET setUploadUrl(@NonNull String uploadUrl) {
|
||||
CheckUtil.checkUrl(uploadUrl);
|
||||
uploadUrl = CheckUtil.checkUrl(uploadUrl);
|
||||
if (mEntity.getUrl().equals(uploadUrl)) return (TARGET) this;
|
||||
mEntity.setUrl(uploadUrl);
|
||||
mEntity.update();
|
||||
|
||||
@@ -53,12 +53,15 @@ public class FtpUploadTarget
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置上传路径
|
||||
* 设置上传路径,FTP上传路径必须是从"/"开始的完整路径
|
||||
*
|
||||
* @param uploadUrl 上传路径
|
||||
*/
|
||||
public FtpUploadTarget setUploadUrl(@NonNull String uploadUrl) {
|
||||
CheckUtil.checkUrl(uploadUrl);
|
||||
uploadUrl = CheckUtil.checkUrl(uploadUrl);
|
||||
if (!uploadUrl.endsWith("/")) {
|
||||
uploadUrl += "/";
|
||||
}
|
||||
mTaskEntity.urlEntity = CommonUtil.getFtpUrlInfo(uploadUrl);
|
||||
if (mEntity.getUrl().equals(uploadUrl)) return this;
|
||||
mEntity.setUrl(uploadUrl);
|
||||
@@ -66,6 +69,15 @@ public class FtpUploadTarget
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置字符编码
|
||||
*/
|
||||
public FtpUploadTarget charSet(String charSet) {
|
||||
if (TextUtils.isEmpty(charSet)) return this;
|
||||
mTaskEntity.charSet = charSet;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* ftp 用户登录信。
|
||||
* 设置登录信息需要在设置上传链接之后{@link #setUploadUrl(String)}
|
||||
|
||||
@@ -35,7 +35,7 @@ import org.apache.commons.net.ftp.OnFtpInputStreamListener;
|
||||
* FTP 单线程上传任务,需要FTP 服务器给用户打开删除和读入IO的权限
|
||||
*/
|
||||
class FtpThreadTask extends AbsFtpThreadTask<UploadEntity, UploadTaskEntity> {
|
||||
private final String TAG = "FtpThreadTask";
|
||||
private final String TAG = "FtpUploadThreadTask";
|
||||
private String dir, remotePath;
|
||||
|
||||
FtpThreadTask(StateConstance constance, IEventListener listener,
|
||||
@@ -61,25 +61,13 @@ class FtpThreadTask extends AbsFtpThreadTask<UploadEntity, UploadTaskEntity> {
|
||||
client = createClient();
|
||||
if (client == null) return;
|
||||
initPath();
|
||||
if (client.makeDirectory(dir)) {
|
||||
fail(mChildCurrentLocation, "创建文件夹失败", null);
|
||||
return;
|
||||
}
|
||||
if (client.changeWorkingDirectory(dir)) {
|
||||
fail(mChildCurrentLocation, "选择工作文件夹失败", null);
|
||||
return;
|
||||
}
|
||||
client.makeDirectory(dir);
|
||||
client.changeWorkingDirectory(dir);
|
||||
client.setRestartOffset(mConfig.START_LOCATION);
|
||||
int reply = client.getReplyCode();
|
||||
if (reply == FTPReply.SYNTAX_ERROR_IN_ARGUMENTS) {
|
||||
if (!FTPReply.isPositivePreliminary(reply) && reply != FTPReply.FILE_ACTION_OK) {
|
||||
fail(mChildCurrentLocation, "上传失败,错误码为:" + reply + ",msg:" + client.getReplyString(), null);
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -126,7 +114,7 @@ class FtpThreadTask extends AbsFtpThreadTask<UploadEntity, UploadTaskEntity> {
|
||||
private void initPath() throws UnsupportedEncodingException {
|
||||
dir = new String(mTaskEntity.urlEntity.remotePath.getBytes(charSet), SERVER_CHARSET);
|
||||
remotePath = new String(
|
||||
(mTaskEntity.urlEntity.remotePath + "/" + mEntity.getFileName()).getBytes(charSet),
|
||||
("/" + mTaskEntity.urlEntity.remotePath + mEntity.getFileName()).getBytes(charSet),
|
||||
SERVER_CHARSET);
|
||||
}
|
||||
|
||||
@@ -134,6 +122,7 @@ class FtpThreadTask extends AbsFtpThreadTask<UploadEntity, UploadTaskEntity> {
|
||||
throws IOException {
|
||||
|
||||
try {
|
||||
ALog.d(TAG, "remotePath【" + remotePath + "】");
|
||||
client.storeFile(remotePath, new FtpFISAdapter(bis), new OnFtpInputStreamListener() {
|
||||
boolean isStoped = false;
|
||||
|
||||
@@ -160,11 +149,13 @@ class FtpThreadTask extends AbsFtpThreadTask<UploadEntity, UploadTaskEntity> {
|
||||
|
||||
int reply = client.getReplyCode();
|
||||
if (!FTPReply.isPositiveCompletion(reply)) {
|
||||
if (reply != FTPReply.TRANSFER_ABORTED) {
|
||||
fail(mChildCurrentLocation, "上传文件错误,错误码为:" + reply + ",msg:" + client.getReplyString(),
|
||||
null);
|
||||
}
|
||||
if (client.isConnected()) {
|
||||
client.disconnect();
|
||||
}
|
||||
if (reply == FTPReply.TRANSFER_ABORTED) return;
|
||||
fail(mChildCurrentLocation, "上传文件错误,错误码为:" + reply, null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ public class CheckUtil {
|
||||
* 检查下载实体
|
||||
*/
|
||||
public static void checkDownloadEntity(DownloadEntity entity) {
|
||||
checkUrl(entity.getUrl());
|
||||
entity.setUrl(checkUrl(entity.getUrl()));
|
||||
checkPath(entity.getDownloadPath());
|
||||
}
|
||||
|
||||
@@ -88,14 +88,25 @@ public class CheckUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测下载链接是否为null
|
||||
* 检测下载链接是否合法,如果地址中path是"//"而不是"/"将会改为"/";
|
||||
*/
|
||||
public static void checkUrl(String url) {
|
||||
public static String checkUrl(String url) {
|
||||
if (TextUtils.isEmpty(url)) {
|
||||
throw new IllegalArgumentException("下载链接不能为null");
|
||||
throw new IllegalArgumentException("url不能为null");
|
||||
} else if (!url.startsWith("http") && !url.startsWith("ftp")) {
|
||||
throw new IllegalArgumentException("url错误");
|
||||
}
|
||||
int index = url.indexOf("://");
|
||||
if (index == -1) {
|
||||
throw new IllegalArgumentException("url不合法");
|
||||
}
|
||||
String temp = url.substring(index + 3, url.length());
|
||||
if (temp.contains("//")) {
|
||||
temp = url.substring(0, index + 3) + temp.replaceAll("//", "/");
|
||||
ALog.w(TAG, "url中含有//,//将转换为/,转换后的url为:" + temp);
|
||||
return temp;
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user