Write I/O file to shared network drive using credentials

前端 未结 2 1649
甜味超标
甜味超标 2020-11-29 08:59

I want to drop a .txt file on a shared network drive. The path is a map on a networkdrive which requires credentials (login and password). Can i pass these parameters using

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 09:19

    This code worked for me:

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

提交回复
热议问题