Procházet zdrojové kódy

UrlUtil.getFileName 使用head方式

Xwite před 3 roky
rodič
revize
47319ef4ba
1 změnil soubory, kde provedl 7 přidání a 11 odebrání
  1. 7 11
      app/src/main/java/io/legado/app/utils/UrlUtil.kt

+ 7 - 11
app/src/main/java/io/legado/app/utils/UrlUtil.kt

@@ -30,14 +30,15 @@ object UrlUtil {
     }
     }
 
 
     /**
     /**
-     * 根据url获取文件名
+     * 根据网络url获取文件名
      */
      */
     fun getFileName(fileUrl: String): String? {
     fun getFileName(fileUrl: String): String? {
         return kotlin.runCatching {
         return kotlin.runCatching {
             var fileName = ""
             var fileName = ""
             val url = URL(fileUrl)
             val url = URL(fileUrl)
             val conn: HttpURLConnection = url.openConnection() as HttpURLConnection
             val conn: HttpURLConnection = url.openConnection() as HttpURLConnection
-            conn.requestMethod = "GET"
+            // head方式
+            conn.requestMethod = "HEAD"
             conn.connect()
             conn.connect()
 
 
             // 方法一
             // 方法一
@@ -47,18 +48,13 @@ object UrlUtil {
                 fileName =
                 fileName =
                     String(
                     String(
                         fileName.toByteArray(StandardCharsets.ISO_8859_1),
                         fileName.toByteArray(StandardCharsets.ISO_8859_1),
-                        StandardCharsets.UTF_8
+                        StandardCharsets.UTF_8 //?
                     )
                     )
             } else {
             } else {
-                // 方法二
-                var newUrl: String = conn.url.file
+                // 方法二 截取
+                var newUrl: String = url.path ?: return null
                 newUrl = URLDecoder.decode(newUrl, "UTF-8")
                 newUrl = URLDecoder.decode(newUrl, "UTF-8")
-                var pos = newUrl.indexOf('?')
-                if (pos > 0) {
-                    newUrl = newUrl.substring(0, pos)
-                }
-                pos = newUrl.lastIndexOf('/')
-                fileName = newUrl.substring(pos + 1)
+                fileName = newUrl.substringAfterLast("/")
             }
             }
             fileName
             fileName
         }.getOrNull()
         }.getOrNull()