UrlUtil.getFileName 使用head方式
This commit is contained in:
@@ -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('?')
|
fileName = newUrl.substringAfterLast("/")
|
||||||
if (pos > 0) {
|
|
||||||
newUrl = newUrl.substring(0, pos)
|
|
||||||
}
|
|
||||||
pos = newUrl.lastIndexOf('/')
|
|
||||||
fileName = newUrl.substring(pos + 1)
|
|
||||||
}
|
}
|
||||||
fileName
|
fileName
|
||||||
}.getOrNull()
|
}.getOrNull()
|
||||||
|
|||||||
Reference in New Issue
Block a user