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;
        }
  • 相关阅读:
    编译预处理命令define
    共享数据的包含const
    友元类,友元函数
    静态成员static
    this 指针
    构造函数与析构函数
    c++类的基础
    void指针和const指针
    c++基础
    组播的实现
  • 原文地址:https://www.cnblogs.com/sixiweb/p/6065623.html
Copyright © 2011-2022 走看看