read remote file in Java - java

OS: macbook
I wanna read remote file in Java.
I test it on my local computer first.
I tried to use File, but you guys told me File can only be used on local file system, thanks.
public static void main(String[] args) throws URISyntaxException {
URI uri = new URI("file://127.0.0.1/Users/jian/Public/logfiles/Hadoop/hdp_log1.log");
File file = new File(uri);
System.out.println(file.exists());
}
I find a library for Macbook, Samba JCIFS, from this link access to file using Java with Samba JCIFS . Here is my code :
public static void main(String[] args) throws URISyntaxException, MalformedURLException, SmbException {
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("","jian", "ywnk");
SmbFile smbFile = new SmbFile("smb://127.0.0.1/Users/jian/Public/logfiles/Hadoop/hdp_log1.log",auth);
System.out.println(smbFile.exists());
}
Then I got this error:
Exception in thread "main" jcifs.smb.SmbAuthException: Logon failure: unknown user name or bad password.
username is the same as I run who am i in terminal, and password is my login password. I don't know why I get this username or password incorrect error.
Thanks for your guys help.

File cannot be used to read "remote files". It is meant to be used for filesystem objects which are only local to the machine, or to be more precise, accessible from the local filesystem.
It so happens that with Unix systems you can have mount points to remote filesystems, for instance an NFS mount, or a FUSE mount. But those will always be seen as "local".
If what you want is to access a remote resource to be viewed as a "file", then File can't do it.
But JSR 203 can. See here for an example.
Therefore, you first need to define what you mean by a "remote file". It entirely depends on the protocol you use. And File cannot do it. Unless the OS has sorted it for you (that is, as mentioned above, you have an NFS mount, a FUSE mount or other).
As to your error, the File constructor warns you that there is an authority component in your URI; and there is: 127.0.0.1. As File expects nothing but a path component to the URI (in addition to the URI scheme, file), this explains the error.

Your requirement can easily be fulfilled by some java FTP library.
Take a look at ftp4j, its a good library for upload and download of files and also, for directory listings.
Apache commons-net is also a good option. They have some really good sample programs too.

Related

Read/write a file with credentials [duplicate]

I have a server where I work with a database and files using a java app.
When I start my app I give a report regarding file access to the server using:
public static boolean folderExists(String folderPath) {
File folderToCheck = new File(folderPath);
return folderToCheck.exists();
}
Every time I start my app (after a fresh restart of my computer)
I get a false response, even though the server is on.
The reason is because I must give an authentication as another user.
What I do is access the server through Windows
where I am being asked for username/password,
and after that I get a true response regarding file access to the server.
Is there a way to give the authentication username/password through Java,
and not through Windows?
Thank you
On Windows 'native' Java IO (e.g. java.io.File) always inherits the security context of the user running the JVM process. For example, you could run the Java app as a Windows service with the correct credentials.
The JCIFS project implements CIFS (the Windows SMB file server protocol) and allows you to directly specify the username/password.
See the API for examples.
I am pretty sure, that there is no way to grant fileaccess by java, without a Windows-Call.
You can call cacls file.log /e /t /p Everyone:f but this will be language-dependent.
I had a similar problem: How to change the file ACL in windows, if I only know the SID?
With Java7 there may be a way to do this.

Reading remote Ubuntu directory using ssh in java?

