Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read remote file in java which needs username and password

I am trying to read a remote file in java

File f = new File("//192.168.1.120/home/hustler/file.txt");

The remote machine needs a Username and Password to allow me to access the file.

Is there a way I could pass the parameters through the java code and read the file?

like image 355
jaysun Avatar asked Jul 30 '12 15:07

jaysun


People also ask

How do I access remote files in Java?

You have access to remote File , InputStream and OutputStream through the client. It extends java. io. File for seamless use in API using File to access its metadata (i.e. length() , lastModified() , ...).

How do I access a shared drive in Java?

The jCIFS library can be used to access Windows Shared directories. Its API mimics the File and File[In|Out]putStream classes in java.io, so you can pick it up very quickly. No change is required in Java code. In your example you access a normal file system file "\\common\\myfile.


2 Answers

package com.eiq;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemOptions;
import org.apache.commons.vfs.Selectors;
import org.apache.commons.vfs.UserAuthenticator;
import org.apache.commons.vfs.VFS;
import org.apache.commons.vfs.auth.StaticUserAuthenticator;
import org.apache.commons.vfs.impl.DefaultFileSystemConfigBuilder;

public class RemoteFileDemo {
    public static void main(String[] args) throws IOException {

        String domain = "hyd\\all";
        String userName = "chiranjeevir";
        String password = "Acvsl@jun2013";
        String remoteFilePath = "\\\\10.0.15.74\\D$\\Suman\\host.txt";


        File f = new File("E:/Suman.txt"); //Takes the default path, else, you can specify the required path
        if (f.exists()) {
            f.delete();
        }
        f.createNewFile();
        FileObject destn = VFS.getManager().resolveFile(f.getAbsolutePath());

        //domain, username, password
        UserAuthenticator auth = new StaticUserAuthenticator(domain, userName, password);
        FileSystemOptions opts = new FileSystemOptions();
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);


        FileObject fo = VFS.getManager().resolveFile(remoteFilePath, opts);

        System.out.println(fo.exists());

        //fo.createFile();

        destn.copyFrom(fo, Selectors.SELECT_SELF);
        destn.close();

        //InputStream is = new FileInputStream(f);

    }
}

This is a program to read a file from the remote machine and store it in our local machine as file E:/Suman.txt.

Take care while writing the file path means instead of : we have to replace it with $ symbol, e.g.: D:\Suman\Boorla\kpl.txt is wrong, D$\\Suman\\Boorla\\kpl.txt is right.

In the above program, you have to change the domain name, username, password and file path of the remote machine. To work with the above program we need to add the following jar files int the classpath.

commons-vfs.jar
commons-logging.jar
like image 175
Suman Boorla Avatar answered Oct 22 '22 23:10

Suman Boorla


Another alternative with jCIFS you can easily specify authentication parameters:

NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domain", "user", "password"); // Authentication info here, domain can be null
try (InputStream is = new SmbFile("smb://192.168.1.120/home/hustler/file.txt", auth).getInputStream()) {
    // Read from 'is' ...
} catch (IOException e) {
    // Handle IOException
}
like image 5
Matthieu Avatar answered Oct 23 '22 01:10

Matthieu