Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.nio.file.Path for URLs?

Tags:

java

path

nio

Java7 ships with a default Path implementation for local files. Is there a Path implementation for URLs?

For example, I should be able to copy a remote resource using the following code:

Path remote = Paths.get(new URI("http://www.example.com/foo/bar.html")); Path local = Paths.get(new URI("/bar.html")); Files.copy(remote, local); 

Currently, this throws java.nio.file.FileSystemNotFoundException: Provider "http" not installed. I could probably implement this myself but I'd rather not reinvent the wheel.

like image 947
Gili Avatar asked Jan 09 '12 03:01

Gili


People also ask

Can URL be a file path?

So yes, file URIs are URLs.

What is Java NIO file paths?

Advertisements. As name suggests Path is the particular location of an entity such as file or a directory in a file system so that one can search and access it at that particular location.

How do I give path to Classpathresource?

Map<String, Object> env = new HashMap<>(); try (FileSystem fs = FileSystems. newFileSystem(uri, env)) { Path path = fs. getPath("/path/myResource"); try (Stream<String> lines = Files. lines(path)) { .... } }

How do I find the path of a file in Java?

In Java, for NIO Path, we can use path. toAbsolutePath() to get the file path; For legacy IO File, we can use file. getAbsolutePath() to get the file path.


2 Answers

You can do:

URI uri = new URI("http://www.example.com/foo/bar.html"); try (InputStream is = uri.toURL().openStream()) {     // ... } 

It will work for http, https and file out of the box, probably for few more.

For relative URIs, you have to resolve them first:

URI relative = new URI("bar.html"); URI base = new URI("http://www.example.com/foo/"); URI absolute = base.resolve(relative); System.out.println(absolute); // prints "http://www.example.com/foo/bar.html" 

Now you can call toURL().openStream() on the absolute URI.

like image 35
Oliv Avatar answered Sep 29 '22 16:09

Oliv


It seems like what you're really trying to do is accomplish what FTP does - copy files from one place to another. I would suggest you find better ways to do this with existing FTP code libraries.

URIs are not file system paths, so you can't treat them as such. They are addresses/resource locators that, when you go there with your browser (or another client that handles them), they trigger some action as defined by the server that's behind them. There's no standard for what that server does, hence the flexibility of web services. Therefore, if your server is doing to accept HTTP requests in this manner to facilitate file copies, you're going to have to roll your own, and pass the file data into a POST request.

To say it another way, (1) don't treat URIs like they are file system paths - they aren't, (2) find an FTP library to copy files, and/or (3) if you really want to build a web service that does this, abstract the details of the file copying via a POST request. If you do #3 understand that what your building is pretty close to custom, and that it will probably only work on a subset of sites that follow your particular design (i.e. the ones you build yourself). There's no standard set of parameters or "file copying" via POST command that I'm aware of that you can leverage to make this "just work" - you're going to have to match up your HTTP request with the web service on the server side.

like image 106
jefflunt Avatar answered Sep 29 '22 18:09

jefflunt