简体   繁体   English

下载文件时文件名中的“,”

[英]“,” in file name when downloading file

I am downloading a file from a controller. 我正在从控制器下载文件。 It does not work. 这是行不通的。 It is because of the "," in the filename. 这是因为文件名中的“,”。 If I remove the "," from the name it will work. 如果我从名称中删除“,”,它将起作用。 Is there any work around for this. 有没有解决的办法。 Should I do String.replace("," ""). 我应该做String.replace(“,”“”)。 There must be a better way? 一定会有更好的办法?

response.setHeader("Content-Disposition", "inline; filename=" + "abbbbababbaababba.4.1.2013,aakdfhfdf.xlsx");

Complete method 完整的方法

@RequestMapping(value = "/newsimage/{newsImageId} ", method = RequestMethod.GET)
public void getImage(HttpServletRequest request, HttpServletResponse response, @PathVariable long newsImageId) {

    NewsImage newsImage = newsImageService.findById(newsImageId);
    String fileName = newsImage.getFileName();
        response.setHeader("Content-Disposition", "inline; filename=" + "abbbbababbaababba.4.1.2013,aakdfhfdf.xlsx");
    response.setContentType(newsImage.getContentType());
    // response.setHeader("Content-Disposition", "attachment;filename=" + newsImage.getFileName());

    OutputStream out;
    try {
        out = response.getOutputStream();
        out.write(newsImage.getData());
        out.flush();
    } catch (IOException e) {
        logger.error(e.getMessage());
    }

}

You need to quote the filename, otherwise it's interpreted as part of header attribute. 您需要引用文件名,否则将其解释为标题属性的一部分。

response.setHeader("Content-Disposition", 
    "inline; filename=\"" + "abbbbababbaababba.4.1.2013,aakdfhfdf.xlsx" + "\"");

There are reserved characters and words but ',' is not one of them. 保留的字符和单词,但“,”不是其中之一。 Still I'd refrain from using ',' (replace with '-'). 我仍然避免使用','(用'-'代替)。 Also, '.' 还有,“。” shouldn't be used for anything other than the suffix. 除后缀外,不应将其用于其他任何用途。

If you really really want to use ',' you should quote the entire file name, not because of operating system (or file system) restrictions but because of HTTP headers. 如果您确实要使用',则应引用整个文件名,这并不是因为操作系统(或文件系统)的限制,而是因为HTTP标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM