When I have this code in my servlet doPost method, everything is fine.
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
...
String data = CharStreams.toString(new InputStreamReader(request.getInputStream(), "UTF-8"));
...
}
I wanted to move this code out to another class, but when I did that I get this error that intellij reports:
java.io.UnsupportedEncodingException
public class SomeUtil {
public String readFromInput(ServletInputStream is) {
return CharStreams.toString(new InputStreamReader(is, "UTF-8"));
}
}
Does the servlet doPost somewhere handle this exception that I can't see?
The doPost()
method declares throws ServletException, IOException
which covers for the throws UnsupportedEncodingException
in the InputStreamReader constructor you are using.
In your readFromInput()
method, you could declare a throws
clause as well or surround that line in a try-catch block. To avoid the UnsupportedEncodingException
, use the constructor that accepts a Charset object and just pass in the standard UTF-8 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.