-1

How we can push multiple files from our local folder to smb share folder using java. I can do with my single file using smbFile and it is working. I am looking for pushing multiple file push to smb share. Any reference links and sample code would be helpful. Thanks.

EDIT, Reference of code :

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

This works fine if i try to send one single file of anyformat. But if would like to send multiple file to smb share fromocal folder. For This i need thr help please. Thanks.

Jokkeri
  • 1,001
  • 1
  • 13
  • 35
Intensity
  • 1
  • 1

2 Answers2

0

Try to declare a SmbFile object that is a root folder of your folder, that you want to upload to the shared folder. Then iterate through the root.listFiles() array. Put the uploadable files in that folder.

I assume that, your SmbFile[] files array only contains one file if it's only uploading one file.

Or another possible solution is that, try to use SmbFileOutputStream and SmbFileInputStream instead of FileOutputStream and FileInputStream.

Soma Básthy
  • 110
  • 1
  • 8
0

I'm guessing you are using jcifs-library (which is quite outdated), so firstly I would recommend you to switch library. I switched to SMBJ and here is how I'm uploading file:

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://learn.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();
                }
            }
        }
}

Of course takes a little effort on connecting the SMB-server and authenticating before this, but that's another case...

Jokkeri
  • 1,001
  • 1
  • 13
  • 35