优化文件名获取
This commit is contained in:
@@ -62,67 +62,66 @@ object UrlUtil {
|
|||||||
url: URL,
|
url: URL,
|
||||||
headerMap: Map<String, String>? = null
|
headerMap: Map<String, String>? = null
|
||||||
): String? {
|
): String? {
|
||||||
// HEAD方式获取链接响应头信息
|
// HEAD方式获取链接响应头信息
|
||||||
val conn: HttpURLConnection = url.openConnection() as HttpURLConnection
|
val conn: HttpURLConnection = url.openConnection() as HttpURLConnection
|
||||||
conn.requestMethod = "HEAD"
|
conn.requestMethod = "HEAD"
|
||||||
// 下载链接可能还需要header才能成功访问
|
// 下载链接可能还需要header才能成功访问
|
||||||
headerMap?.forEach { key, value ->
|
headerMap?.forEach { key, value ->
|
||||||
conn.setRequestProperty(key, value)
|
conn.setRequestProperty(key, value)
|
||||||
}
|
}
|
||||||
// 禁止重定向 否则获取不到响应头返回的Location
|
// 禁止重定向 否则获取不到响应头返回的Location
|
||||||
conn.setInstanceFollowRedirects(false)
|
conn.setInstanceFollowRedirects(false)
|
||||||
conn.connect()
|
conn.connect()
|
||||||
|
|
||||||
// val fileSize = conn.getContentLengthLong() / 1024
|
// val fileSize = conn.getContentLengthLong() / 1024
|
||||||
/** Content-Disposition 存在三种情况
|
/** Content-Disposition 存在三种情况
|
||||||
* filename="filename"
|
* filename="filename"
|
||||||
* filename=filename
|
* filename=filename
|
||||||
* filename*=charset''filename
|
* filename*=charset''filename
|
||||||
*/
|
*/
|
||||||
val raw: String? = conn.getHeaderField("Content-Disposition")
|
val raw: String? = conn.getHeaderField("Content-Disposition")
|
||||||
// Location跳转到实际链接
|
// Location跳转到实际链接
|
||||||
val redirectUrl: String? = conn.getHeaderField("Location")
|
val redirectUrl: String? = conn.getHeaderField("Location")
|
||||||
|
|
||||||
return if (raw != null) {
|
return if (raw != null) {
|
||||||
val fileNames = raw.split(";".toRegex()).filter { it.contains("filename") }
|
val fileNames = raw.split(";".toRegex()).filter { it.contains("filename") }
|
||||||
val names = hashSetOf<String>()
|
val names = hashSetOf<String>()
|
||||||
fileNames.forEach {
|
fileNames.forEach {
|
||||||
var filename = it.substringAfter("=")
|
var filename = it.substringAfter("=")
|
||||||
if (it.contains("filename*")) {
|
if (it.contains("filename*")) {
|
||||||
val data = filename.split("''")
|
val data = filename.split("''")
|
||||||
names.add(URLDecoder.decode(data[1], data[0]))
|
names.add(URLDecoder.decode(data[1], data[0]))
|
||||||
} else {
|
} else {
|
||||||
fileName = fileName
|
fileName = fileName
|
||||||
.replace("^\"".toRegex(), "")
|
.replace("^\"".toRegex(), "")
|
||||||
.replace("\"$".toRegex(), "")
|
.replace("\"$".toRegex(), "")
|
||||||
names.add(
|
names.add(
|
||||||
String(
|
String(
|
||||||
fileName.toByteArray(StandardCharsets.ISO_8859_1),
|
fileName.toByteArray(StandardCharsets.ISO_8859_1),
|
||||||
StandardCharsets.UTF_8
|
StandardCharsets.UTF_8
|
||||||
)
|
|
||||||
)
|
)
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
names.firstOrNull()
|
}
|
||||||
} else if (redirectUrl != null) {
|
names.firstOrNull()
|
||||||
val newUrl= URL(URLDecoder.decode(redirectUrl, "UTF-8"))
|
} else if (redirectUrl != null) {
|
||||||
getFileNameFromPath(newUrl)
|
val newUrl= URL(URLDecoder.decode(redirectUrl, "UTF-8"))
|
||||||
} else {
|
getFileNameFromPath(newUrl)
|
||||||
// 其余情况 返回响应头
|
} else {
|
||||||
val headers = conn.getHeaderFields()
|
// 其余情况 返回响应头
|
||||||
val headersString = buildString {
|
val headers = conn.getHeaderFields()
|
||||||
headers.forEach { key, value ->
|
val headersString = buildString {
|
||||||
value.forEach {
|
headers.forEach { key, value ->
|
||||||
append(key)
|
value.forEach {
|
||||||
append(": ")
|
append(key)
|
||||||
append(it)
|
append(": ")
|
||||||
append("\n")
|
append(it)
|
||||||
}
|
append("\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
AppLog.put("Cannot obtain URL file name:\n$headersString")
|
|
||||||
null
|
|
||||||
}
|
}
|
||||||
|
AppLog.put("Cannot obtain URL file name:\n$headersString")
|
||||||
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user