文件下载时,文件名为中文和含有特殊字符的综合解决方案

文件下载时,文件名为中文和含有特殊字符的综合解决方案

 

/**
     * window操作系统文件名不能含有 ? “ ”/ \ < > * | :
     * mac操作系统文件名不能以.开头
     * linux和Mac基本一直,
     * 
     * @param fileName
     * @return
     */
    public String checkFileName(String fileName) {
        Pattern pattern = Pattern.compile("[\\s\\\\/:\\*\\?\\\"<>\\|.]");
        Matcher matcher = pattern.matcher(fileName);
        fileName = matcher.replaceAll(""); // 将匹配到的非法字符以空替换
        return fileName;
    }

 

posted @ 2023-03-09 16:27  袁骞骞  阅读(169)  评论(0编辑  收藏  举报