gpt4 book ai didi

java - 为什么这个 Spring MVC Controller 返回损坏的 UTF-8 字符串?

转载 作者:行者123 更新时间:2023-11-29 03:16:36 25 4
gpt4 key购买 nike

为了让我的 Spring MVC 使用 UTF-8,我修改了很多地方,包括 char 过滤器、jsp 中的 contextType,以及修复了 MySQL,所以我的项目可以完美地使用 UTF-8。

但是,这个新添加的函数并不能正常工作

@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";

如您所见,我已经设置了 procudes 和 setCharacterEncoding。返回的字符串在 Ajax 中使用,我有

        var ajax = new XMLHttpRequest();
ajax.overrideMimeType('text/xml;charset=UTF-8');

这是jsp中表单的开头

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

但我的网页仍然显示????。那我还缺少什么?我没有 JQuery,所以除非必须,否则希望有一个不使用它的解决方案。

最佳答案

找到关于 ResponseBody 的链接由于 Spring web“错误”导致返回 UTF-8 字符串的问题

总结:

  1. 设置org.springframework.web.filter.CharacterEncodingFilter在 web.xml 中执行 UTF-8
  2. 设置<value>text/plain;charset=UTF-8</value>在 spring-servlet.xml 中
  3. StringHttpMessageConverter通过创建自己的类解决问题

关于java - 为什么这个 Spring MVC Controller 返回损坏的 UTF-8 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26209802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com