Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem that the download file name is invalid for response.setHeader settings

Shulou Source: shulou.com Published: 2022-06-01 20:01:13 09月29日 Update

This article mainly explains how to solve the problem of "response.setHeader setting download file name invalid". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian slowly and deeply to study and learn "response.setHeader setting download file name invalid" together.

response.setHeader setting download filename invalid response.setContentType("application/octet-stream");response.setHeader("Content-Disposition", "attachment; filename=down.xls");

After setting, click the download file name invalid, is the name of the method

solve

Put the above code in front of the stream.

response.setheader is invalid

Recently, when building the architecture, we used HandlerInterceptorAdapter.

response.setHeader("token", token) is used in afterCompletion or postHandle;

The idea was to return a new jwt token to the caller after the method execution was complete.

However, the actual return does not get the value of set.

reason is

Response header settings, to load the buffer before the response content, http protocol is in accordance with the response status line, the response header and response body output order, after the header does not take effect. Therefore, writing a header in the preHandle can take effect.

output buffer

The response body of the HTTP message output by the Servlet program is first written to an output buffer provided by the Servlet engine, and the contents of the buffer will not be sent to the client by the Servlet engine until the output buffer is filled or the Servlet program has written all the response contents.

After using the output buffer, the Servlet engine can adjust the response status line, each response header and response body strictly according to the position order of the HTTP message before outputting it to the client.

If all the response content is loaded in the output buffer when the response is submitted to the client, the Servlet engine calculates the size of the response body and automatically sets the Content-Length header field.

If the output buffer is loaded with only a portion of the total response content when the response is submitted to the client, the Servlet Engine will transmit the response content using HTTP 1.1 chunked encoding (specified by setting the Transfer-Encoding header field).

Thank you for reading, the above is "response.setHeader set download file name invalid how to solve" the content, after the study of this article, I believe that we have a deeper understanding of response.setHeader set download file name invalid how to solve this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Content output buffer buffer file name engine client client text learning reason field method message status program order in strict accordance with Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Redmi NVidia macOS Apple