From f1bbea129c6104f07b4cdb640f2eccddfdd10c4a Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 25 Jul 2021 01:23:30 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=99cronet=E6=B7=BB=E5=8A=A0=E9=BB=98?= =?UTF-8?q?=E8=AE=A4content-type=E9=98=B2=E6=AD=A2=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/help/http/cronet/CronetHelper.kt | 2 ++ .../java/io/legado/app/help/storage/BookWebDav.kt | 4 ++-- app/src/main/java/io/legado/app/lib/webdav/WebDav.kt | 11 +++++++---- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/http/cronet/CronetHelper.kt b/app/src/main/java/io/legado/app/help/http/cronet/CronetHelper.kt index b6d50f116..e47a4abfa 100644 --- a/app/src/main/java/io/legado/app/help/http/cronet/CronetHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/cronet/CronetHelper.kt @@ -53,6 +53,8 @@ fun buildRequest(request: Request, callback: UrlRequest.Callback): UrlRequest { val contentType: MediaType? = requestBody.contentType() if (contentType != null) { requestBuilder.addHeader("Content-Type", contentType.toString()) + } else { + requestBuilder.addHeader("Content-Type", "text/plain") } val buffer = Buffer() requestBody.writeTo(buffer) diff --git a/app/src/main/java/io/legado/app/help/storage/BookWebDav.kt b/app/src/main/java/io/legado/app/help/storage/BookWebDav.kt index 6147ff1db..cbdbd15ce 100644 --- a/app/src/main/java/io/legado/app/help/storage/BookWebDav.kt +++ b/app/src/main/java/io/legado/app/help/storage/BookWebDav.kt @@ -131,7 +131,7 @@ object BookWebDav { WebDav(exportsWebDavUrl).makeAsDir() // 如果导出的本地文件存在,开始上传 val putUrl = exportsWebDavUrl + fileName - WebDav(putUrl).upload(byteArray) + WebDav(putUrl).upload(byteArray, "text/plain") } } catch (e: Exception) { Handler(Looper.getMainLooper()).post { @@ -153,7 +153,7 @@ object BookWebDav { val json = GSON.toJson(bookProgress) val url = getProgressUrl(book) if (initWebDav()) { - WebDav(url).upload(json.toByteArray()) + WebDav(url).upload(json.toByteArray(), "application/json") } } } diff --git a/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt b/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt index e09b83c46..93e3752b2 100644 --- a/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt +++ b/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt @@ -185,11 +185,14 @@ class WebDav(urlStr: String) { /** * 上传文件 */ - suspend fun upload(localPath: String, contentType: String? = null): Boolean { + suspend fun upload( + localPath: String, + contentType: String = "application/octet-stream" + ): Boolean { val file = File(localPath) if (!file.exists()) return false // 务必注意RequestBody不要嵌套,不然上传时内容可能会被追加多余的文件信息 - val fileBody = file.asRequestBody(contentType?.toMediaType()) + val fileBody = file.asRequestBody(contentType.toMediaType()) val url = httpUrl val auth = HttpAuth.auth if (url != null && auth != null) { @@ -204,9 +207,9 @@ class WebDav(urlStr: String) { return false } - suspend fun upload(byteArray: ByteArray, contentType: String? = null): Boolean { + suspend fun upload(byteArray: ByteArray, contentType: String): Boolean { // 务必注意RequestBody不要嵌套,不然上传时内容可能会被追加多余的文件信息 - val fileBody = byteArray.toRequestBody(contentType?.toMediaType()) + val fileBody = byteArray.toRequestBody(contentType.toMediaType()) val url = httpUrl val auth = HttpAuth.auth if (url != null && auth != null) {