URL的java.nio.file.Path吗? [英] java.nio.file.Path for URLs?

查看:182
本文介绍了URL的java.nio.file.Path吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java7附带了默认的路径本地文件的实现. URL的路径实现吗?

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);

当前,这引发java.nio.file.FileSystemNotFoundException: Provider "http" not installed.我也许可以自己实现,但我不想重蹈覆辙.

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.

推荐答案

似乎您真正想做的是完成FTP的工作-将文件从一个地方复制到另一个地方.我建议您找到使用现有FTP代码库执行此操作的更好方法.

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.

URI不是文件系统路径,因此您不能这样对待它们.它们是地址/资源定位器,当您使用浏览器(或处理它们的另一个客户端)去那里时,它们将触发位于其后的服务器所定义的某些操作.该服务器的功能没有标准,因此Web服务具有灵活性.因此,如果您的服务器正以这种方式接受HTTP请求以简化文件复制,则您将必须自己滚动,并将文件数据传递到POST请求中.

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.

换句话说,(1)不会像对待URI一样将URI视为文件系统路径-不会,(2)找到用于复制文件的FTP库,和/或(3)如果您确实想要要构建执行此操作的Web服务,请通过POST请求提取文件复制的详细信息.如果您执行第3步,则应了解您的建筑物非常接近自定义,并且该建筑物可能仅在遵循您的特定设计的一部分站点(即您自己建造的站点)上起作用.我知道没有标准的参数集或通过POST命令进行的文件复制",我知道您可以利用它来使此工作正常"-您将不得不将HTTP请求与Web服务上的Web服务进行匹配服务器端.

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.

这篇关于URL的java.nio.file.Path吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