I want to read the content of a remote directory using java.
The directory is on a machine running Ubuntu. Right clicking on the folder should give the share folder option and its installed samba client for windows sharing, but I don't have any machine running Windows.
I'm looking for a java api library to access the remote directory content?
User will only provide username, password, ip and folder name.
eg [//172.17.0.1/sharefolder/demo/]
Thanks.
For a Samba Share:
Even SAMBA shares in linux use the same protocol as windows shares.
So the post here can help: How can I mount a windows drive in Java?
Basically, you could mount the shared location as a network drive using "net use" command .
You could call this either through windows console, or through a java Process.
For a SFTP location:
If you don't have a problem with calling/using an external command you could use sshfs (either out of java or through Process) to mount the remote directory into a local folder.
See: http://numberformat.wordpress.com/2010/03/01/how-to-mount-a-remote-ssh-filesystem-using-sshfs/
If you want pure java on how to access SFTP,I read that a library called JSch can be used to access SFTP directly from java. See:
http://chrisjordan.ca/post/15052396308/java-sftp
http://jcraft.com/jsch/examples/Sftp.java.html
If it's another type please specify
You might find the The Java CIFS Client Library having the API you need - it is useful for both server and client.
Here is an example taken from their documentation to retrieve a file:
import jcifs.smb.*;
jcifs.Config.setProperty( "jcifs.netbios.wins", "192.168.1.220" );
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domain", "username", "password");
SmbFileInputStream in = new SmbFileInputStream("smb://host/c/My Documents/somefile.txt", auth);
byte[] b = new byte[8192];
int n;
while(( n = in.read( b )) > 0 ) {
System.out.write( b, 0, n );
}
For SFTP consider using JSCAPE's Secure FTP Factory. Documentation with code examples can be found here.
jsch-nio is a fully functional unix/linux java FileSystemProvider over ssh.

Reading a file on a server

I have an applet which is partly designed to (only) read in text files and make stuff based on that. in my applet, this is what I have as the "read" method which reads in files:
public void read (String file1) throws IOException
{
str.removeAllElements (); // str is a global vector
BufferedReader dia = new BufferedReader (new FileReader (file1));
for (;;)
{
strc = dia.readLine ();
if (strc == null)
break;
str.add (strc);
}
}
this works fine when I'm running it through the JVM, but when I take it online the files that I want to access are not accessible even though they are hosted on the same server and folder.
HTML for my applet looks like this:
<applet
codebase = "[the url that hosts my class and text files]"
code = "[my class file].class"
width = ###
height = ###>
</applet>
The specific error I'm getting is:
AccessControlException
access denied ("java.io.FilePermission" "dial1.txt" "read")
So if anyone could help, that would be awesome!
I suspect the real problem here is using a java.io.File. Put server/client aside for the moment and a sand-boxed applet cannot establish a File - at all. But to extend that slightly, a trusted applet can establish a File - but only one that points to files on the local drives of the client machine. A File can never point back to the server, they just don't work that way.
So that leads to. The proper way for an applet to access resources is by URL. Java uses URLs a great deal, even for accessing classes in Jars.
A sand-boxed applet can establish an URL pointing back to the server from which it was deployed.
As to how to form that URL. The URL can be formed relative to the code base (the location of the Jars/classes) or document base (the location of the HTML).
Applets run inside the web browser. Hence, on the computer of the user who downloads your applet. So even if the files you are looking for exist on the computer of the user you won't be able to read them because you don't have file system access to people surfing the internet. Read your local file on server-side. So in your case, you probably need a servlet instead of an applet.

java.io.FileNotFoundException (Access is denied)

File posisifileXML = new File("namefile.xml");
Writer outXML = new BufferedWriter(new FileWriter(posisifileXML));
outXML.write(String1);
outXML.close();
I have created Java applet in Linux, the program is used to create a namefile.xml, when I'm accessing java applet from the browser (with that Linux) to create a file, it's worked. That file is saved in my home directory. But the problem is when I'm accessing that Java applet from the browser with another computer (Windows 7), then appeared an error:
java.io.FileNotFoundException:namefile.xml (Access is denied)
oh sorry, I want to create a file from client's computer (windows 7) to the server's computer via the client's browser (using java applet)..
What should I do?
You should have posted the code :(
However, I have had the same issue because of the following two issues, when I was working with Applets
The access was restricted by windows (sometimes user privileges can do it. i.e: in my machine, it's hard to access C:/Desktop for programs)
Forgot to give the rights using Policy tool
The program is seeking for an existing file, but there is no such
File posisifileXML = new File("namefile.xml");
Never dump a File from an applet into the 'default directory'. Make that something like:
File posisifileXML = new File(
new File(System.getProperty("user.home")),
"namefile.xml");
I want to create a file from client's computer (windows 7) to the server's computer via the client's browser..
An applet on the client computer can not create or access a File on the server, security aside. File objects just do not work that way.
In order to get information from the user to be saved on the server file-system, requires help from the server.

Java - Access file with user authentication

I have a server where I work with a database and files using a java app.
When I start my app I give a report regarding file access to the server using:
public static boolean folderExists(String folderPath) {
File folderToCheck = new File(folderPath);
return folderToCheck.exists();
}
Every time I start my app (after a fresh restart of my computer)
I get a false response, even though the server is on.
The reason is because I must give an authentication as another user.
What I do is access the server through Windows
where I am being asked for username/password,
and after that I get a true response regarding file access to the server.
Is there a way to give the authentication username/password through Java,
and not through Windows?
Thank you
On Windows 'native' Java IO (e.g. java.io.File) always inherits the security context of the user running the JVM process. For example, you could run the Java app as a Windows service with the correct credentials.
The JCIFS project implements CIFS (the Windows SMB file server protocol) and allows you to directly specify the username/password.
See the API for examples.
I am pretty sure, that there is no way to grant fileaccess by java, without a Windows-Call.
You can call cacls file.log /e /t /p Everyone:f but this will be language-dependent.
I had a similar problem: How to change the file ACL in windows, if I only know the SID?
With Java7 there may be a way to do this.

Categories

Resources