Splash Splash - 3 months ago 23
Ajax Question

Why this Spring MVC controller returns corrupted UTF-8 String?

I have modified so many places to make my Spring MVC work with UTF-8, including a char filter, contextType in jsp, and fixed MySQL as well, so my project is working perfectly with UTF-8.

However, this newly added function wouldn't get it right

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
public @ResponseBody
String uploadFileHandler(
@RequestParam("name") String name,
@RequestParam("type") String type,
@RequestParam("file") MultipartFile file//, HttpServletResponse response
) throws IOException {
// response.setCharacterEncoding("UTF-8");
if (file.isEmpty())
return "Empty";

As you can see, I have put procudes, and setCharacterEncoding. The returned String is used in Ajax and I have

var ajax = new XMLHttpRequest();

Here is the beginning of the form in jsp

<form name = "myForm" method="POST" onsubmit="return validateForm()" enctype="multipart/form-data">

But my webpage still shows ????. So what else am I missing? I don't have JQuery, so hoping a solution without using it unless a must.


Found this link about ResponseBody problem with return UTF-8 string due to Spring web "bug"


  1. set org.springframework.web.filter.CharacterEncodingFilter in web.xml to enforce UTF-8
  2. set <value>text/plain;charset=UTF-8</value> in spring-servlet.xml
  3. StringHttpMessageConverter problem eliminated by creating own class