kotlin httpservletrequest对象为字符串

时间:2018-03-14 17:51:19

标签: http servlets kotlin

将http servlet请求对象转换为字符串时,我总是得到一个“”字符串。这是我的代码。

private fun getBody(request: HttpServletRequest): String {
    var inputAsString = ""
    try {
        var inputStream: InputStream  = request.inputStream
        if (inputStream != null) {
            inputAsString = inputStream.bufferedReader().use { it.readText() }
        }
    } catch (ex: IOException) {
        throw ex
    }
    return inputAsString
}

1 个答案:

答案 0 :(得分:0)

1)如果你像这样重写代码,你的代码看起来会更好:

private fun getBody(request: HttpServletRequest): String = request
    .inputStream.bufferedReader().use { it.readText() }

2)您的问题不在此代码中,因为它有效!