zoukankan      html  css  js  c++  java
  • java 获取某个URL的文件扩展名的方法(非精确,精确的扩展名应该使用服务器返回的MIME-TYPE)

        public static String getFileExtension(URL extUrl) {
            //URL: "http://photosaaaaa.net/photos-ak-snc1/v315/224/13/659629384/s659629384_752969_4472.jpg"
           // String filename = "";
            //PATH: /photos-ak-snc1/v315/224/13/659629384/s659629384_752969_4472.jpg
    
            String extension="";
            String path = extUrl.getPath();
            //Checks for both forward and/or backslash
            //NOTE:**While backslashes are not supported in URL's
            //most browsers will autoreplace them with forward slashes
            //So technically if you're parsing an html page you could run into
            //a backslash , so i'm accounting for them here;
            String[] pathContents = path.split("[\\/]");
            if(pathContents != null){
                int pathContentsLength = pathContents.length;
                //System.out.println("Path Contents Length: " + pathContentsLength);
    //            for (int i = 0; i < pathContents.length; i++) {
    //                System.out.println("Path " + i + ": " + pathContents[i]);
    //            }
                //lastPart: s659629384_752969_4472.jpg
                String lastPart = pathContents[pathContentsLength-1];
                String[] lastPartContents = lastPart.split("\.");
                if(lastPartContents != null && lastPartContents.length > 1){
                    int lastPartContentLength = lastPartContents.length;
                   // System.out.println("Last Part Length: " + lastPartContentLength);
                    //filenames can contain . , so we assume everything before
                    //the last . is the name, everything after the last . is the
                    //extension
                    String name = "";
                    for (int i = 0; i < lastPartContentLength; i++) {
                       // System.out.println("Last Part " + i + ": "+ lastPartContents[i]);
                        if(i < (lastPartContents.length -1)){
                            name += lastPartContents[i] ;
                            if(i < (lastPartContentLength -2)){
                                name += ".";
                            }
                        }
                    }
                    extension = lastPartContents[lastPartContentLength -1];
                   // filename = name + "." +extension;
                   // System.out.println("Name: " + name);
                   // System.out.println("Extension: " + extension);
                   // System.out.println("Filename: " + filename);
                }
            }
            return extension;
        }
  • 相关阅读:
    【响应式Web设计实践 #BOOK#】
    【JS】(+﹏+)~
    -_-#【邮件】qq邮箱不显示图片
    -_-#【Markdown】
    51Nod——N1284 2 3 5 7的倍数
    51Nod——N1118 机器人走方格
    洛谷——P1014 Cantor表
    洛谷—— P1434 滑雪
    洛谷——P1443 马的遍历
    python(24)- 面向对象进阶
  • 原文地址:https://www.cnblogs.com/sixiweb/p/6065623.html
Copyright © 2011-2022 走看看