author:wangyimiao

desc:新增FTP上传能力
master
yimiao 3 years ago
parent ec37cafe67
commit 319be21546

@ -11,6 +11,8 @@ import com.common.commonlib.image.loader.ImageLoader;
import com.common.commonlib.net.RequestCallBack;
import com.common.commonlib.net.interceptor.RequestHeadInterceptor;
import com.common.commonlib.net.interceptor.ResponseHeadInterceptor;
import com.common.commonlib.utils.BaseUtils;
import com.common.commonlib.utils.FTPUtils;
import com.common.commonlibtest.bean.ArticlesResponse;
import com.common.commonlibtest.bean.CollectionResponse;
import com.common.commonlibtest.bean.LoginResponse;
@ -118,5 +120,18 @@ public class MainActivity extends AppCompatActivity {
}
});
});
binding.uploadBtn.setOnClickListener(l -> {
String filePath = BaseUtils.INSTANCE.getExternalStorageDirectory(this);
String fileName = "/richang.md";
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
FTPUtils.RESULT result = FTPUtils.INSTANCE.ftpUpload("192.168.101.215", "2221", "admin", "123456", filePath, fileName);
Log.i("wangym", result.name());
}
});
thread.start();
});
}
}

@ -60,4 +60,15 @@
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.770" />
<Button
android:id="@+id/upload_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ftpUpload"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.770" />
</androidx.constraintlayout.widget.ConstraintLayout>

@ -19,6 +19,9 @@ allprojects {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
flatDir {
dirs 'libs'
}
}
}

@ -1,6 +1,7 @@
package com.common.commonlib.utils
import android.content.Context
import android.os.Environment
/**
* 基础工具类
@ -12,7 +13,14 @@ object BaseUtils {
return list == null || list.isEmpty()
}
/**
* 获取应用中文件存储
*/
fun getExternalStoragePath(context: Context): String? {
return context.getExternalFilesDir(null)?.path
}
fun getExternalStorageDirectory(context: Context): String? {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).path
}
}

@ -0,0 +1,133 @@
package com.common.commonlib.utils
import org.apache.commons.net.ftp.FTP
import org.apache.commons.net.ftp.FTPClient
import org.apache.commons.net.ftp.FTPReply
import java.io.BufferedOutputStream
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
/**
* FTP工具类
*/
object FTPUtils {
/**
* 默认缓冲池大小
*/
private const val DEFAULT_BUFFER_SIZE = 1024
/**
* 默认编码模式
*/
private const val DEFAULT_ENCODING = "UTF-8"
/**
* 结果
*/
enum class RESULT { SUCCESS, FAILED }
/**
* ftp上传
* @param url ftp地址
* @param port ftp连接端口号
* @param username 登录用户名
* @param password 登录密码
* @param fileNamePath 本地文件保存路径
* @param fileName 本地文件名
* @return
*/
fun ftpUpload(
url: String?,
port: String,
username: String?,
password: String?,
fileNamePath: String,
fileName: String
): RESULT {
val ftpClient = FTPClient()
val fis: FileInputStream?
var returnMessage = RESULT.FAILED
try {
ftpClient.connect(url, port.toInt())
val loginResult = ftpClient.login(username, password)
val returnCode = ftpClient.replyCode
if (loginResult && FTPReply.isPositiveCompletion(returnCode)) { // 如果登录成功
ftpClient.bufferSize = DEFAULT_BUFFER_SIZE
ftpClient.controlEncoding = DEFAULT_ENCODING
ftpClient.setFileType(FTP.BINARY_FILE_TYPE)
ftpClient.enterLocalPassiveMode()
fis = FileInputStream(fileNamePath + fileName)
ftpClient.storeFile(fileName, fis)
returnMessage = RESULT.SUCCESS //上传成功
} else { // 如果登录失败
returnMessage = RESULT.FAILED
}
} catch (e: IOException) {
e.printStackTrace()
returnMessage = RESULT.FAILED
} finally {
try {
ftpClient.disconnect()
} catch (e: IOException) {
e.printStackTrace()
throw RuntimeException("关闭FTP连接发生异常", e)
}
}
return returnMessage
}
/**
* ftp下载
* @param url
* @param port
* @param username
* @param password
* @param filePath 存放文件的路径
* @param FTP_file 要下载的文件名
* @param SD_file 本地文件名
*/
fun ftpDown(
url: String?,
port: Int,
username: String?,
password: String?,
filePath: String,
FTP_file: String?,
SD_file: String
): RESULT {
val buffOut: BufferedOutputStream?
val ftpClient = FTPClient()
var returnMessage = RESULT.FAILED
try {
ftpClient.connect(url, port)
val loginResult = ftpClient.login(username, password)
val returnCode = ftpClient.replyCode
if (loginResult && FTPReply.isPositiveCompletion(returnCode)) { // 如果登录成功
ftpClient.bufferSize = DEFAULT_BUFFER_SIZE
ftpClient.controlEncoding = DEFAULT_ENCODING
ftpClient.enterLocalPassiveMode()
buffOut = BufferedOutputStream(FileOutputStream(filePath + SD_file), 8 * 1024)
ftpClient.retrieveFile(FTP_file, buffOut)
buffOut.flush()
buffOut.close()
ftpClient.logout()
ftpClient.disconnect()
returnMessage = RESULT.SUCCESS //上传成功
} else { // 如果登录失败
returnMessage = RESULT.FAILED
}
} catch (e: IOException) {
e.printStackTrace()
throw java.lang.RuntimeException("FTP客户端出错", e)
} finally {
try {
ftpClient.disconnect()
} catch (e: IOException) {
e.printStackTrace()
throw java.lang.RuntimeException("关闭FTP连接发生异常", e)
}
}
return returnMessage
}
}
Loading…
Cancel
Save