吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2517|回复: 4
收起左侧

[Java 原创] 【C/Java/Dart/Go】处理文件名不能包含字符\/:*?“<>|

[复制链接]
dadashuai 发表于 2021-8-26 15:42
本帖最后由 dadashuai 于 2021-8-27 08:55 编辑

C语言

//检测文件名是否合法,不包含:\/:*?“<>|
int CheckFileName(char *path) {
        int i = 0;
        for(i = strlen(path) - 1; i >= 0; i--){
                if(path[i] == '\\' || path[i] == '/' || path[i] == ':' || path[i] == '*' || path[i] == '?' || path[i] == '"' || path[i] == '<' || path[i] == '>' || path[i] == '|'){
                        return 0;
                }

        }
        return 1;
}

//去除文件名中不合法的部分 需要释放内存
char *HandleFileName(char *path) {
    size_t len = strlen(path)+1;
        char *buffer = (char *)malloc(len);
        int i = 0;
    int index = 0;
    memset(buffer, 0, len);
        for(i = 0; i < len; i++){
                if(path[i] == '\\' || path[i] == '/' || path[i] == ':' || path[i] == '*' || path[i] == '?' || path[i] == '"' || path[i] == '<' || path[i] == '>' || path[i] == '|'){

                } else {
                        buffer[index] = path[i];
            index++;
                }

        }
        return buffer;
}

java

// 检测文件名是否合法,不包含:\/:*?“<>|
        static boolean CheckFileName(String path) {
                if (path == null)
                        return false;
                for (int i = path.length() - 1; i >= 0; i--) {
                        if (path.charAt(i) == '\\' || path.charAt(i) == '/' || path.charAt(i) == ':' || path.charAt(i) == '*'
                                        || path.charAt(i) == '?' || path.charAt(i) == '\"' || path.charAt(i) == '<' || path.charAt(i) == '>'
                                        || path.charAt(i) == '|') {
                                return false;
                        }

                }
                return true;
        }

        // 去除文件名中不合法的部分
        static String HandleFileName(String path) {
                if (path == null)
                        return "";
                StringBuffer buffer = new StringBuffer();
                for (int i = 0; i < path.length(); i++) {
                        if (path.charAt(i) == '\\' || path.charAt(i) == '/' || path.charAt(i) == ':' || path.charAt(i) == '*'
                                        || path.charAt(i) == '?' || path.charAt(i) == '\"' || path.charAt(i) == '<' || path.charAt(i) == '>'
                                        || path.charAt(i) == '|') {

                        } else {
                                buffer.append(path.charAt(i));
                        }

                }
                return buffer.toString();
        }

dart

 //检测文件名是否合法,不包含:\/:*?“<>|
  static bool CheckFileName(String path){
if(path == null)return false;
    for(var i=path.length-1; i>=0; i--){
      if(path[i] == "\\" || path[i]=="/" || path[i]==":"|| path[i] == "*" || path[i]=="?"||path[i]=="\"" || path[i]=="<" || path[i]==">" || path[i]=="|"){
        return false;
      }

    }
    return true;
  }

  //去除文件名中不合法的部分
  static String HandleFileName(String path){
    if(path == null)return "";
    StringBuffer buffer = new StringBuffer();
    for(var i=0; i<path.length; i++){
      if(path[i] == "\\" || path[i]=="/" || path[i]==":"|| path[i] == "*" || path[i]=="?"||path[i]=="\"" || path[i]=="<" || path[i]==">" || path[i]=="|"){

      }else{
        buffer.write(path[i]);
      }

    }
    return buffer.toString();
  }

go


//检测文件名是否合法,不包含:\/:*?“<>|
func CheckFileName(path string) bool {
        var i = 0
        for i = len(path) - 1; i >= 0; i-- {
                if path[i] == '\\' || path[i] == '/' || path[i] == ':' || path[i] == '*' || path[i] == '?' || path[i] == '"' || path[i] == '<' || path[i] == '>' || path[i] == '|' {
                        return false
                }

        }
        return true
}

//去除文件名中不合法的部分
func HandleFileName(path string) string {
        var buffer bytes.Buffer
        var i = 0
        for i = 0; i < len(path); i++ {
                if path[i] == '\\' || path[i] == '/' || path[i] == ':' || path[i] == '*' || path[i] == '?' || path[i] == '"' || path[i] == '<' || path[i] == '>' || path[i] == '|' {

                } else {
                        buffer.WriteByte(path[i])
                }

        }
        return buffer.String()
}

JavaScript


// 检测文件名是否合法,不包含:\/:*?“<>|
 function CheckFileName( path) {
    if (path == null)
        return false;
    for (var i = path.length - 1; i >= 0; i--) {
        if (path.charAt(i) == '\\' || path.charAt(i) == '/' || path.charAt(i) == ':' || path.charAt(i) == '*'
                || path.charAt(i) == '?' || path.charAt(i) == '\"' || path.charAt(i) == '<' || path.charAt(i) == '>'
                || path.charAt(i) == '|') {
            return false;
        }
    }
    return true;
}
// 去除文件名中不合法的部分
 function HandleFileName( path) {
    if (path == null)
        return "";
    var buffer = ""
    for (var i = 0; i < path.length; i++) {
        if (path.charAt(i) == '\\' || path.charAt(i) == '/' || path.charAt(i) == ':' || path.charAt(i) == '*'
                || path.charAt(i) == '?' || path.charAt(i) == '\"' || path.charAt(i) == '<' || path.charAt(i) == '>'
                || path.charAt(i) == '|') {
        } else {
            buffer += (path.charAt(i));
        }
    }
    return buffer;
}

免费评分

参与人数 1吾爱币 +5 热心值 +1 收起 理由
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

杨辣子 发表于 2021-8-26 16:10
用正则会更方便
905407220 发表于 2021-8-26 16:45
无相孤君 发表于 2021-8-27 07:43
 楼主| dadashuai 发表于 2021-8-27 08:56

加上了。js稍微修改一下就行了

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
905407220 + 1 + 1 谢谢@Thanks!

查看全部评分

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-20 08:04

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表