【问题标题】:Pushing multiple files to smb share jcifs from local folder将多个文件从本地文件夹推送到 smb 共享 jcifs
【发布时间】:2021-08-10 16:58:23
【问题描述】:

我们如何使用 java 将多个文件从本地文件夹推送到 smb 共享文件夹。我可以使用 smbFile 处理我的单个文件,它正在工作。我正在寻找将多个文件推送到 smb 共享。 任何参考链接和示例代码都会有所帮助。 谢谢。

编辑,代码参考:

SmbFile[] files = getSMBListOfFiles(sb, logger, domain, userName, password, sourcePath, sourcePattern);

if (files == null)
    return false;
output(sb, logger, "      Source file count: " + files.length);
String destFilename;
FileOutputStream fileOutputStream;
InputStream fileInputStream;
byte[] buf;
int len;
for (SmbFile smbFile: files) {
    destFilename = destinationPath + smbFile.getName();
    output(sb, logger, "         copying " + smbFile.getName());
    try {
        fileOutputStream = new FileOutputStream(destFilename);
        fileInputStream = smbFile.getInputStream();
        buf = new byte[16 * 1024 * 1024];
        while ((len = fileInputStream.read(buf)) > 0) {
            fileOutputStream.write(buf, 0, len);
        }
        fileInputStream.close();
        fileOutputStream.close();
    } catch (SmbException e) {
        OutputHandler.output(sb, logger, "Exception during copyNetworkFilesToLocal stream to output, SMP issue: " + e.getMessage(), e);
        e.printStackTrace();
        return false;
    } 

如果我尝试发送任何格式的单个文件,这会很好。但是如果想从本地文件夹发送多个文件到 smb 共享。为此,我需要帮助。谢谢。

【问题讨论】:

    标签: java rest rest-assured smb jcifs


    【解决方案1】:

    尝试声明一个SmbFile 对象,它是您要上传到共享文件夹的文件夹的根文件夹。然后遍历root.listFiles() 数组。 将可上传的文件放在该文件夹中。

    我假设,如果您的 SmbFile[] files 数组仅包含一个文件,则它只上传一个文件。

    或者另一种可能的解决方案是,尝试使用SmbFileOutputStreamSmbFileInputStream 而不是FileOutputStreamFileInputStream

    【讨论】:

      【解决方案2】:

      我猜你正在使用 jcifs-library(它已经过时了),所以首先我建议你切换库。我切换到 SMBJ,这是我上传文件的方式:

      private static void upload(File source, DiskShare diskShare, String destPath, boolean overwrite) throws IOException {
              try (InputStream is = new java.io.FileInputStream(source)) {
                  if (destPath != null && is != null) {
      
                      // https://docs.microsoft.com/en-us/windows/win32/fileio/creating-and-opening-files
                      Set<AccessMask> accessMask = new HashSet<>(EnumSet.of(
                              AccessMask.FILE_READ_DATA,
                              AccessMask.FILE_WRITE_DATA, AccessMask.DELETE));
                      Set<SMB2ShareAccess> shareAccesses = new HashSet<>(
                              EnumSet.of(SMB2ShareAccess.FILE_SHARE_WRITE));
                      Set<FileAttributes> createOptions = new HashSet<>(
                              EnumSet.of(FileAttributes.FILE_ATTRIBUTE_NORMAL));
      
                      try (com.hierynomus.smbj.share.File file = diskShare
                              .openFile(destPath, accessMask, createOptions,
                                      shareAccesses,
                                      (overwrite
                                              ? SMB2CreateDisposition.FILE_OVERWRITE_IF
                                              : SMB2CreateDisposition.FILE_CREATE),
                                      EnumSet.noneOf(SMB2CreateOptions.class))) {
      
                          int bufferSize = 2048;
                          if (source.length() > 20971520l) {
                              bufferSize = 131072;
                          }
                          byte[] buffer = new byte[bufferSize];
                          long fileOffset = 0;
                          int length = 0;
                          while ((length = is.read(buffer)) > 0) {
                              fileOffset = diskShare.getFileInformation(destPath)
                                      .getStandardInformation().getEndOfFile();
                              file.write(buffer, fileOffset, 0, length);
                          }
                          file.flush();
                          file.close();
                      } finally {
                          is.close();
                      }
                  }
              }
      }
      

      当然,在此之前连接 SMB 服务器和进行身份验证需要花一点力气,但这是另一种情况......

      【讨论】:

        猜你喜欢
        • 2012-11-01
        • 2013-07-14
        • 2018-07-26
        • 2013-01-22
        • 2020-01-14
        • 1970-01-01
        • 2014-05-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多