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);
}
}
यह रिमोट मशीन से फ़ाइल को पढ़ने और फ़ाइल E:/Suman.txt
के रूप में हमारे स्थानीय मशीन में संग्रहीत करने के लिए एक कार्यक्रम है।
लें देखभाल, जबकि फ़ाइल पथ लेखन का मतलब :
के बजाय हम, $
प्रतीक के साथ बदलने के लिए है, उदा .: D:\Suman\Boorla\kpl.txt
गलत है, D$\\Suman\\Boorla\\kpl.txt
सही है।
उपरोक्त प्रोग्राम में, आपको दूरस्थ नाम के डोमेन नाम, उपयोगकर्ता नाम, पासवर्ड और फ़ाइल पथ को बदलना होगा। उपर्युक्त प्रोग्राम के साथ काम करने के लिए हमें कक्षा 0 में निम्नलिखित jar
फ़ाइलों को जोड़ने की आवश्यकता है।
commons-vfs.jar
commons-logging.jar
स्रोत
2013-06-21 08:28:58
निम्नलिखित उपयोगी हो सकता है: http://stackoverflow.com/q/208839/1311351 –
यही कारण है, एक अच्छा स्रोत है, लेकिन कोड को लिनक्स मशीन और विंडोज – jaysun