写点什么

Android 的 Ftp 断点上传 ---Aria 使用笔记

用户头像
Changing Lin
关注
发布于: 1 小时前
Android的Ftp断点上传---Aria使用笔记

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.Logimport com.arialyy.aria.core.Ariaimport com.arialyy.aria.core.common.FtpOptionimport com.arialyy.aria.core.inf.IEntityimport com.arialyy.aria.core.task.UploadTaskimport com.arialyy.aria.core.upload.UploadEntityimport com.arialyy.aria.core.upload.UploadTaskListenerimport 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
用户头像

Changing Lin

关注

获得机遇的手段远超于固有常规之上~ 2020.04.29 加入

我能做的,就是调整好自己的精神状态,以最佳的面貌去面对那些未曾经历过得事情,对生活充满热情和希望。

评论

发布
暂无评论
Android的Ftp断点上传---Aria使用笔记