14

我想在共享网络驱动器上放置一个 .txt 文件。该路径是网络驱动器上的地图,需要凭据(登录名和密码)。我可以使用 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){
}

谢谢你。

4

2 回答 2

18

不,使用 java CIFS 客户端库。你可以通过java连接远程windows机器。例子 -

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

谢谢

编辑:JCIFS 仅支持不安全的 SMB1 协议,并且多年来一直处于维护模式。使用 jcifs-ng获得 Windows 10 所需的 SMB2/SMB3 支持。

于 2012-05-15T11:51:37.520 回答
0

这段代码对我有用:

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

  }
于 2015-06-16T09:47:52.367 回答