将多个文件推送到本地文件夹中的 smb 共享 jcif

问题描述

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

编辑,代码参考:

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

if (files == null)
    return false;
output(sb,"      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,"         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,len);
        }
        fileInputStream.close();
        fileOutputStream.close();
    } catch (SmbException e) {
        OutputHandler.output(sb,"Exception during copyNetworkFilesToLocal stream to output,SMP issue: " + e.getMessage(),e);
        e.printstacktrace();
        return false;
    } 

如果我尝试发送任何格式的单个文件,这工作正常。但是如果想将多个文件发送到 smb 共享 fromocal 文件夹。为此,我需要帮助。谢谢。

解决方法

代码参考:

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

if (files == null)
    return false;
output(sb,"      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,"         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,len);
        }
        fileInputStream.close();
        fileOutputStream.close();
    } catch (SmbException e) {
        OutputHandler.output(sb,"Exception during copyNetworkFilesToLocal stream to output,SMP issue: " + e.getMessage(),e);
        e.printStackTrace();
        return false;
    } 

如果我尝试发送任何格式的单个文件,这工作正常。但是如果想将多个文件发送到 smb 共享 fromocal 文件夹。为此,我需要帮助。 谢谢。

,

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

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

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

,

我猜你正在使用 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,length);
                    }
                    file.flush();
                    file.close();
                } finally {
                    is.close();
                }
            }
        }
}

当然在此之前连接 SMB 服务器并进行身份验证需要一些努力,但那是另一种情况...