Read remote file in java that 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 access to the file.

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

+5
source share
4 answers

Here is the code I wrote and it works fine.

File f=new File("abc.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());
            UserAuthenticator auth=new StaticUserAuthenticator("", "myusername", "secret_password");
            FileSystemOptions opts=new FileSystemOptions();
            DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
            FileObject fo=VFS.getManager().resolveFile("\\\\192.168.0.1\\direcory\\to\\GetData\\sourceFile.txt",opts);
            destn.copyFrom(fo,Selectors.SELECT_SELF);
            destn.close();

Now you can use the file to perform the necessary operations. Sort of...

InputStream is=new FileInputStream(f);
+1
source
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 for reading a file from a remote computer and saving it on our local machine as a file E:/Suman.txt.

:, $, : D:\Suman\Boorla\kpl.txt , D$\\Suman\\Boorla\\kpl.txt .

, , . jar int .

commons-vfs.jar
commons-logging.jar
+7

Another jCIFS alternative makes it easy to define 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
}
+4
source

You can also try Commons VSF . Check UserAuthenticator

+2
source

All Articles