JavaBlog.fr / Java.lu DEVELOPMENT,Java,Libray SAMBA JCIFS : Open Source library for CIFS/SMB networking

SAMBA JCIFS : Open Source library for CIFS/SMB networking

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/

001package com.huo.test.javacifs;
002 
003import java.io.File;
004import java.text.MessageFormat;
005import java.util.Calendar;
006import java.util.GregorianCalendar;
007import java.util.concurrent.Executors;
008import java.util.concurrent.ScheduledExecutorService;
009 
010import jcifs.smb.NtlmPasswordAuthentication;
011import jcifs.smb.SmbException;
012import jcifs.smb.SmbFile;
013import jcifs.smb.SmbFileFilter;
014 
015import org.apache.commons.io.FileUtils;
016import org.apache.commons.io.IOUtils;
017 
018/**
019 * Class Test SAMBA for CIFS library
020 * @author huseyino001
021 */
022public class TestCIFS implements Runnable {
023     
024    private NtlmPasswordAuthentication ntlmPasswordAuthentication = null;
025    private SmbFile inputFolder;
026    private int nbOfRetentionDays;
027 
028    public static void main(String[] args) {
029        // Creation of ExecutorService
030        final ScheduledExecutorService executorServiceOnceScanning = Executors.newSingleThreadScheduledExecutor();
031 
032        try {
033            System.out.println(" ############################## EXECUTION "+TestCIFS.class+" - START ############################## ");
034            final TestCIFS testCIFS = new TestCIFS();
035            //
036            NtlmPasswordAuthentication ntlmPasswordAuthentication = new NtlmPasswordAuthentication("LU", "huseyin", "mypassword");
037            testCIFS.ntlmPasswordAuthentication = ntlmPasswordAuthentication;
038            //
039            testCIFS.inputFolder = new SmbFile("", testCIFS.ntlmPasswordAuthentication);
040            System.out.println("Input folder = "+ testCIFS.inputFolder.getCanonicalPath());
041            //
042            testCIFS.nbOfRetentionDays = 7;
043            System.out.println("Nb of retention days = "+ testCIFS.nbOfRetentionDays);
044             
045            // Once scanning execution
046            executorServiceOnceScanning.submit(testCIFS);
047 
048            executorServiceOnceScanning.shutdown();
049 
050        } catch (Throwable e) {
051            e.printStackTrace();
052        }finally{
053            executorServiceOnceScanning.shutdown();
054        }
055    }
056 
057     
058    @Override
059    public void run() {
060        try {
061            processDeletingSubFolders(this);
062             
063        } catch (Exception e) {
064            e.printStackTrace();
065        } finally {
066            System.out.println(" ############################## EXECUTION "+TestCIFS.class+" - END ############################## ");
067        }
068    }
069 
070 
071     
072    private void processDeletingSubFolders(final TestCIFS testCIFS) {
073        try {
074            System.out.println(MessageFormat.format("Scanning \"input\" directory for deleting folders : \"{0}\" - START" + IOUtils.LINE_SEPARATOR, testCIFS.inputFolder.getCanonicalPath()));
075 
076            for (final SmbFile aFolderInINPUT_SmbFile : testCIFS.inputFolder.listFiles(new SmbFileFilter() {
077                @Override
078                public boolean accept(SmbFile aFolderInINPUT_SmbFile) throws SmbException {
079                    boolean ret = false;
080                     
081                    if(aFolderInINPUT_SmbFile.isDirectory()
082                        && aFolderInINPUT_SmbFile.canRead()
083                        && aFolderInINPUT_SmbFile.canWrite()
084                        && aFolderInINPUT_SmbFile.getName().startsWith("Folder_TO_DELETE_")){
085                         
086                        if(aFolderInINPUT_SmbFile.getLastModified()>0){
087                            Calendar calLastModified = GregorianCalendar.getInstance();
088                            calLastModified.setTimeInMillis(aFolderInINPUT_SmbFile.getLastModified());
089 
090                            Calendar calRetention = GregorianCalendar.getInstance();
091                            calRetention.add(Calendar.DAY_OF_MONTH, -testCIFS.nbOfRetentionDays);
092 
093                            ret = calLastModified.compareTo(calRetention)<0;
094                        }
095                    }
096                    return ret;
097                }
098            })) {
099                    System.out.println(MessageFormat.format("Deleting \"{0}\" - START", aFolderInINPUT_SmbFile));
100                    try {
101                        System.out.println(MessageFormat.format("{0}={1}", "Folder to delete", aFolderInINPUT_SmbFile.getCanonicalPath()));
102                        deleteDirectory(aFolderInINPUT_SmbFile);
103             
104                         
105                    } catch (Throwable e) {
106                        StringBuilder sb = new StringBuilder(MessageFormat.format("Unable to delete folder {0}", aFolderInINPUT_SmbFile));
107                        sb.append(IOUtils.LINE_SEPARATOR);
108                        sb.append(e.getMessage());
109                        System.out.println(sb.toString());
110                        e.printStackTrace();
111                    }
112                    System.out.println(MessageFormat.format("Deleting \"{0}\" - END", aFolderInINPUT_SmbFile));
113            }// FOR-END
114            System.out.println(MessageFormat.format("Scanning \"input\" directory for deleting folders : \"{0}\" - END" + IOUtils.LINE_SEPARATOR, testCIFS.inputFolder.getCanonicalPath()));
115 
116        } catch (Exception e) {
117            System.out.println("Listing of \""+testCIFS.inputFolder.getCanonicalPath()+"\" directory failed ");
118            e.printStackTrace();
119        }
120    }
121     
122     
123    private void deleteDirectory(SmbFile aSmbFile) throws Exception{
124        if(aSmbFile!=null){
125            aSmbFile.delete();
126            if(aSmbFile.exists()){
127                FileUtils.deleteDirectory(new File(aSmbFile.getUncPath()));
128            }
129        }
130    }
131     
132    private void deleteFile(SmbFile aSmbFile) throws Exception{
133        if(aSmbFile!=null){
134            final File fileToDelete = new File(aSmbFile.getCanonicalPath());
135            if (!fileToDelete.delete()) {
136                aSmbFile.delete();
137            }
138        }
139    }
140     
141}

Best regards,

Huseyin

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

Related Post