Download the file with spring mvc ResponseEntity < byte [] >. The contents of the file are garbled.

backend code:

clipboard.png

,reponseheader,ResponseEntiry;

:

clipboard.png
;

,

clipboard.png

a.txt,:
:aaaa
:"YWFhYQ=="

springrequestMappingHandler:

clipboard.png

:

clipboard.png

ask for advice, how to solve it? do you want to change the configuration of handlermapping? I have tried to use ByteArrayHttpMessageConverter to swipe data to the browser. It still seems to be garbled

.
Mar.19,2021

has been solved. First, add application/octet-stream support stream format download to the support types of byteArrayHttpMessageConverter. Secondly, add @ RequestMapping (value= "/ downLoadTemplate", produces= "application/octet-stream;charset=UTF-8")
to the method. The main purpose is to let spring know the output format of the method, so that spring knows which messageConvert object to output content. The writeWithMessageConverters source code of abstractMessageConverterMethodProcessor involved is as follows:

clipboard.png

Menu