Spring MVC elegant download and normal file name display

Use Spring MVC's ResponseEntity to pass in the bytecode of the file to achieve the download function, without HttpServletResponse responsewriting bytes to the output stream.

public class BasicController {
    
    
    protected ResponseEntity<byte[]> getFile(String fileName, byte[] dataArray) throws Exception {
    
    
        fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString());
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", "attachment; filename=\"" + fileName + "\"; filename*=utf-8''" + fileName);
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        return ResponseEntity
                .ok()
                .headers(headers)
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(dataArray);
    }

    protected ResponseEntity<byte[]> getExcelFile(String fileName, byte[] dataArray) throws Exception {
    
    
//        fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString());
        // Safari 和 Chrome 都可以正常下载包含中文文件名的 excel 文件
        fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", "attachment; filename=\"" + fileName + "\"; filename*=utf-8''" + fileName);
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        headers.add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        return ResponseEntity
                .ok()
                .headers(headers)
                .body(dataArray);
    }
}

getFileDownload Excel file method does not work in Safari browser, the download is a suffix .dmsfile. getExcelFileSpecify the MIME type of Excel and encode the file name to be ISO_8859_1compatible with Chrome and Safari downloads.

@RestController
public class FileProcessController extends BasicController {
    
    
    @GetMapping("/test/download")
    public ResponseEntity<byte[]> downloadFile(d) throws Exception {
    
    
    	 // 得到文件的字节数组 
    	 byte[] dataArray = getData(); 
        return this.getExcelFile("excel-文件下载.xlsx", dataArray);
    }
}

Simulate it in the browser:

Spring MVC 文件下载测试: <a href="http://localhost:8080/test/download">点击下载</a>

Insert picture description here
When I was testing in the project, because there was no cookie, the request backend did not have permission.
In the Console of Chrome or Safari developer tools, use:

document.cookie="COOKIE_KEY=COOKIE_VALUE";

Set the request cookie.
Insert picture description here
Reference: Solve the problem that the download file names of major browsers are not displayed correctly.
Java realizes the browser to download files, and solves the garbled and suffix problems of compatible browsers.
Spring MVC file download and Chinese file name garbled solution

Guess you like

Origin blog.csdn.net/jiaobuchong/article/details/84641668
Recommended