目前我使用 Apache CommonsVFS 从其他站点获取图像并且效果很好。
CommonsVFS 的网站说它支持 HTTPS 协议,但我发现我无法使用 CommonVFS 访问那些以 https:// 开头的 URL,而我可以从我的 Firefox 正常浏览它。
例如,下面的代码会产生一个异常,说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...
我还尝试为 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 服务器呢?