2012-05-15 1 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

번호를 사용하여 자바 CIFS Client library 감사드립니다. 당신은 자바를 통해 원격 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

코드를 덤프하지 말고 OP의 컨텍스트에서 설명하십시오. 타사 jar [CIFS Client library] (https://jcifs.samba.org/) –

+0

도 포함시켜주십시오! 네,하지만이 코드는 네트워크 드라이브에서 사용자 이름과 암호를 사용하여 파일을 읽는 데 필요한 컨텍스트를 쓰는 것을 잊었습니다. 타사 jar CIFS 클라이언트 라이브러리가 필요합니다. –

관련 문제