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
następujące mogą być pomocne: http://stackoverflow.com/q/208839/1311351 –
To dobre źródło, ale kod musi działać w maszynie linuxowej i windows – jaysun