Skip to content

Commit

Permalink
清理冗余代码
Browse files Browse the repository at this point in the history
  • Loading branch information
jsonwan committed May 19, 2021
1 parent bbbab27 commit d5f0094
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 151 deletions.
49 changes: 0 additions & 49 deletions src/main/java/com/tencent/bk/devops/atom/task/utils/FileUtils.kt

This file was deleted.

14 changes: 0 additions & 14 deletions src/main/java/com/tencent/bk/devops/atom/task/utils/JsonUtils.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,45 +1,18 @@
package com.tencent.bk.devops.atom.task.utils

import java.io.File
import java.io.FileOutputStream
import java.io.UnsupportedEncodingException
import java.net.URLEncoder
import java.util.ArrayList
import java.util.concurrent.TimeUnit
import okhttp3.MediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import org.slf4j.LoggerFactory

object OkhttpUtils {

private val logger = LoggerFactory.getLogger(OkhttpUtils::class.java)

val jsonMediaType = MediaType.parse("application/json")

private val okHttpClient = okhttp3.OkHttpClient.Builder()
private val okHttpClient = OkHttpClient.Builder()
.connectTimeout(5L, TimeUnit.SECONDS)
.readTimeout(30L, TimeUnit.SECONDS)
.writeTimeout(30L, TimeUnit.SECONDS)
.build()!!

// 下载会出现从 文件源--(耗时长)---->网关(网关全部收完才转发给用户,所以用户侧与网关存在读超时的可能)-->用户
private val longHttpClient = okhttp3.OkHttpClient.Builder()
.connectTimeout(5L, TimeUnit.SECONDS)
.readTimeout(30L, TimeUnit.MINUTES)
.writeTimeout(30L, TimeUnit.MINUTES)
.build()!!

@Throws(UnsupportedEncodingException::class)
fun joinParams(params: Map<String, String>): String {
val paramItem = ArrayList<String>()
for ((key, value) in params) {
paramItem.add(key + "=" + URLEncoder.encode(value, "UTF-8"))
}
return paramItem.joinToString("&")
}

fun doGet(url: String, headers: Map<String, String> = mapOf()): Response {
return doGet(okHttpClient, url, headers)
}
Expand All @@ -48,14 +21,6 @@ object OkhttpUtils {
return doHttp(okHttpClient, request)
}

fun doLongGet(url: String, headers: Map<String, String> = mapOf()): Response {
return doGet(longHttpClient, url, headers)
}

fun doLongHttp(request: Request): Response {
return doHttp(longHttpClient, request)
}

private fun doGet(okHttpClient: OkHttpClient, url: String, headers: Map<String, String> = mapOf()): Response {
val requestBuilder = Request.Builder()
.url(url)
Expand All @@ -73,56 +38,4 @@ object OkhttpUtils {
return okHttpClient.newCall(request).execute()
}

fun downloadFile(url: String, destPath: File) {
val request = Request.Builder()
.url(url)
.get()
.build()
longHttpClient.newCall(request).execute().use { response ->
if (response.code() == 404) {
logger.warn("The file $url is not exist")
throw RuntimeException("文件不存在")
}
if (!response.isSuccessful) {
logger.warn("fail to download the file from $url because of ${response.message()} and code ${response.code()}")
throw RuntimeException("获取文件失败")
}
if (!destPath.parentFile.exists()) {
destPath.parentFile.mkdirs()
logger.info("mkdir file:${destPath.parentFile.name}")
}
val buf = ByteArray(4096)
response.body()!!.byteStream().use { bs ->
var len = bs.read(buf)
FileOutputStream(destPath).use { fos ->
while (len != -1) {
fos.write(buf, 0, len)
len = bs.read(buf)
}
}
}
}
}

fun downloadFile(response: Response, destPath: File) {
if (response.code() == 304) {
logger.info("file is newest, do not download to $destPath")
return
}
if (!response.isSuccessful) {
logger.warn("fail to download the file because of ${response.message()} and code ${response.code()}")
throw RuntimeException("获取文件失败")
}
if (!destPath.parentFile.exists()) destPath.parentFile.mkdirs()
val buf = ByteArray(4096)
response.body()!!.byteStream().use { bs ->
var len = bs.read(buf)
FileOutputStream(destPath).use { fos ->
while (len != -1) {
fos.write(buf, 0, len)
len = bs.read(buf)
}
}
}
}
}

0 comments on commit d5f0094

Please sign in to comment.