2012-07-30 14 views
5

Próbuję odczytać plik zdalny w javaCzytaj zdalny plik w java, który wymaga nazwy użytkownika i hasła

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

zdalny wymaga nazwy użytkownika i hasła, aby umożliwić mi dostęp do pliku.

Czy istnieje sposób przekazać parametry za pośrednictwem kodu java i odczytać plik?

+2

następujące mogą być pomocne: http://stackoverflow.com/q/208839/1311351 –

+0

To dobre źródło, ale kod musi działać w maszynie linuxowej i windows – jaysun

Odpowiedz

1

Oto kod, napisałem i działa idealnie.

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(); 

Teraz możesz użyć tego pliku do wykonania wymaganych operacji. Coś jak ...

InputStream is=new FileInputStream(f); 
+0

Czy kopiujesz plik lokalnie przed odczytaniem z lokalnej kopii? To dość nieefektywne ... – Matthieu

+0

jak mogę dodać tę ścieżkę klas? –

7
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="[email protected]"; 
    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); 

} 
} 

Jest to program do odczytu pliku ze zdalnego komputera i zapisać go w naszym lokalnym komputerze jako plik E:/Suman.txt.

Zadbaj pisząc ścieżkę pliku oznacza zamiast : musimy zastąpić go $ symbol, np .: D:\Suman\Boorla\kpl.txt jest źle, D$\\Suman\\Boorla\\kpl.txt ma rację.

W powyższym programie należy zmienić nazwę domeny, nazwę użytkownika, hasło i ścieżkę do pliku zdalnego komputera. Aby pracować z powyższym programem, musimy dodać następujące pliki jar do ścieżki klas.

commons-vfs.jar 
commons-logging.jar 
+1

jak mogę dodać tę ścieżkę klas? –

4

Inną alternatywą z jCIFS można łatwo określić parametry uwierzytelniania:

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 
} 
Powiązane problemy