1

I need to access files on a local sever, and obtain their path for use in a File object.

  • I don't think I need to use HttpURLConnection for this purpose, do I?

  • Also, File file = new File(IPAddress) doesn't work.
    Where IPAddress looks something like smb://192.168.1.xxx.

  • Will file:///192.168.1.xxx work for IPAddress?

I'm not able to find a solution on the internet where both accessing a local server and listing its files is achieved.

So, how do I get file paths from a local server for performing File operations?



Edit 1:

By local server I mean a computer on my network on which I have a shared folder.

And I'm building an app that can access that folder and contents in it and do something with them.

I am facing problems fetching file paths to that shared folder content.

Dhruv
  • 367
  • 6
  • 16
  • 1
    `file://` is for local filesystem. You'll likely need an `ftp` or `http` server to host the files. – OneCricketeer Nov 19 '16 at 18:27
  • Please explain, in detail, what you mean by "a local server". – CommonsWare Nov 19 '16 at 18:28
  • @CommonsWare By local server I mean a computer on my network on which I have a shared folder. – Dhruv Nov 19 '16 at 18:35
  • 2
    Related: http://stackoverflow.com/questions/9389366/how-to-access-share-folder-in-windows-through-android-and-read-files – OneCricketeer Nov 19 '16 at 18:37
  • That is not a very detailed explanation. How exactly is this "folder" "shared"? For example, is the "local server" running Windows, where the "shared folder" is being served via CIFS? – CommonsWare Nov 19 '16 at 18:39
  • @CommonsWare On Microsoft Windows (haven't tried on other OS) you can configure any folder or a partition to be accessible via any other device, on the same network as that of your original Windows machine, via the IP address of that machine. You can setup any folder on Windows to be accessible via your Android device and that folder may contain any file of any format. – Dhruv Nov 19 '16 at 18:48
  • 1
    That is handled through a server, on the Windows machine, that serves files using a network protocol called [CIFS](https://en.wikipedia.org/wiki/Server_Message_Block). You need a CIFS client library, such as jCIFS, to talk to a CIFS server, just as you need to use an HTTP client API to talk to an HTTP server. [The question that cricket_007 linked to](https://en.wikipedia.org/wiki/Server_Message_Block) covers this. – CommonsWare Nov 19 '16 at 19:00
  • Can I establish an SMB connection without authentication? – Dhruv Nov 21 '16 at 12:06

1 Answers1

1

Thanks everyone who helped...

My answer might help someone.

NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", username, password);
// username and password which you use for logging into your Windows PC
SmbFile network;

try {
        network = new SmbFile("smb://servername or IPAddress", auth);
        for (SmbFile node : network.listFiles()) {

            // network path is now contained in 'node'
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (SmbException e) {
        e.printStackTrace();
    }

You'll have to add
<uses-permission android:name="android.permission.INTERNET" />
in your AndroidManifest.xml before the <application> tag

and

ActivityCompat.requestPermissions(this /*context*/, new String[]{Manifest.permission.INTERNET}, MY_PERMISSIONS); in your Activity.java for getting access to Internet on API target 23 (Android 6.0) and above.

Download and include in your project jcifs-x.x.xx.jar for SmbFile and NtlmPasswordAuthentication Class, from here.

Dhruv
  • 367
  • 6
  • 16