简体   繁体   English

如何在 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)

I'm trying to download a pdf file from s3 and then return a response as byte array.我正在尝试从 s3 下载 pdf 文件,然后以字节数组的形式返回响应。 Everything is working find except the "Content-Type", in Content-Type the charset=UTF-8 is getting added.除了“Content-Type”之外,一切都在工作,在 Content-Type 中添加了 charset=UTF-8。

I have included "spring.http.encoding.force=false" in application.properties.我在 application.properties 中包含了“spring.http.encoding.force=false”。 Below is my code snipet:下面是我的代码片段:

@GetMapping(value = "/view/{clientId}/{documentId}", produces = { "application/pdf" })
public ResponseEntity<?> downloadDocument(@PathVariable String clientId,
                                              @PathVariable String documentId) {

....
return ResponseEntity.ok()
                    .contentLength(...)
                    .header(HttpHeaders.CONTENT_TYPE, "application/pdf")
                    .header(HttpHeaders.CONTENT_DISPOSITION,"inline; filename=\"download.pdf\"")
                    .body(downloadInputStream.toByteArray());
}

In the response log I'm getting following:在响应日志中,我得到以下信息:

{
  "type": "response",
  "correlation": "e297f48c4475d510",
  "direction": "EGRESS",
  "duration": 5536,
  "protocol": "HTTP/1.1",
  "status": 200,
  "headers": {
    "Keep-Alive": "timeout=360",
    "x-request-id": "e297f48c4475d510",
    "Content-Disposition": "inline; filename=\"download.pdf\"",
    "Connection": "keep-alive",
    "Content-Length": "864038",
    "Date": "Thu, 01 Jul 2021 09:53:08 GMT",
    "Content-Type": "application/pdf;charset=UTF-8"
  }
}

Have you try below properties?您是否尝试过以下属性?

server.servlet.encoding.force=false
server.servlet.encoding.forceResponse=false

However, both of the properties should default to false, so unless it is explicitly set to true, it won't set the encoding for you.但是,这两个属性都应默认为 false,因此除非显式设置为 true,否则不会为您设置编码。

Another posibility is that HttpServletResponse.setCharacterEncoding() is called somewhere in your code or library.另一种可能性是HttpServletResponse.setCharacterEncoding()在您的代码或库中的某处被调用。 You can try to debug on that method to see if any class is calling it.您可以尝试调试该方法以查看是否有任何类正在调用它。

charset=UTF-8 is usually for plain text simple pdf files and because there is no "Content-Transfer-Encoding" the server is just assuming. charset=UTF-8 通常用于纯文本简单的 pdf 文件,因为没有“内容传输编码”,服务器只是假设。 inline is load and use , attachment pops a dialog box.内联是加载和使用,附件弹出对话框。 Page 2 and 3 in the link show how to properly set a servlet download.链接中的第 2 页和第 3 页显示了如何正确设置 servlet 下载。 http://www.windsolarhybridaustralia.x10.mx/Ramfile-html_B_.pdf http://www.windsolarhybridaustralia.x10.mx/Ramfile-html_B_.pdf

暂无
暂无

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

相关问题 是内容类型“text/xml; 字符集=utf-8”错了吗? - Is content-type “text/xml; charset=utf-8” wrong? spring boot mvc - 不支持内容类型'application / json; charset = UTF-8' - spring boot mvc - Content type 'application/json;charset=UTF-8' not supported 不支持 Spring Boot 内容类型 'multipart/form-data;boundary=------------------------#;charset=UTF-8' - Spring Boot Content type 'multipart/form-data;boundary=--------------------------#;charset=UTF-8' not supported "<i>ElasticsearchException[Failed to parse info response.<\/i> ElasticsearchException [无法解析信息响应。<\/b> <i>Check logs for detailed information - Unsupported Content-Type: text\/html;<\/i>检查日志以获取详细信息 - 不支持的内容类型:文本\/html;<\/b> <i>charset=utf-8]<\/i>字符集=utf-8]<\/b>" - ElasticsearchException[Failed to parse info response. Check logs for detailed information - Unsupported Content-Type: text/html; charset=utf-8] Spring Boot - 不支持内容类型“application/x-www-form-urlencoded;charset=UTF-8” - Spring Boot - Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported Spring 启动 controller 调用不支持内容类型 'application/json;charset=UTF-8' - Content type 'application/json;charset=UTF-8' not supported with Spring boot controller call 不支持的内容类型:文本/xml; charset=utf-8 支持的有:[application/soap+xml] - Unsupported Content-Type: text/xml; charset=utf-8 Supported ones are: [application/soap+xml] 消耗=“charset=utf-8”不工作 Spring 启动 - consumes=“charset=utf-8” not working Spring boot 不支持的内容类型:text / html; charset = UTF-8支持的是:Jdev中的[text / xml] - Unsupported Content-Type: text/html; charset=UTF-8 Supported ones are: [text/xml] in Jdev 内容类型 &#39;application/json;charset=UTF-8&#39; - Content type 'application/json;charset=UTF-8'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM