2012-07-30 9 views
5

मैं रिमोट मशीन एक उपयोगकर्ता नाम और पासवर्ड की जरूरत है मुझे फ़ाइल का उपयोग करने की अनुमति जावाजो जरूरत यूज़रनेम और पासवर्ड

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

में एक दूरस्थ फ़ाइल पढ़ने की कोशिश कर रहा हूँ जावा में रिमोट फाइल पढ़ें।

क्या कोई तरीका है कि मैं जावा कोड के माध्यम से पैरामीटर पास कर सकता हूं और फ़ाइल पढ़ सकता हूं?

+2

निम्नलिखित उपयोगी हो सकता है: http://stackoverflow.com/q/208839/1311351 –

+0

यही कारण है, एक अच्छा स्रोत है, लेकिन कोड को लिनक्स मशीन और विंडोज – jaysun

उत्तर

1

यहां कोड है, मैंने लिखा है और यह पूरी तरह से काम कर रहा है।

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

अब आप आवश्यक संचालन करने के लिए फ़ाइल का उपयोग कर सकते हैं। कुछ की तरह ...

InputStream is=new FileInputStream(f); 
+0

क्या आप स्थानीय प्रतिलिपि से पढ़ने से पहले स्थानीय रूप से फ़ाइल की प्रतिलिपि बना रहे हैं? यह बहुत अक्षम है ... – Matthieu

+0

मैं इस कक्षापथ को कैसे जोड़ सकता हूं ?? –

2

आप Commons VSF भी आजमा सकते हैं। UserAuthenticator

+0

में काम करने की ज़रूरत है, धन्यवाद, यह एक आकर्षण की तरह काम करता है, मैंने कोड को उत्तर के रूप में पोस्ट किया है। धन्यवाद फिर से – jaysun

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

} 
} 

यह रिमोट मशीन से फ़ाइल को पढ़ने और फ़ाइल E:/Suman.txt के रूप में हमारे स्थानीय मशीन में संग्रहीत करने के लिए एक कार्यक्रम है।

लें देखभाल, जबकि फ़ाइल पथ लेखन का मतलब : के बजाय हम, $ प्रतीक के साथ बदलने के लिए है, उदा .: D:\Suman\Boorla\kpl.txt गलत है, D$\\Suman\\Boorla\\kpl.txt सही है।

उपरोक्त प्रोग्राम में, आपको दूरस्थ नाम के डोमेन नाम, उपयोगकर्ता नाम, पासवर्ड और फ़ाइल पथ को बदलना होगा। उपर्युक्त प्रोग्राम के साथ काम करने के लिए हमें कक्षा 0 में निम्नलिखित jar फ़ाइलों को जोड़ने की आवश्यकता है।

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

मैं इस कक्षापथ को कैसे जोड़ सकता हूं ?? –

4

jCIFS साथ एक अन्य विकल्प आप आसानी से निर्दिष्ट कर सकते हैं प्रमाणीकरण पैरामीटर:

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 
} 

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^