Android 的 Ftp 断点上传 ---Aria 使用笔记
发布于: 1 小时前
1.Aria 库官方链接
// 修改build.gradle,添加如下依赖
implementation 'me.laoyuyu.aria:core:3.8.16'
annotationProcessor 'me.laoyuyu.aria:compiler:3.8.16'
implementation 'me.laoyuyu.aria:ftp:3.8.16'// # 如果需要使用ftp,请增加该组件
// 修改AndroidManifest.xml文件,添加权限声明
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
复制代码
2.代码
使用 kotlin 语言特性,封装一个单例的 FtpManager 类,用于处理上传任务
import android.util.Log
import com.arialyy.aria.core.Aria
import com.arialyy.aria.core.common.FtpOption
import com.arialyy.aria.core.inf.IEntity
import com.arialyy.aria.core.task.UploadTask
import com.arialyy.aria.core.upload.UploadEntity
import com.arialyy.aria.core.upload.UploadTaskListener
import com.dp.logcatapp.model.Config
/**
* 新增FtpManager类,用于上传Ftp文件
*/
class FtpManager private constructor() : UploadTaskListener {
companion object{
private const val PORT = 21
@Volatile
private var instance: FtpManager? = null
private val instanceLock = Any()
fun getInstance(): FtpManager {
val tmp = instance
if (tmp != null) {
return tmp
}
synchronized(instanceLock) {
if (instance == null) {
instance = FtpManager()
}
return instance!!
}
}
}
private var config: Config? = null
var callback: UploadCallback? = null
private val TAG = "FtpManager"
init {
val obj = this
Aria.upload(obj).register()
}
fun recycle(){
val obj = this
Aria.upload(obj).unRegister()
}
fun getTaskList(): List<UploadEntity>?{
val obj = this
return Aria.upload(obj).taskList
}
fun getAllNotCompleteTask(): List<UploadEntity>?{
val obj = this
return Aria.upload(obj).allNotCompleteTask
}
fun taskExists(taskId: Long): Boolean{
val obj = this
val ftp = Aria.upload(obj).loadFtp(taskId)
return ftp.taskExists()
}
fun removeAllTask(removeFile: Boolean = false){
val obj = this
Aria.upload(obj).removeAllTask(removeFile)
}
fun isComplete(taskId: Long): Boolean{
val obj = this
val ftp = Aria.upload(obj).loadFtp(taskId)
Log.e(TAG, "isComplete: 状态 ${ftp.taskState}")
return ftp.taskState == IEntity.STATE_COMPLETE
}
fun cancel(taskId: Long){
val obj = this
val ftp = Aria.upload(obj).loadFtp(taskId)
ftp?.cancel(true)
}
// fun cancel(filePath: String){
// val obj = this
// val ftp = Aria.upload(obj).loadFtp(filePath)
// ftp?.create()
// }
fun stop(taskId: Long){
val obj = this
val ftp = Aria.upload(obj).loadFtp(taskId)
ftp?.stop()
}
fun resume(taskId: Long, newStart: Boolean = false){
val obj = this
val ftp = Aria.upload(obj).loadFtp(taskId)
ftp?.resume(newStart)
}
var taskId: Long? = null
fun upload(config: Config, filePath: String): Unit{
val ftpOption = FtpOption()
ftpOption.login(config.user, config.pawd)
val obj = this
taskId = Aria.upload(obj)
.loadFtp(filePath) // 文件路径
.setUploadUrl("ftp://${config.user}:${config.pawd}@${config.ip}:${PORT}/${config.dir}") // 设置文件保存路径
.option(ftpOption)
.create()
Log.e(TAG, "upload: $filePath $taskId")
}
override fun onWait(task: UploadTask?) {
Log.e(TAG, "onWait $task")
}
override fun onPre(task: UploadTask?) {
Log.e(TAG, "onPre $task")
}
override fun onTaskPre(task: UploadTask?) {
Log.e(TAG, "onTaskPre $task")
}
override fun onTaskResume(task: UploadTask?) {
Log.e(TAG, "onTaskResume $task")
}
override fun onTaskStart(task: UploadTask?) {
Log.e(TAG, "onTaskStart $task")
callback?.onTaskStart(task)
}
override fun onTaskStop(task: UploadTask?) {
Log.e(TAG, "onTaskStop $task")
}
override fun onTaskCancel(task: UploadTask?) {
Log.e(TAG, "onTaskCancel $task")
}
override fun onTaskFail(task: UploadTask?, e: Exception?) {
Log.e(TAG, "onTaskFail $task $e")
e?.printStackTrace()
callback?.onTaskFail(task, e)
}
override fun onTaskComplete(task: UploadTask?) {
Log.e(TAG, "onTaskComplete $task")
callback?.onTaskComplete(task)
}
override fun onTaskRunning(task: UploadTask?) {
Log.e(TAG, "onTaskRunning $task ${task?.entity?.currentProgress}")
callback?.onTaskRunning(task)
}
override fun onNoSupportBreakPoint(task: UploadTask?) {
Log.e(TAG, "onNoSupportBreakPoint $task")
}
}
复制代码
3.在使用过程中遇到的问题
onTaskRunning 方法回调不及时,且当文件较小时,获取到的 task.entity.currentProgress 恒为 0;当文件较大如 10MB 以上,获取到的 task.entity.currentProgress 才会出现变化,但是改值的变化也是存在滞后性
注解不生效 @upload.onTaskRunning,代替的是 接口 UploadTaskListener,需要 register
划线
评论
复制
发布于: 1 小时前阅读数: 2
版权声明: 本文为 InfoQ 作者【Changing Lin】的原创文章。
原文链接:【http://xie.infoq.cn/article/ce7c5f986148d5e5a1e589bff】。文章转载请联系作者。
Changing Lin
关注
获得机遇的手段远超于固有常规之上~ 2020.04.29 加入
我能做的,就是调整好自己的精神状态,以最佳的面貌去面对那些未曾经历过得事情,对生活充满热情和希望。
评论