简体   繁体   English

在 Struts 中使用 HttpServletResponse 时如何从 Content-type 中删除字符集

[英]How to remove charset from Content-type when using HttpServletResponse in Struts

I'm trying to send a response with "application/json" content-type.我正在尝试使用“application/json”内容类型发送响应。 The server only accepts this content-type.服务器只接受这种内容类型。

But when I try to set the content-type, it automatically adds "charset=ISO-8859-1" and this is causing problems.但是当我尝试设置内容类型时,它会自动添加“charset=ISO-8859-1”,这会导致问题。 Is there a way to remove it?有没有办法删除它?

I've added the code of how I'm setting the content-type and getting the writer in HttpServletResponse.我已经添加了如何设置内容类型并在 HttpServletResponse 中获取作者的代码。

response.setContentType("application/json");
response.getWriter().println(json);

I need the content-type to be just "application/json".我需要内容类型只是“应用程序/json”。

try this:尝试这个:

response.setContentType(MediaType.APPLICATION_JSON_VALUE);
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(res.getBytes(StandardCharsets.UTF_8));
outputStream.flush();

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

相关问题 如何在 Spring boot(v2.4.2) 中从 Content-Type 响应头中删除 charset=UTF-8 - How to remove charset=UTF-8 from Content-Type response header in Spring boot(v2.4.2) 在Content-Type中指定charset时,Jersey和@FormParam无法正常工作 - Jersey and @FormParam not working when charset is specified in the Content-Type 如何抑制字符集被自动添加到 okhttp 中的 Content-Type - How to suppress Charset being automatically added to Content-Type in okhttp 如何在使用RestTemplate(来自其他客户端)时为分段上传中的文件设置内容类型 - How to set content-type for the file in multipart upload when using RestTemplate (from a rest client) 在Struts2中设置响应的内容类型 - Setting the content-type of a response in Struts2 不允许内容类型:Struts 2 中的文件上传 - Content-Type not allowed: fileUpload in Struts 2 如何在IBM HTTPD服务器上设置Content-Type HTTP标头的字符集部分? - How do I set the charset portion of the Content-Type HTTP Header on an IBM HTTPD Server? Java应用程序服务器如何决定在HTTP Content-type标头中发送什么字符集? - How does a Java application server decide what charset to send in the HTTP Content-type header? 在 Java 中解析 Content-Type 标头而不验证字符集 - Parsing a Content-Type header in Java without validating the charset 是内容类型“text/xml; 字符集=utf-8”错了吗? - Is content-type “text/xml; charset=utf-8” wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM