Copy File from Network folder using JCIFS is an Open Source client library

Here is the code snippet which is used for copying the files from Network folder with user name and password authentication to local drives. This is done with the help of JCIFS

Maven Dependencies

<dependency>
    <groupId>org.samba.jcifs</groupId>
    <artifactId>jcifs</artifactId>
    <version>1.3.14-kohsuke-1</version>
</dependency>

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import net.sf.jasperreports.engine.JRException;

public void copyFileFromNetwork() {

String userPassword = "userName" + ":" + "password";
String netWorkFolder = "smb://xxx.xxx.x.xx/files/pdfresults/";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
userPassword);
SmbFile smbFile;
FileOutputStream fileOutputStream;
InputStream fileInputStream;
byte[] buf;
int len;
try {
File temp = File.createTempFile("prefix", ".pdf");
smbFile = new SmbFile(netWorkFolder, auth);
// Look for File mydocument*.*
SmbFile[] files = smbFile.listFiles("mydocument" + "*");
if (files.length > 0) {
fileOutputStream = new FileOutputStream(temp);
fileInputStream = files[0].getInputStream();
buf = new byte[16 * 1024 * 1024];
while ((len = fileInputStream.read(buf)) > 0) {
fileOutputStream.write(buf, 0, len);
}
fileInputStream.close();
fileOutputStream.close();
} else
System.out.println(" File Not Found");
} catch (Exception e) {
System.out.println(" Error " + e);
}
}