Reputation: 387
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.
Upvotes: 1
Views: 3630
Reputation: 387
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
andActivityCompat.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.
Upvotes: 1