Apache CommonsVFS connect to https server
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_resul开发者_JS百科t(<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?
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.
精彩评论