This commit is contained in:
kunfei
2023-03-14 23:41:30 +08:00
parent 90c7030688
commit eddef060ea
2 changed files with 13 additions and 14 deletions

View File

@@ -241,7 +241,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
webFiles.clear()
val fileName = "${book.name} 作者:${book.author}"
book.downloadUrls!!.map {
val mFileName = "${fileName}.${LocalBook.parseFileSuffix(it)}"
val mFileName = UrlUtil.getFileName(it) ?: fileName
val isSupportedFile = AppPattern.bookFileRegex.matches(mFileName)
val isSupportDecompress = AppPattern.archiveFileRegex.matches(mFileName)
WebFile(it, mFileName, isSupportedFile, isSupportDecompress)

View File

@@ -29,9 +29,12 @@ object UrlUtil {
.replace("|", "%7C")
}
fun getFileName(fileUrl: String): String {
var fileName = ""
try {
/**
* 根据url获取文件名
*/
fun getFileName(fileUrl: String): String? {
return kotlin.runCatching {
var fileName = ""
val url = URL(fileUrl)
val conn: HttpURLConnection = url.openConnection() as HttpURLConnection
conn.requestMethod = "GET"
@@ -46,23 +49,19 @@ object UrlUtil {
fileName.toByteArray(StandardCharsets.ISO_8859_1),
StandardCharsets.UTF_8
)
}
// 方法二
var newUrl: String? = conn.url.file
if (newUrl == null || newUrl.isEmpty()) {
} else {
// 方法二
var newUrl: String = conn.url.file
newUrl = URLDecoder.decode(newUrl, "UTF-8")
var pos = newUrl.indexOf('?')
if (pos > 0) {
newUrl = newUrl.substring(0, pos)
}
pos = newUrl!!.lastIndexOf('/')
pos = newUrl.lastIndexOf('/')
fileName = newUrl.substring(pos + 1)
}
} catch (e: Exception) {
e.printStackTrace()
}
return fileName
fileName
}.getOrNull()
}
fun getSuffix(url: String, default: String): String {