如何确定路径是否是本地文件

时间:2011-07-04 18:50:14

标签: java android

只将一个位置作为字符串,是否有可靠的方法来确定这是一个本地文件(例如/mnt/sdcard/test.jpg)还是远程资源(例如http://www.xyz.com/test.jpg)?

使用Uri.parse将其转换为Uri似乎没有给我任何指示文件位置的内容。

我真的不想在字符串中查找//!

6 个答案:

答案 0 :(得分:20)

您还可以查看android.webkit.URLUtil班级

URLUtil.isFileUrl(file)

答案 1 :(得分:7)

uri格式是

<protocol>://<server:port>/<path>

本地文件有:

file:///mnt/...

或只是

 /mnt

所以如果字符串以

开头
\w+?://

这不是file://那么这是url

答案 2 :(得分:4)

我也有同样的问题,并尝试使用Penkov Vladimir解决方案,但它没有用,因为Uri有'内容'的架构,也不是远程资源。

我使用了以下代码,效果很好。

List<Uri> urls = new ArrayList<>();
List<Uri> locals = new ArrayList<>();
for (Uri uri : uris) {
    if (uri.getScheme() != null && (uri.getScheme().equals("content") || uri.getScheme().equals("file"))) {
        locals.add(uri);
    } else {
        urls.add(uri);
    }
}

答案 3 :(得分:3)

为避免硬编码:

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Test {

  public static void main( String args[] ) throws Exception {
    final String[] inputs = {
      "/tmp/file.txt",
      "http://www.stackoverflow.com",
      "file:///~/calendar",
      "mailto:java-net@java.sun.com",
      "urn:isbn:096139210x",
      "gopher://host.com:70/path",
      "wais://host.com:210/path",
      "news:newsgroup",
      "nntp://host.com:119/newsgroup",
      "finger://user@host.com/",
      "ftp://user:password@host.com:2121/",
      "telnet://user:password@host.com",
      "//localhost/index.html"
    };


    for( final String input : inputs ) {
      System.out.println( "---------------------------------------------" );

      final String protocol = getProtocol( input );
      System.out.println( "protocol: " + protocol );

      if( "file".equalsIgnoreCase( protocol ) ) {
        System.out.println( "file    : " + input );
      }
      else {
        System.out.println( "not file: " + input );
      }
    }
  }

  /**
   * Returns the protocol for a given URI or filename.
   *
   * @param source Determine the protocol for this URI or filename.
   *
   * @return The protocol for the given source.
   */
  private static String getProtocol( final String source ) {
    assert source != null;

    String protocol = null;

    try {
      final URI uri = new URI( source );

      if( uri.isAbsolute() ) {
        protocol = uri.getScheme();
      }
      else {
        final URL url = new URL( source );
        protocol = url.getProtocol();
      }
    } catch( final Exception e ) {
      // Could be HTTP, HTTPS?
      if( source.startsWith( "//" ) ) {
        throw new IllegalArgumentException( "Relative context: " + source );
      }
      else {
        final File file = new File( source );
        protocol = getProtocol( file );
      }
    }

    return protocol;
  }

  /**
   * Returns the protocol for a given file.
   *
   * @param file Determine the protocol for this file.
   *
   * @return The protocol for the given file.
   */
  private static String getProtocol( final File file ) {
    String result;

    try {
      result = file.toURI().toURL().getProtocol();
    } catch( Exception e ) {
      result = "unknown";
    }

    return result;
  }
}

输出:

---------------------------------------------
protocol: file
file    : /tmp/file.txt
---------------------------------------------
protocol: http
not file: http://www.stackoverflow.com
---------------------------------------------
protocol: file
file    : file:///~/calendar
---------------------------------------------
protocol: mailto
not file: mailto:java-net@java.sun.com
---------------------------------------------
protocol: urn
not file: urn:isbn:096139210x
---------------------------------------------
protocol: gopher
not file: gopher://host.com:70/path
---------------------------------------------
protocol: wais
not file: wais://host.com:210/path
---------------------------------------------
protocol: news
not file: news:newsgroup
---------------------------------------------
protocol: nntp
not file: nntp://host.com:119/newsgroup
---------------------------------------------
protocol: finger
not file: finger://user@host.com/
---------------------------------------------
protocol: ftp
not file: ftp://user:password@host.com:2121/
---------------------------------------------
protocol: telnet
not file: telnet://user:password@host.com
---------------------------------------------
Exception in thread "main" java.lang.IllegalArgumentException: Relative context: //localhost/index.html
    at Test.getProtocol(Test.java:67)
    at Test.main(Test.java:30)

答案 4 :(得分:0)

基于answer by Penkov Vladimir,这是我使用的确切Java代码:

String path = "http://example.com/something.pdf";
if (path.matches("(?!file\\b)\\w+?:\\/\\/.*")) {
    // Not a local file
}

使用RegExr

查看

答案 5 :(得分:-1)

检查它是否是本地文件,您只需执行此操作:

public static boolean isLocalFile(String path) {
        return new File(path).exists();
}

要查看它是否是网址,Cameron Ketcham的答案是最好的。