UnsupportedEncodingException当我从我的servlet doPost方法
问题描述:
移动逻辑我自己上课的时候我在我的servlet doPost方法有这样的代码,一切都很好。UnsupportedEncodingException当我从我的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
,使用接受一个字符集对象的构造,只是通过在标准UTF-8。
你能尝试它接受一个字符集的构造? (又名:'Charset.forName( “UTF-8”)') – NoDataFound 2014-09-22 23:55:08
为什么在我的servlet的doPost相同的代码工作的? – Blankman 2014-09-22 23:56:02
它目前在什么类?你是从已经抛出一个更通用的异常(如IOException)的某个Servlet类进行扩展吗? – mkobit 2014-09-22 23:56:41