14

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 FileOutputStream?

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

Thank you.

smn.tino
  • 2,272
  • 4
  • 32
  • 41
Anonymoose
  • 2,389
  • 6
  • 36
  • 69

2 Answers2

19

No. Use java CIFS Client library. you can connect remote windows machine through java. example -

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

Thanks

EDIT: JCIFS only supports the unsecure SMB1 protocol and has been in maintainance mode for some years. Use jcifs-ng for SMB2/SMB3 support which is required from Windows 10.

Anonymoose
  • 2,389
  • 6
  • 36
  • 69
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0

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

  }
Kenneth Clark
  • 1,725
  • 2
  • 14
  • 26
Dinanath Parit
  • 165
  • 3
  • 4
  • 2
    Please do not just dump code , explain in context for the OP .. You have also included a third party jar [CIFS Client library](https://jcifs.samba.org/) – Kenneth Clark Jun 16 '15 at 10:15
  • Sorry! Yeah i forgot to write the context however this code for reading any files from network drive using username and password and it's require third party jar CIFS Client library – Dinanath Parit Jun 17 '15 at 12:16