java 下载文件 文件名称变空格的问题

 
// 设置header 下载    , 后台不返回 下载文件名,由前端指定文件名---
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        try {
            String encodeName = UriUtils.encodePath(fileName + ".xlsx", Charsets.UTF_8.toString());
            response.setHeader("Content-Disposition", "attachment;filename=" + encodeName);
        } catch (UnsupportedEncodingException e) {
            log.warn(e.getMessage(), e);
        }

 

 
参考:  https://stackoverflow.com/questions/22932060/url-encode-decode-on-file-name-replace-spaces-with-need-alternative

You could also convert a space to %20.

See: URL encoding the space character: + or %20?

There are also various other Java libraries that do URL encoding, with %20. Here are a two examples:

Guava:

UrlEscapers.urlPathSegmentEscaper().escape(urlToEscape);

Spring Framework:

UriUtils.encodePath(urlToEscape, Charsets.UTF_8.toString());

posted on 2022-04-21 17:28  正义的伙伴!  阅读(437)  评论(0编辑  收藏  举报

导航

//增加一段JS脚本,为目录生成使用