This commit is contained in:
laoyuyu
2019-11-19 20:03:25 +08:00
parent 846e6c0720
commit 1b54bfc4ac
15 changed files with 202 additions and 119 deletions

View File

@@ -1,58 +0,0 @@
/*
* 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.sftp;
import com.arialyy.aria.core.inf.OnFileInfoCallback;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSchException;
/**
* 获取sftp文件信息
*
* @author lyy
*/
public class AbsSFtpFileInfoThread<WP extends AbsTaskWrapper> implements Runnable {
private WP mWrapper;
private SFtpUtil mUtil;
private OnFileInfoCallback mCallback;
public AbsSFtpFileInfoThread(SFtpUtil util, WP wrapper, OnFileInfoCallback callback) {
mWrapper = wrapper;
mUtil = util;
mCallback = callback;
}
@Override public void run() {
startFlow();
}
private void startFlow() {
}
private ChannelSftp createChannel() {
ChannelSftp sftp = null;
try {
sftp = (ChannelSftp) mUtil.getSession().openChannel(SFtpUtil.CMD_TYPE_SFTP);
sftp.connect();
} catch (JSchException e) {
e.printStackTrace();
}
return sftp;
}
}

View File

@@ -0,0 +1,42 @@
/*
* 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.sftp;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import java.util.Vector;
/**
* sftp文件信息适配器
*/
public abstract class BaseInfoThreadAdapter<WRAPPER extends AbsTaskWrapper> {
private WRAPPER mWrapper;
public BaseInfoThreadAdapter(WRAPPER taskWrapper) {
mWrapper = taskWrapper;
}
public WRAPPER getWrapper() {
return mWrapper;
}
/**
* 处理文件
*
* @return true 处理文件成功false 处理文件失败,结束任务
*/
protected abstract boolean handlerFile(Vector vector);
}

View File

@@ -16,19 +16,25 @@
package com.arialyy.aria.sftp;
import com.arialyy.aria.core.AriaConfig;
import com.arialyy.aria.core.common.AbsEntity;
import com.arialyy.aria.core.FtpUrlEntity;
import com.arialyy.aria.core.common.AbsNormalEntity;
import com.arialyy.aria.core.inf.OnFileInfoCallback;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.ftp.FtpTaskOption;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;
import java.util.Vector;
/**
*
* https://cloud.tencent.com/developer/article/1354612
*
* @author lyy
*/
public class AbsSFtpInfoThread<ENTITY extends AbsEntity, TASK_WRAPPER extends AbsTaskWrapper<ENTITY>>
public class SFtpInfoThread<ENTITY extends AbsNormalEntity, TASK_WRAPPER extends AbsTaskWrapper<ENTITY>>
implements Runnable {
private final String TAG = CommonUtil.getClassName(getClass());
@@ -40,8 +46,12 @@ public class AbsSFtpInfoThread<ENTITY extends AbsEntity, TASK_WRAPPER extends Ab
protected long mSize = 0;
protected String charSet = "UTF-8";
private boolean isUpload = false;
private SFtpUtil mSFtpUtil;
private BaseInfoThreadAdapter mAdapter;
public AbsSFtpInfoThread(TASK_WRAPPER taskWrapper, OnFileInfoCallback callback) {
public SFtpInfoThread(SFtpUtil ftpUtil, TASK_WRAPPER taskWrapper,
OnFileInfoCallback callback) {
mSFtpUtil = ftpUtil;
mTaskWrapper = taskWrapper;
mEntity = taskWrapper.getEntity();
mTaskOption = (FtpTaskOption) taskWrapper.getTaskOption();
@@ -52,7 +62,39 @@ public class AbsSFtpInfoThread<ENTITY extends AbsEntity, TASK_WRAPPER extends Ab
}
}
@Override public void run() {
public void setAdapter(BaseInfoThreadAdapter adapter) {
mAdapter = adapter;
}
@Override public void run() {
if (mAdapter == null) {
ALog.e(TAG, "adapter为空");
return;
}
try {
ChannelSftp channelSftp =
(ChannelSftp) mSFtpUtil.getSession().openChannel(SFtpUtil.CMD_TYPE_SFTP);
Vector files = channelSftp.ls(getUrlEntity().remotePath);
if (files.isEmpty()) {
ALog.e(TAG, String.format("路径【%s】没有文件", getUrlEntity().remotePath));
mCallback.onFail(mEntity, null, false);
return;
}
if (!mAdapter.handlerFile(files)) {
ALog.e(TAG, "文件处理失败");
mCallback.onFail(mEntity, null, false);
return;
}
} catch (JSchException ex) {
ex.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
}
private FtpUrlEntity getUrlEntity() {
return mTaskOption.getUrlEntity();
}
}

View File

@@ -0,0 +1,31 @@
/*
* 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.sftp.download;
import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.sftp.BaseInfoThreadAdapter;
import java.util.Vector;
final class DSFtpInfoThreadAdapter extends BaseInfoThreadAdapter<DTaskWrapper> {
DSFtpInfoThreadAdapter(DTaskWrapper taskWrapper) {
super(taskWrapper);
}
@Override protected boolean handlerFile(Vector vector) {
return false;
}
}

View File

@@ -26,7 +26,7 @@ import com.arialyy.aria.core.loader.NormalLoader;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.exception.BaseException;
import com.arialyy.aria.ftp.FtpTaskOption;
import com.arialyy.aria.sftp.AbsSFtpFileInfoThread;
import com.arialyy.aria.sftp.SFtpInfoThread;
import com.arialyy.aria.sftp.SFtpUtil;
/**
@@ -57,15 +57,19 @@ public class SFtpDLoaderUtil extends AbsNormalLoaderUtil {
}
@Override protected Runnable createInfoThread() {
return new AbsSFtpFileInfoThread<>(mSftpUtil, (DTaskWrapper) getTaskWrapper(), new OnFileInfoCallback() {
@Override public void onComplete(String key, CompleteInfo info) {
DSFtpInfoThreadAdapter adapter = new DSFtpInfoThreadAdapter((DTaskWrapper) getTaskWrapper());
SFtpInfoThread infoThread = new SFtpInfoThread<>(mSftpUtil, (DTaskWrapper) getTaskWrapper(),
new OnFileInfoCallback() {
@Override public void onComplete(String key, CompleteInfo info) {
}
}
@Override public void onFail(AbsEntity entity, BaseException e, boolean needRetry) {
@Override public void onFail(AbsEntity entity, BaseException e, boolean needRetry) {
mSftpUtil.logout();
}
});
}
});
infoThread.setAdapter(adapter);
return infoThread;
}
}