Tuesday, 27 January 2015

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

4 comments:

  1. Friend tested the code but would be giving the following error to me: Error jcifs.smb.SmbException: Access is denied.
    How can I resolve this?

    ReplyDelete
  2. Microsoft office has a different version, and the entire version has the different setup file. Some of the version is Office 2013, Office 2016 and Office 365 etc. Go to www.office.com/setup and redeem your key. Office Com Setup is one of the most popular productivity programs around. If you have your product key handy, you don't even need a disc to start installing Office just visit office setup help. Norton is one of the most reliable antivirus providers in the market to download visit www.norton.com/setup . There are multiple manufacturers making printers for home and office use with varied range as printer series per the need and affordability of different customers. Printer Tech Support. It has become one of the most useful computer hardware devices that allow people to convert computer generated typescript into a physical printed document.

    ReplyDelete
  3. I would like to thank you for the efforts you have made in writing this article god bless You. You have a bright future ahead.
    We are providing help and support for Microsoft office Setup and activation. Call us or email us the error or problem, our one of the expert contact you with the suitable perfect solution. Get the MS Office application suite and as per your need and see how it is easy to work with Microsoft Office. http://setupoffize.com
    mcafee is one of the most reliable antivirus providers in the market. Norton is delivering top rated protection from mobile devices and computers. Norton has 3 antivirus packages http://mcafeecomactivatenow.xyz
    we also provide apples apple support and apple support number at http://applenumber.com

    ReplyDelete