Solve the problem that the file name contains a comma when downloading in Google Chrome, causing the page to display that the page is unavailable

Project scenario:

When using the file service project developed by myself, the test feedback is that when downloading a file, the download fails when the file name contains a comma, and the download link page cannot be redirected.
The project is developed using springboot, and the file upload is based on the SpringMVC form file upload. However, when downloading, because the original file name needs to be downloaded, and the Chinese name of the attachment needs to be queried from the database and then assigned to the response header, the file is returned in a streaming manner.


Problem Description

The file name is as follows, I have to complain about it is really a fresh and elegant file name (tmd)

 副本AAB,AIGJ,AIPG,BCAP,BESP,BHL,BKG,BOA,CPM--CPM.xlsx (21.08K) 

Error message:

下载文件报错“:org.apache.catalina.connector.ClientAbortException: java.io.IOException: 远程主机强迫关闭了一个现有的连接。

When jumping to the download link, the page responds as follows
insert image description here


Cause Analysis:

After testing the Chinese comma, it can be downloaded normally. 英文逗号The problem is very clear. When the attachment is downloaded , it is interrupted by the browser (this time using chrome) because the file name contains
insert image description here


solution:

Set the response header to add quotes

It is found that it is a bug of Google Chrome, so when we set the response header, add quotation marks to the filename. The
actual effect of setting the response header Content-Disposition is as follows:content-disposition=attachment;fileName="Original file name"

 response.setHeader("Content-Disposition", "attachment;filename=\"" + FileUtil.encodeChineseDownloadFileName(request, realFileName) + "\"");

code show as below:
insert image description here

The solution to downloading garbled characters in the file name

FileUtil.encodeChineseDownloadFileName(request, realFileName)

   /**
     * 浏览器下载时文件名乱码问题解决
     *
     * @param request
     * @param pFileName 文件名
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String encodeChineseDownloadFileName(HttpServletRequest request, String pFileName) throws UnsupportedEncodingException {
    
    
        String[] IE_BROWSER_SIGNALS = {
    
    "MSIE", "Trident", "Edge"};
        String FIREFOX = "Firefox";

        boolean isMSIE = false;
        String userAgent = request.getHeader("User-Agent");
        if (StringUtils.isBlank(userAgent)) {
    
    
            userAgent = request.getHeader("USER-AGENT");
        }
        for (String signal : IE_BROWSER_SIGNALS) {
    
    
            if (StringUtils.isNotBlank(userAgent) && userAgent.contains(signal)) {
    
    
                isMSIE = true;
            }
        }

        //返回的文件名
        String filename;
        if (isMSIE) {
    
    
            //IE浏览器的乱码问题解决
            filename = URLEncoder.encode(pFileName, "UTF-8");
            filename = StringUtils.replace(filename, "+", "%20");
        } else if (StringUtils.contains(userAgent, FIREFOX)) {
    
    
            filename = "=?UTF-8?B?" + (new String(org.apache.commons.codec.binary.Base64.encodeBase64(pFileName.getBytes("UTF-8")))) + "?=";
        } else {
    
    
            //万能乱码问题解决
            filename = new String(pFileName.getBytes("UTF-8"), "ISO-8859-1");
        }

        return filename;
    }

Guess you like

Origin blog.csdn.net/qq_29864051/article/details/131034538