There are many ways to implement the file download function on the front end. We will not introduce them one by one here. Here we only introduce the implementation method of using file stream download.
Since it is a file stream, it definitely needs to return a bunch of binary codes to the front end. As the back end, it can return aOutPutStream
The backend can use the servlet provided in Java HttpServletResponse
. The core step is to set the data type of the response, set it to a certain file type or binary format, and the response header, and then ServletOutputStream
send the file to the front end in the form of a stream.
Directly upload the code
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=example.xls");
explain
-
First set the response data type. Set the type of the file you want to download. If you don’t know what type the file is but you are sure it is not a text file, then use the following representation so that the browser knows that this is some binary data
application/octet-stream
. It will not be parsed. -
Next is to set the response header:
attachment
: Indicates that the response content should be treated as an attachment and will trigger the browser download behavior.
filename
: The parameter specifies the name of the downloaded file. The above code will cause the browser to download an example.xls file.
Here is a link for you, which contains common MIME types and a list of common MIME types , such as the picture below, png type, pdf, ppt, etc.
Then it is sent to the front end in the form of a stream.
ServletOutputStream outputStream = response.getOutputStream();
checkInfo.write(outputStream);
outputStream.flush();
In this way, the backend completes passing a file to the frontend in the form of a file stream, and a request initiated by the frontend will trigger the browser's download behavior.
Below is the complete code
@GetMapping("/output/{classId}/{checkName}")
public void checkToExcel(@PathVariable String checkName, @PathVariable String classId, HttpServletResponse response) {
HSSFWorkbook checkInfo = checkExcelService.getCheckInfo(classId, checkName);
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + checkName + "签到统计表.xls");
try {
ServletOutputStream outputStream = response.getOutputStream();
checkInfo.write(outputStream);
outputStream.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
I don’t know if any friends are like me. The return value of a method is written as a stream (for example ByteArrayOutputStream
). This is wrong.
When I wrote this program before, I returned a stream to the front end. As a result, the downloaded file was always in the wrong format, and the response data type received by the browser was always json. The reason was that the response was not used to send the stream, so it must be used to obtain the stream response.getOutputStream()
. , and then flush, so that the front end actually receives the binary file stream.