4 Eylül 2014 Perşembe

JAVA SMB/SAMBA Protokolü ile Dosya Transferi(Sadece IP ile Dosya Transferi)

FTP ve SFTP ile dosya aktarımı yapabilmemiz için serverin 22 nolu portunu kullanma yetkimiz olması gerekiyor. Bu tarz durumlarda eğer kullanıcı adı ve şifremiz var ise SMB protokolü ile sadece IP,kullanıcı adı ve şifre  ile server'a bağlanıp dosya aktarımı yada indirme işlemi yapabiliriz. SMB Hakkında kısaca kısaca bilgi vermek gerekirse:

SMB kavramı İngilizce "Server Message Block" kelimelerinin kısaltmasıdır. İlk olarak IBM tarafından kullanılmıştır.

SMB protokolü bir bilgisayarda uygulamaları okuma ve dosyalara yazma ve bir bilgisayar ağı sunucu programlarından hizmet talep etmek için izin veren bir ağ dosya paylaşım protokolüdür.
SMB protokolü TCP / IP protokolü veya diğer ağ protokolleri üstünde kullanılabilir. SMB protokolü, bir uygulama   uzak bir sunucuda dosya veya diğer kaynaklara erişebilir.

Linux tarafında SMB karşılığı SAMBA olarak geçmektedir. Protokol aynı fakat isimler farklıdır. Bizim uygulamamız platform bağımsız çalışaçak. Kodlar aşağıdaki gibidir

 
Maven projesi genel yapısı



pom.xml deki jar tanımı



 

directDownload.java
SmbDocTransferEx\src\main\java\com\blogtest\ex\smbdoctransferex\directDownload.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.blogtest.ex.smbdoctransferex;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;

/**
 *
 * @author BeytullahC
 */
public class directDownload {

    public static void main(String[] args) throws Exception {
      

      
    }

    public static void download(String username, String password, String filePath, String downloadPath) throws Exception {
        String user = username + ":" + password;
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
        String path = "smb://" + filePath;
        SmbFile sFile = new SmbFile(path, auth);
        InputStream stream = new SmbFileInputStream(sFile);
        byte[] buffer = new byte[1024];
        BufferedOutputStream bos = null;
        try (BufferedInputStream bis = new BufferedInputStream(stream)) {
            File newFile = new File(downloadPath);
            OutputStream os = new FileOutputStream(newFile);
            bos = new BufferedOutputStream(os);
            int readCount;
            System.out.println("Writing: ");
            while ((readCount = bis.read(buffer)) > 0) {
                bos.write(buffer, 0, readCount);
            }
            System.out.println("End");
        } catch (IOException e) {
            e.printStackTrace(System.err);
        } finally {
            if (bos != null) {
                bos.close();
            }
        }
    }

    

}
SmbDocTransferEx\src\main\java\com\blogtest\ex\smbdoctransferex\directUpload.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.blogtest.ex.smbdoctransferex;

import java.io.Closeable;
import java.io.FileInputStream;
import java.io.IOException;
import jcifs.Config;
import jcifs.smb.SmbFileOutputStream;

/**
 *
 * @author BeytullahC
 */
public class directUpload {

    /**
     *
     * @param username
     * @param password
     * @param FilePath
     *
     * @param FileName
     * @param UploadPath
     * @return
     *
     * FilePath : C:/
     *
     * FileName : X.pdf
     *
     * upload path smb://directory
     *
     * if the operation is successful
     *
     * Return true
     *
     * else
     *
     * Return false
     *
     *
     */
    public static Boolean fileUpload(String username, String password, String FilePath, String FileName, String UploadPath) {
        String localFile = FilePath + "/" + FileName;
        String remuteUploadPath = UploadPath + "/" + FileName;
        //Config.setProperty("jcifs.smb.client.domain", "NTDOMAIN");
        Config.setProperty("jcifs.smb.client.username", username);
        Config.setProperty("jcifs.smb.client.password", password);
        //Config.setProperty( "jcifs.netbios.wins", "172.22.224.64");
        System.out.println("Upload ''" + localFile + "'' to ''" + remuteUploadPath + "''");

        try {
            SmbFileOutputStream out;
            int len = 0;
            int total = 0;
            try (FileInputStream in = new FileInputStream(localFile)) {
                out = new SmbFileOutputStream(remuteUploadPath);
                byte[] buf = new byte[1024 * 16];

                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                    total += len;
                }
            }
            out.close();
            System.out.println(total + " bytes copied.");
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
        return false;
    }

    static void close(Closeable resource) {
        if (resource != null) {
            try {
                resource.close();
            } catch (IOException e) {
                e.printStackTrace(System.err);
            }
        }
    }

}

-->

Hiç yorum yok:

Yorum Gönder