2012-05-15 6 views
7

共有ネットワークドライブに.txtファイルをドロップします。パスは、資格情報(ログインとパスワード)を必要とするネットワークドライブ上のマップです。 FileOutputStreamを使用してこれらのパラメータを渡すことはできますか?資格情報を使用して共有ネットワークドライブにI/Oファイルを書き込みます。

 FileOutputStream fos; 
     DataOutputStream dos; 

     try { 
      File file= new File(path + "/" + fileName + ".txt"); 
      fos = new FileOutputStream(file); 
      dos=new DataOutputStream(fos); 
      dos.writeChars(stringContent); 
      dos.close(); 
      fos.close(); 
     } 
     catch(IOException eio){ 
     } 

あなた

答えて

12

番号を使用するJava CIFS Client libraryありがとうございます。あなたはjavaを介してリモートのWindowsマシンに接続することができます。例 -

+1

はこれで任意の経験を持っていない。今私は、カスタムライブラリを使用することを、私はそれを含める必要があります.jarファイルか何か? jarファイルはどのようにこれらのクラスで動作できますか? – Hazaart

+0

[http://jcifs.samba.org/src/](http://jcifs.samba.org/src/)からjcifs-1.1.11.jar jarをダウンロードし、このjarをビルドパスに追加します。 –

+2

ドメインを持っていればドメインを使用する必要がありますユーザ:パスワード –

0

このコードは、私のために働いた

String user = "user:password"; 
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user); 
String path = "smb://my_machine_name/D/MyDev/test.txt"; 
SmbFile sFile = new SmbFile(path, auth); 
SmbFileOutputStream sfos = new SmbFileOutputStream(sFile); 
sfos.write("Test".getBytes()); 
sfos.close(); 

ありがとう:

public void downloadFromNetworkDrive3() throws MalformedURLException, SmbException, IOException { 
     String user = "domain;username:password";//domain name which you connect 
     NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user); 
     String path = "smb://198.168.20.27/D$/MICROS/opera/export/OPERA/dinaamum/audit/Thumbs.db"; 

     SmbFile sFile = new SmbFile(path, auth); 
     SmbFileOutputStream sfos; 
     SmbFileInputStream sfis; 
     try { 
//  sfos = new SmbFileOutputStream(sFile); 
      sfis = new SmbFileInputStream(sFile); 

//  sfos.write("hihowareyou".getBytes()); 
      File tempFile = null; 
      String filePath = null; 
      filePath = "c://usr/local/cache/leelafiles"; 
      tempFile = new File(filePath); 
      if (tempFile.exists()) { 
      } else { 
       tempFile.mkdirs(); 
      } 
      tempFile = new File(filePath); 
//  File[] allFilesAndDirs = tempFile.listFiles(); 
      FileOutputStream writer = new FileOutputStream(tempFile + File.separator + "Thumbs.db"); 
      byte[] b = new byte[8192]; 
      int n; 
      while ((n = sfis.read(b)) > 0) { 
       System.out.write(b, 0, n); 
       writer.write(b, 0, n); 
      } 
      sfis.close(); 
      writer.close(); 

     } catch (UnknownHostException ex) { 
      Logger.getLogger(ReportSchedulerJob.class.getName()).log(Level.SEVERE, null, ex); 
     } 

    } 
+1

第三者jar [CIFS Client library](https://jcifs.samba.org/) –

+0

も含まれていますので、コードをダンプしてOPのコンテキストで説明してください。ええ、私はコンテキストを書くのを忘れていましたが、このコードはユーザー名とパスワードを使ってネットワークドライブからファイルを読み込むためのもので、サードパーティのjar CIFS Clientライブラリ –

関連する問題