From 111e6c777e5e4d422614b4ad8bf61995ed73f848 Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Thu, 16 Mar 2023 09:01:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6=E5=90=8D?= =?UTF-8?q?=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/utils/UrlUtil.kt | 109 +++++++++--------- 1 file changed, 54 insertions(+), 55 deletions(-) diff --git a/app/src/main/java/io/legado/app/utils/UrlUtil.kt b/app/src/main/java/io/legado/app/utils/UrlUtil.kt index df2ee99ff..068e0a505 100644 --- a/app/src/main/java/io/legado/app/utils/UrlUtil.kt +++ b/app/src/main/java/io/legado/app/utils/UrlUtil.kt @@ -62,67 +62,66 @@ object UrlUtil { url: URL, headerMap: Map? = null ): String? { - // HEAD方式获取链接响应头信息 - val conn: HttpURLConnection = url.openConnection() as HttpURLConnection - conn.requestMethod = "HEAD" - // 下载链接可能还需要header才能成功访问 - headerMap?.forEach { key, value -> - conn.setRequestProperty(key, value) - } - // 禁止重定向 否则获取不到响应头返回的Location - conn.setInstanceFollowRedirects(false) - conn.connect() + // HEAD方式获取链接响应头信息 + val conn: HttpURLConnection = url.openConnection() as HttpURLConnection + conn.requestMethod = "HEAD" + // 下载链接可能还需要header才能成功访问 + headerMap?.forEach { key, value -> + conn.setRequestProperty(key, value) + } + // 禁止重定向 否则获取不到响应头返回的Location + conn.setInstanceFollowRedirects(false) + conn.connect() - // val fileSize = conn.getContentLengthLong() / 1024 - /** Content-Disposition 存在三种情况 - * filename="filename" - * filename=filename - * filename*=charset''filename - */ - val raw: String? = conn.getHeaderField("Content-Disposition") - // Location跳转到实际链接 - val redirectUrl: String? = conn.getHeaderField("Location") + // val fileSize = conn.getContentLengthLong() / 1024 + /** Content-Disposition 存在三种情况 + * filename="filename" + * filename=filename + * filename*=charset''filename + */ + val raw: String? = conn.getHeaderField("Content-Disposition") + // Location跳转到实际链接 + val redirectUrl: String? = conn.getHeaderField("Location") - return if (raw != null) { - val fileNames = raw.split(";".toRegex()).filter { it.contains("filename") } - val names = hashSetOf() - fileNames.forEach { - var filename = it.substringAfter("=") - if (it.contains("filename*")) { - val data = filename.split("''") - names.add(URLDecoder.decode(data[1], data[0])) - } else { - fileName = fileName - .replace("^\"".toRegex(), "") - .replace("\"$".toRegex(), "") - names.add( - String( - fileName.toByteArray(StandardCharsets.ISO_8859_1), - StandardCharsets.UTF_8 - ) + return if (raw != null) { + val fileNames = raw.split(";".toRegex()).filter { it.contains("filename") } + val names = hashSetOf() + fileNames.forEach { + var filename = it.substringAfter("=") + if (it.contains("filename*")) { + val data = filename.split("''") + names.add(URLDecoder.decode(data[1], data[0])) + } else { + fileName = fileName + .replace("^\"".toRegex(), "") + .replace("\"$".toRegex(), "") + names.add( + String( + fileName.toByteArray(StandardCharsets.ISO_8859_1), + StandardCharsets.UTF_8 ) - } - } - names.firstOrNull() - } else if (redirectUrl != null) { - val newUrl= URL(URLDecoder.decode(redirectUrl, "UTF-8")) - getFileNameFromPath(newUrl) - } else { - // 其余情况 返回响应头 - val headers = conn.getHeaderFields() - val headersString = buildString { - headers.forEach { key, value -> - value.forEach { - append(key) - append(": ") - append(it) - append("\n") - } + ) + } + } + names.firstOrNull() + } else if (redirectUrl != null) { + val newUrl= URL(URLDecoder.decode(redirectUrl, "UTF-8")) + getFileNameFromPath(newUrl) + } else { + // 其余情况 返回响应头 + val headers = conn.getHeaderFields() + val headersString = buildString { + headers.forEach { key, value -> + value.forEach { + append(key) + append(": ") + append(it) + append("\n") } } - AppLog.put("Cannot obtain URL file name:\n$headersString") - null } + AppLog.put("Cannot obtain URL file name:\n$headersString") + null } }