Tuesday, 27 January 2015

Create New File in Network folder using JCIFS is an Open Source client library

Here is the code snippet which is used to create file in 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 boolean createFileInNetworkFolder() {
boolean successful = false;
try {
String userPassword = "UserName" + ":" + "pasword";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
userPassword);
String newFileName = "sample.txt";
String fileContent = "Just a Test File";
String path = "smb://xxx.xxx.x.xx/files/" + newFileName;
SmbFile sFile = new SmbFile(path, auth);
SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
sfos.write(fileContent.getBytes());
successful = true;
sfos.close();
} catch (Exception e) {
successful = false;
System.out.println("Problem in Creating New File :" + e);
}
return successful;
}

1 comment:

  1. Thanks for the guide, specially the way you've explained the whole thing is just awesome. Thanks for sharing these codes with us, I was looking for creating a new file in network folder using JCIFS but I wasn't able to find any authentic guide about that, Thank to you for this guide I get my solution. Keep up the great work buddy.

    Dental Websites Design is the specialty of OptiMized360.Com

    ReplyDelete