Hi,
Here, a simple example using the Samba Java CIFS Client Library. JCIFS is an Open Source client library that implements the CIFS/SMB networking protocol in 100% Java. CIFS is the standard file sharing protocol on the Microsoft Windows platform (e.g. Map Network Drive …). This client is used extensively in production on large Intranets.
Some resources:
https://jcifs.samba.org/
http://blog.icodejava.com/875/java-tutorial-using-jcifs-to-copy-files-to-shared-network-drive-using-username-and-password/
package com.huo.test.javacifs;
import java.io.File;
import java.text.MessageFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileFilter;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
/**
* Class Test SAMBA for CIFS library
* @author huseyino001
*/
public class TestCIFS implements Runnable {
private NtlmPasswordAuthentication ntlmPasswordAuthentication = null;
private SmbFile inputFolder;
private int nbOfRetentionDays;
public static void main(String[] args) {
// Creation of ExecutorService
final ScheduledExecutorService executorServiceOnceScanning = Executors.newSingleThreadScheduledExecutor();
try {
System.out.println(" ############################## EXECUTION "+TestCIFS.class+" - START ############################## ");
final TestCIFS testCIFS = new TestCIFS();
//
NtlmPasswordAuthentication ntlmPasswordAuthentication = new NtlmPasswordAuthentication("LU", "huseyin", "mypassword");
testCIFS.ntlmPasswordAuthentication = ntlmPasswordAuthentication;
//
testCIFS.inputFolder = new SmbFile("", testCIFS.ntlmPasswordAuthentication);
System.out.println("Input folder = "+ testCIFS.inputFolder.getCanonicalPath());
//
testCIFS.nbOfRetentionDays = 7;
System.out.println("Nb of retention days = "+ testCIFS.nbOfRetentionDays);
// Once scanning execution
executorServiceOnceScanning.submit(testCIFS);
executorServiceOnceScanning.shutdown();
} catch (Throwable e) {
e.printStackTrace();
}finally{
executorServiceOnceScanning.shutdown();
}
}
@Override
public void run() {
try {
processDeletingSubFolders(this);
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println(" ############################## EXECUTION "+TestCIFS.class+" - END ############################## ");
}
}
private void processDeletingSubFolders(final TestCIFS testCIFS) {
try {
System.out.println(MessageFormat.format("Scanning \"input\" directory for deleting folders : \"{0}\" - START" + IOUtils.LINE_SEPARATOR, testCIFS.inputFolder.getCanonicalPath()));
for (final SmbFile aFolderInINPUT_SmbFile : testCIFS.inputFolder.listFiles(new SmbFileFilter() {
@Override
public boolean accept(SmbFile aFolderInINPUT_SmbFile) throws SmbException {
boolean ret = false;
if(aFolderInINPUT_SmbFile.isDirectory()
&& aFolderInINPUT_SmbFile.canRead()
&& aFolderInINPUT_SmbFile.canWrite()
&& aFolderInINPUT_SmbFile.getName().startsWith("Folder_TO_DELETE_")){
if(aFolderInINPUT_SmbFile.getLastModified()>0){
Calendar calLastModified = GregorianCalendar.getInstance();
calLastModified.setTimeInMillis(aFolderInINPUT_SmbFile.getLastModified());
Calendar calRetention = GregorianCalendar.getInstance();
calRetention.add(Calendar.DAY_OF_MONTH, -testCIFS.nbOfRetentionDays);
ret = calLastModified.compareTo(calRetention)<0;
}
}
return ret;
}
})) {
System.out.println(MessageFormat.format("Deleting \"{0}\" - START", aFolderInINPUT_SmbFile));
try {
System.out.println(MessageFormat.format("{0}={1}", "Folder to delete", aFolderInINPUT_SmbFile.getCanonicalPath()));
deleteDirectory(aFolderInINPUT_SmbFile);
} catch (Throwable e) {
StringBuilder sb = new StringBuilder(MessageFormat.format("Unable to delete folder {0}", aFolderInINPUT_SmbFile));
sb.append(IOUtils.LINE_SEPARATOR);
sb.append(e.getMessage());
System.out.println(sb.toString());
e.printStackTrace();
}
System.out.println(MessageFormat.format("Deleting \"{0}\" - END", aFolderInINPUT_SmbFile));
}// FOR-END
System.out.println(MessageFormat.format("Scanning \"input\" directory for deleting folders : \"{0}\" - END" + IOUtils.LINE_SEPARATOR, testCIFS.inputFolder.getCanonicalPath()));
} catch (Exception e) {
System.out.println("Listing of \""+testCIFS.inputFolder.getCanonicalPath()+"\" directory failed ");
e.printStackTrace();
}
}
private void deleteDirectory(SmbFile aSmbFile) throws Exception{
if(aSmbFile!=null){
aSmbFile.delete();
if(aSmbFile.exists()){
FileUtils.deleteDirectory(new File(aSmbFile.getUncPath()));
}
}
}
private void deleteFile(SmbFile aSmbFile) throws Exception{
if(aSmbFile!=null){
final File fileToDelete = new File(aSmbFile.getCanonicalPath());
if (!fileToDelete.delete()) {
aSmbFile.delete();
}
}
}
}
Best regards,
Huseyin
