Apache CommonsVFS 连接到 https 服务器

发布于 2024-09-24 10:32:52 字数 3097 浏览 0 评论 0原文

目前我使用 Apache CommonsVFS 从其他站点获取图像,效果很好。

CommonsVFS 的网站说支持 HTTPS 协议,但是我发现使用 CommonVFS 无法访问 https:// 开头的 URL,而可以 正常地从我的 Firefox 浏览它

例如,以下代码将产生异常,显示无法连接到 HTTP 服务器。

scala> import org.apache.commons.vfs.VFS                                                  
import org.apache.commons.vfs.VFS

scala> val fsManager = VFS.getManager();
fsManager: org.apache.commons.vfs.FileSystemManager = org.apache.commons.vfs.impl.StandardFileSystemManager@16a0733

scala> val file1 = fsManager.resolveFile ("http://bone.twbbs.org.tw/Images/desk.jpg")
file1: org.apache.commons.vfs.FileObject = http://bone.twbbs.org.tw/Images/desk.jpg

scala> val file2 = fsManager.resolveFile ("https://na.archives.gov.tw/archives/chinese/98/search/popup.jsp?res=/export/home/xdcm/do/ScaleDoFiles/jpg/00/00/01/21/07/A313480000K=0089=577=1=1=003=0002-i.jpg")
org.apache.commons.vfs.FileSystemException: Could not connect to HTTP server on "na.archives.gov.tw".
    at org.apache.commons.vfs.provider.http.HttpClientFactory.createConnection(HttpClientFactory.java:105)
    at org.apache.commons.vfs.provider.http.HttpFileProvider.doCreateFileSystem(HttpFileProvider.java:81)
    at org.apache.commons.vfs.provider.AbstractOriginatingFileProvider.findFile(AbstractOriginatingFileProvider.java:81)
    at org.apache.commons.vfs.provider.AbstractOriginatingFileProvider.findFile(AbstractOriginatingFileProvider.java:62)
    at org.apache.commons.vfs.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:641)
    at org.apache.commons.vfs.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:602)
    at org.apache.commons.vfs.impl.DefaultFileS...

我还尝试为 FileManager 添加 HTTPS 提供程序,但仍然没有成功,它显示 URL 方案“https”已注册。

scala> fsManager.asInstanceOf[org.apache.commons.vfs.impl.StandardFileSystemManager].addProvider("https", new org.apache.commons.vfs.provider.https.HttpsFileProvider)
org.apache.commons.vfs.FileSystemException: Multiple providers registered for URL scheme "https".
    at org.apache.commons.vfs.impl.DefaultFileSystemManager.addProvider(DefaultFileSystemManager.java:174)
    at org.apache.commons.vfs.impl.DefaultFileSystemManager.addProvider(DefaultFileSystemManager.java:152)
    at .<init>(<console>:8)
    at .<clinit>(<console>)
    at RequestResult$.<init>(<console>:9)
    at RequestResult$.<clinit>(<console>)
    at RequestResult$scala_repl_result(<console>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at scala.tools.nsc.Interpr...

那么,如何使用commonVFS访问https服务器呢?

Currently I use Apache CommonsVFS to fetch images from other sites and it works well.

The website of CommonsVFS says it supports HTTPS protocol, but I found I could not access those URL starts with https:// using CommonVFS, while I could browse it from my Firefox normally.

For example, the following code will yield an exception says could not connect to HTTP server..

scala> import org.apache.commons.vfs.VFS                                                  
import org.apache.commons.vfs.VFS

scala> val fsManager = VFS.getManager();
fsManager: org.apache.commons.vfs.FileSystemManager = org.apache.commons.vfs.impl.StandardFileSystemManager@16a0733

scala> val file1 = fsManager.resolveFile ("http://bone.twbbs.org.tw/Images/desk.jpg")
file1: org.apache.commons.vfs.FileObject = http://bone.twbbs.org.tw/Images/desk.jpg

scala> val file2 = fsManager.resolveFile ("https://na.archives.gov.tw/archives/chinese/98/search/popup.jsp?res=/export/home/xdcm/do/ScaleDoFiles/jpg/00/00/01/21/07/A313480000K=0089=577=1=1=003=0002-i.jpg")
org.apache.commons.vfs.FileSystemException: Could not connect to HTTP server on "na.archives.gov.tw".
    at org.apache.commons.vfs.provider.http.HttpClientFactory.createConnection(HttpClientFactory.java:105)
    at org.apache.commons.vfs.provider.http.HttpFileProvider.doCreateFileSystem(HttpFileProvider.java:81)
    at org.apache.commons.vfs.provider.AbstractOriginatingFileProvider.findFile(AbstractOriginatingFileProvider.java:81)
    at org.apache.commons.vfs.provider.AbstractOriginatingFileProvider.findFile(AbstractOriginatingFileProvider.java:62)
    at org.apache.commons.vfs.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:641)
    at org.apache.commons.vfs.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:602)
    at org.apache.commons.vfs.impl.DefaultFileS...

I also tried to add HTTPS provider for FileManager, but still has no luck, it says URL scheme "https" is already registered.

scala> fsManager.asInstanceOf[org.apache.commons.vfs.impl.StandardFileSystemManager].addProvider("https", new org.apache.commons.vfs.provider.https.HttpsFileProvider)
org.apache.commons.vfs.FileSystemException: Multiple providers registered for URL scheme "https".
    at org.apache.commons.vfs.impl.DefaultFileSystemManager.addProvider(DefaultFileSystemManager.java:174)
    at org.apache.commons.vfs.impl.DefaultFileSystemManager.addProvider(DefaultFileSystemManager.java:152)
    at .<init>(<console>:8)
    at .<clinit>(<console>)
    at RequestResult$.<init>(<console>:9)
    at RequestResult$.<clinit>(<console>)
    at RequestResult$scala_repl_result(<console>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at scala.tools.nsc.Interpr...

So, how could I use commonVFS to access https server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫性小仙女 2024-10-01 10:32:52

好的,我发现它抛出此异常,因为我尝试连接的站点的证书在我的 Java 密钥库中不受信任。

我按照此处的说明将证书添加到我的密钥库中,现在一切正常。

OK, I found out that it throws this exception because the certificate of the site which I try to connected is not trusted in my Java keystore.

I followed the instruction here to add the certificate to my keystore, and everything works fine now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文