This commit is contained in:
laoyuyu
2019-11-02 10:58:49 +08:00
parent e8c5a9f08b
commit c1b1cc1390
29 changed files with 331 additions and 52 deletions

1
SFtpComponent/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/build

View File

@@ -0,0 +1,33 @@
apply plugin: 'com.android.library'
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode rootProject.ext.versionCode
versionName rootProject.ext.versionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "androidx.appcompat:appcompat:${rootProject.ext.XAppcompatVersion}"
implementation "com.jcraft:jsch:0.1.55"
implementation "com.jcraft:jzlib:1.1.3"
implementation project(path: ':FtpComponent')
compile project(path: ':PublicComponent')
}

View File

21
SFtpComponent/proguard-rules.pro vendored Normal file
View File

@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

View File

@@ -0,0 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.arialyy.aria.sftp" />

View File

@@ -0,0 +1,58 @@
/*
* 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.AriaConfig;
import com.arialyy.aria.core.common.AbsEntity;
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.CommonUtil;
/**
*
* https://cloud.tencent.com/developer/article/1354612
* @author lyy
*/
public class AbsSFtpInfoThread<ENTITY extends AbsEntity, TASK_WRAPPER extends AbsTaskWrapper<ENTITY>>
implements Runnable {
private final String TAG = CommonUtil.getClassName(getClass());
protected ENTITY mEntity;
protected TASK_WRAPPER mTaskWrapper;
protected FtpTaskOption mTaskOption;
private int mConnectTimeOut;
protected OnFileInfoCallback mCallback;
protected long mSize = 0;
protected String charSet = "UTF-8";
private boolean isUpload = false;
public AbsSFtpInfoThread(TASK_WRAPPER taskWrapper, OnFileInfoCallback callback) {
mTaskWrapper = taskWrapper;
mEntity = taskWrapper.getEntity();
mTaskOption = (FtpTaskOption) taskWrapper.getTaskOption();
mConnectTimeOut = AriaConfig.getInstance().getDConfig().getConnectTimeOut();
mCallback = callback;
if (mEntity instanceof UploadEntity) {
isUpload = true;
}
}
@Override public void run() {
}
}

View File

@@ -0,0 +1,125 @@
/*
* 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 android.text.TextUtils;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import java.util.Properties;
/**
* sftp登录
*
* @author lyy
*/
public class SFtpLogin {
private String ip, userName, password;
private int port;
private Session session;
private boolean isLogin = false;
private SFtpLogin() {
createClient();
}
/**
* 创建客户端
*/
private void createClient() {
JSch jSch = new JSch();
try {
if (TextUtils.isEmpty(userName)) {
session = jSch.getSession(userName, ip, port);
} else {
session = jSch.getSession(ip);
}
if (!TextUtils.isEmpty(password)) {
session.setPassword(password);
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);// 为Session对象设置properties
session.setTimeout(3000);// 设置超时
isLogin = true;
} catch (JSchException e) {
e.printStackTrace();
}
}
/**
* 执行登录
*/
public Session login() {
try {
session.connect(); // 通过Session建立连接
} catch (JSchException e) {
e.printStackTrace();
}
return session;
}
/**
* 登出
*/
public void logout() {
if (session != null) {
session.disconnect();
}
isLogin = false;
}
public static class Builder {
private String ip, userName, password;
private int port = 22;
public Builder setIp(String ip) {
this.ip = ip;
return this;
}
public Builder setUserName(String userName) {
this.userName = userName;
return this;
}
public Builder setPassword(String password) {
this.password = password;
return this;
}
public Builder setPort(int port) {
this.port = port;
return this;
}
public SFtpLogin build() {
SFtpLogin login = new SFtpLogin();
login.ip = ip;
login.userName = userName;
login.password = password;
login.port = port;
if (TextUtils.isEmpty(ip)) {
throw new IllegalArgumentException("ip不能为空");
}
if (port < 0 || port > 65534) {
throw new IllegalArgumentException("端口错误");
}
return login;
}
}
}