【问题标题】:How to copy file from local to share with JCifs?如何从本地复制文件以与 JCifs 共享?
【发布时间】:2014-05-21 00:34:50
【问题描述】:

我可以将文件从共享复制到本地。但我想切换,并将文件从本地复制到共享。

I am trying this code:
SmbFile oldFile = new SmbFile("c:/tmp/test.xml");
SmbFile newFile = new SmbFile("smb://someone_pc/tmp/test.xml", new NtlmPasswordAuthentication("", username, password));
oldFile.copyTo(newFile);

但我在copyTo 方法上遇到了异常:

Invalid operation for workgroups or servers

我应该如何将文件从本地复制到共享?

【问题讨论】:

    标签: java samba jcifs


    【解决方案1】:

    前段时间我使用 jcifs。 你可以试试newFile.createNewFile();,然后使用copyTo。 如果这不起作用,请尝试 newFile.getOutputStream() 并将数据写入此流,而不是使用 copyTo

    【讨论】:

      【解决方案2】:

      我参加聚会有点晚了,但这可能对其他提出这个问题的人有用。

      我从本地上传文件以使用流共享,它可以正常工作。我的代码是:

      SmbFile remoteFile =  new SmbFile(remotePath, auth);
      SmbFileOutputStream out = new SmbFileOutputStream(remoteFile);
      FileInputStream fis = new FileInputStream(localFile);
      out.write(IOUtils.toByteArray(fis));
      out.close();
      

      【讨论】:

      • 您正在将整个文件加载到内存中。如果无法做到这一点,会发生什么?
      【解决方案3】:

      使用Javi_Swift 等流通过 SMB 协议将文件从本地复制到共享磁盘的完整解决方案(部分写入 - 无法将整个文件加载到内存中的大文件的解决方案):

      // local source file and target smb file
      File fileSource = new File("C:/example.jpg");
      SmbFile smbFileTarget = new SmbFile(smbFileContext, "example.jpg");
      // input and output stream
      FileInputStream fis = new FileInputStream(fileSource);
      SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFileTarget);
      // writing data
      try {
          // 16 kb
          final byte[] b  = new byte[16*1024];
          int read = 0;
          while ((read=fis.read(b, 0, b.length)) > 0) {
              smbfos.write(b, 0, read);
          }
      }
      finally {
          fis.close();
          smbfos.close();
      }
      

      【讨论】:

      • jcifs.smb.SmbException:访问被拒绝。在 jcifs.smb.SmbTransport.checkStatus(SmbTransport.java:542) 在 jcifs.smb.SmbTransport.send(SmbTransport.java:644) 在 jcifs.smb.smb.send(SmbSession.java:242) 在 jcifs.smb。 SmbTree.send(SmbTree.java:111) at jcifs.smb.SmbFile.send(SmbFile.java:729) at jcifs.smb.SmbFile.open0(SmbFile.java:934) 你能解决这个问题吗?
      • @Michal 是的,我没有这个问题(访问被拒绝)。看来您没有 Smb 目录的权限。你能检查你的 NtlmPasswordAuthentication(jcifs 1.x 版)或 CIFSContext(jcifs 2.x 版)吗?
      【解决方案4】:

      使用 Java 1.7 的 Files 类的示例:

      Path source = Paths.get("c:/tmp/test.xml");
      SmbFile newFile = new SmbFile("smb://someone_pc/tmp/test.xml", new NtlmPasswordAuthentication("", username, password));
      try (OutputStream out = newFile.getOutputStream())
      {
          Files.copy(source, out);
      }
      

      【讨论】:

        【解决方案5】:
        public void uploadToSmb(String destinationPath,File localFile){
            public final static byte[] BUFFER = new byte[10 * 8024];
                ByteArrayInputStream inputStream = null;
                SmbFileOutputStream sfos = null;
                try {
                    String user = username + ":" + password;
                    int lenghtOfFile = (int) localFile.length();
                    byte[] data = FileUtils.readFileToByteArray(localFile);
                    inputStream = new ByteArrayInputStream(data);
                    String path = destinationPath + localFile.getName();
                    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
                    remoteFile = new SmbFile(path, auth);
                    sfos = new SmbFileOutputStream(remoteFile);
                    long total = 0;
                    while ((count = inputStream.read(BUFFER)) > 0) {
                        total += count;
                        // publishing the progress....
                        // After this onProgressUpdate will be called
                        int percentage = (int) ((total / (float) lenghtOfFile) * 100);
                        publishProgress(percentage);
                        //  publishProgress((int) ((total * 100) / lenghtOfFile));
                        // writing data to file
                            sfos.write(BUFFER,0,count);
        
                    }
                        sfos.flush();
                        inputStream.close();
                        sfos.close();
        
                } catch (Exception e) {
                    e.printStackTrace();
                } 
              }  
        

        此代码运行良好...

        【讨论】:

          猜你喜欢
          • 2012-11-01
          • 2013-01-22
          • 1970-01-01
          • 1970-01-01
          • 2013-07-14
          • 2015-06-01
          • 2020-11-13
          • 1970-01-01
          • 2014-05-08
          相关资源
          最近更新 更多