繁体   English   中英

当我从Servlet doPost方法将逻辑移至自己的类时,出现UnsupportedEncodingException

[英]UnsupportedEncodingException when I moved logic to my own class from my Servlet doPost method

当我的servlet doPost方法中包含此代码时,一切都很好。

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
...
String data = CharStreams.toString(new InputStreamReader(request.getInputStream(), "UTF-8"));
...
}

我想将这段代码移到另一个类,但是当我这样做时,我收到了Intellij报告的错误:

java.io.UnsupportedEncodingException

public class SomeUtil {

  public String readFromInput(ServletInputStream is) {

    return CharStreams.toString(new InputStreamReader(is, "UTF-8"));
  }

}

Servlet doPost是否可以处理我看不到的异常?

doPost()方法声明throws ServletException, IOException覆盖了正在使用的InputStreamReader构造函数中的throws UnsupportedEncodingException

在您的readFromInput()方法中,您也可以声明throws子句,或将该行包含在try-catch块中。 为避免UnsupportedEncodingException ,请使用接受Charset对象并仅传递标准UTF-8的构造函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM