mr_tawan
mr_tawan

Reputation: 666

Using SMB protocol in URL while using JCIFS library on Android

I'm using JCIFS in my new Android project. Somehow I've decided to use URL class to generalized the file path (so I can add more protocol later). What I did is as below

URL url = new URL("smb://192.168.1.1/filepath");

And then java.net.MalformedURLException exception is thrown.

Exception in thread "main" java.net.MalformedURLException: unknown protocol: smb
    at java.net.URL.<init>(URL.java:184)
    at java.net.URL.<init>(URL.java:127)

Consulting JCIFS FAQ reveals that I have to register the protocol before using the class. However I don't really know how to do so in Android. I think the library do this already, but it doesn't on Android.

So what should I do ?

Upvotes: 8

Views: 11597

Answers (2)

mr_tawan
mr_tawan

Reputation: 666

I've just saw the usage in JCIFS reference, in the SmbFile reference.

When using the java.net.URL class with 'smb://' URLs it is necessary to first call the static jcifs.Config.registerSmbURLHandler(); method. This is required to register the SMB protocol handler.

So I add this call and it works properly.

Upvotes: 8

11101101b
11101101b

Reputation: 7779

Don't use a URL object. Pass the URL directly into SmbFile constructor. For example:

SmbFile file = new SmbFile("smb://192.168.1.1/filepath");

Then you can do most everything you can do with a regular File.

Upvotes: 0

Related Questions