将base64String上载到服务器我怎么知道进度?

问题描述

这是我的代码

   val urlConnection: HttpURLConnection = url.openConnection() as HttpURLConnection
                    var nameOnly = attachment.commonName.split("/")
                    urlConnection.requestMethod = "POST"
                    urlConnection.doInput = true
                    urlConnection.doOutput = true
                    urlConnection.setRequestProperty("Authorization","xelion " + prefs.authToken)
                    urlConnection.setRequestProperty("Content-Type",mime)
                    urlConnection.setRequestProperty("Content-disposition","attachment; filename=\"" + nameOnly.last() + "\"")
                    urlConnection.connect()
                    var decodedBytes = Base64.decode(attachment.contentsB64String,Base64.DEFAULT)
                    urlConnection.outputStream.write(decodedBytes)

                    val `is`: InputStream = urlConnection.inputStream
                    val b = ByteArray(1024)

                    while (`is`.read(b) !== -1) baos.write(b)

                    urlConnection.disconnect()
                    if (urlConnection.responseCode == 200) {
                        var stringValue = baos.toString()
                        FL.i("NETWORK","Reponse uploadFile is: " + stringValue)
                        var json = stringValue.substring(0,stringValue.indexOf("\u0000"))
                        var attachment = AttachmentReader().getobject(json)
                        callback.onSuccess(attachment)
                    } else {
                        FL.i("NETWORK","Error Reponse uploadFile is: " + urlConnection.responseMessage)
                        callback.onError(RestResponse(null,baos.toString(),ErrorResponse(urlConnection.responseCode,urlConnection.responseMessage)))
                    }

我尝试过类似的操作:https://stackoverflow.com/a/45530367/1307987 但是它只会在最后一次被调用。我在做什么错了?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)