当前位置: 首页 > wzjs >正文

wordpress 引用视频宁波关键词优化企业网站建设

wordpress 引用视频,宁波关键词优化企业网站建设,跑腿app开发价格表,爱做电影网站一、背景 根据后端返回的url下载地址,去执行文件下载,将文件保存到SD卡。这里使用Retrofit网络框架。 二、代码实现 2.1、定义一个DownloadFileService interface DownloadFileService {StreamingGETsuspend fun downloadFile(Url fileUrl: String):ResponseBody…

一、背景

根据后端返回的url下载地址,去执行文件下载,将文件保存到SD卡。这里使用Retrofit网络框架。

二、代码实现

2.1、定义一个DownloadFileService
interface DownloadFileService {@Streaming@GETsuspend fun downloadFile(@Url fileUrl: String):ResponseBody
}
2.2、定义一个FileDownloadClient
private var mDownliadService: DownloadFileService? = nullfun getUploadFileService() :DownloadFileService{if(mDownliadService==null){val okHttpClient = OkHttpClient.Builder().callTimeout(0, TimeUnit.SECONDS)//0 代表不考虑请求的超时 这里的超时是指整个请求过程的超时,如果设置太短,任务还没执行完,会以超时结束任务,这里尤其要注意.connectTimeout(60, TimeUnit.SECONDS).readTimeout(60, TimeUnit.SECONDS).writeTimeout(60, TimeUnit.SECONDS).retryOnConnectionFailure(false)// 重连.followRedirects(false)// 重定向//.addInterceptor(RequestInterceptor(authorization,requestId,offset,uploadType))//.addInterceptor(RemoveContentLengthInterceptor())//.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).build()val retrofit: Retrofit = Retrofit.Builder().baseUrl(DeviceUtil.getInstance().getServerIp()).addConverterFactory(GsonConverterFactory.create()).client(okHttpClient).build()mDownliadService= retrofit.create(DownloadFileService::class.java)}return mDownliadService!!}
2.3、定义一个DownloadCallback
interface DownloadCallback {fun onSuccess(srcApkTarPath:String)fun onProgress(progress: Int)fun onFailure(msg:String?)
}
2.4、定义一个FileDownloadManager,执行文件下载
class FileDownloadManager {private val TAG = "FileDownloadManager_"companion object {private var singleInstance: FileDownloadManager? = nullget() {// 懒汉模式if (null == field) {field = FileDownloadManager()}return field}@Synchronized // 添加注解,线程同步,线程安全fun getInstance(): FileDownloadManager {return singleInstance!! // 表示非空时执行}}fun doDownloadFile(appName:String,packageName: String,downUrl: String,fileSize: Long,fileMd5:String,downloadCallback: DownloadCallback) {try {val coroutineScope = CoroutineScope(Dispatchers.Default)coroutineScope.launch(Dispatchers.IO) {val response = FileDownloadClient.getUploadFileService().downloadFile(downUrl)val length = response.contentLength()XLogUtil.d("${TAG}doDownloadFile result appName:$appName,,,packageName:$packageName,,,length:$length,,,fileSize:$fileSize,,,downUrl:$downUrl")if (length > 0) {val folder = File(ConstantUtil.DOWNLOAD_FOLDER_PATH)if (!folder.exists()) {folder.mkdirs()}// 使用输入流保存响应体到文件val inputStream = response.byteStream()val fileName = "$packageName.tar"var fileTargetPath =ConstantUtil.DOWNLOAD_FOLDER_PATH + File.separator + fileNameval outputStream = FileOutputStream(fileTargetPath)val buf = ByteArray(1024)var downLoadFileSize = 0var lastProgress = 0do {val numread = inputStream.read(buf)if (numread == -1) {break}outputStream.write(buf, 0, numread)downLoadFileSize += numreadval progressValue =((downLoadFileSize * 100f) / length).toInt()//相同的进度值只回调一次if (progressValue > lastProgress) {lastProgress = progressValuedownloadCallback?.apply {onProgress(progressValue)}}} while (true)outputStream.flush()// 关闭文件输出流和输入流outputStream.close()//inputStream.close()val localFileMd5=FileUtil.getInstance().getFileMd5(fileTargetPath)if(localFileMd5==fileMd5){downloadCallback?.onSuccess(fileTargetPath)}else{downloadCallback?.onFailure("文件 md5 不一致,localFileMd5:$localFileMd5,,,fileMd5:$fileMd5")}}else{downloadCallback?.onFailure("读取文件len=0")}}} catch (e: Exception) {XLogUtil.e("${TAG}doDownloadFile Exception appName:$appName,,,packageName:$packageName,,,Exception:${e.message},,,fileSize:$fileSize,,,downUrl:$downUrl")downloadCallback?.onFailure(e.message)}}
}

http://www.dtcms.com/wzjs/190843.html

相关文章:

  • 专门做免费东西试吃的网站seo什么意思简单来说
  • 做徽商要做网站吗百度推广和优化哪个好
  • 网站的建设进入哪个科目网站建设流程是什么
  • 网站建设的cms系统国外域名注册平台
  • 网站建设课题简介他达拉非功效与作用主要会有哪些
  • 综合性网站模板苏州seo关键词优化外包
  • 陕西省建设厅管理中心网站一站式媒体发稿平台
  • 软件开发属于什么行业类别南京谷歌seo
  • 企查查入口网页版seo黑帽培训骗局
  • 外贸软件定制网站seo优化心得
  • 宝塔面板WordPress优化鹤壁seo
  • wordpress隐藏站点身份流量平台有哪些
  • 浙江建设工程招标网优化大师官方下载
  • 网站建设公司现状网址链接生成器
  • 做我男朋友好不好网站天津企业seo
  • 牡丹江建设厅网站网店代运营需要多少钱
  • 加快公司网站建设seo排名优化有哪些
  • 做彩票网站多少钱seo分析案例
  • 贵州省交通建设集团网站杭州seo建站
  • 做网站jw100网站开发需要的技术
  • 电脑维修 做网站谷歌推广代理
  • .com的网站需要备案吗google关键词分析
  • 做网站到八方资源网怎么样网络产品及其推广方法
  • 创意合肥网站建设网页优化方法
  • 做二维码网站各大搜索引擎入口
  • 网站用什么格式的图片格式广告联盟app推广
  • 公司已有网站 如何自己做推广广告推广平台代理
  • 品牌网站的推广电商软文范例300字
  • seo网站服务公司怎样让自己的网站排名靠前
  • 公司网站建设费会计分录百度人工客服24小时电话