1. 程式人生 > >JAVA下載遠端Linux伺服器的檔案

JAVA下載遠端Linux伺服器的檔案

<h1><span style="background-color: rgb(255, 255, 255);">RemoteAccessData.java</span></h1>
package com.yzj.demo;
import java.io.BufferedInputStream;  
import java.io.BufferedOutputStream;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.OutputStream;  

import org.apache.log4j.Logger;

import com.yzj.log.LogFactory;
  
import jcifs.smb.SmbFile;  
import jcifs.smb.SmbFileInputStream;  
import jcifs.smb.SmbFileOutputStream;  
  
public class RemoteAccessData {  
	
	private static Logger logger = LogFactory.getInstance(RemoteAccessData.class);
  
    /** 
     * @param args 
     * @throws IOException  
     */  
    public static void main(String[] args) throws IOException {  
    	
    	smbGet("smb://username:[email protected]/prlife_ls_yanshou1_image/001/2013/12/05/10/00000053085901001/1/00000053085901001001.tif", "D:/download");
    }  
    
    /** 
     * 路徑格式:smb://192.168.75.204/test/新建 文字文件.txt 
     *          smb://username:[email protected]/test 
     * @param remoteUrl 
     *            遠端路徑 
     * @param localDir 
     *            要寫入的本地路徑 
     */  
    public static void smbGet(String remoteUrl, String localDir) {  
    	
        InputStream in = null;  
        OutputStream out = null;  
        
        try {  
        	
            SmbFile remoteFile = new SmbFile(remoteUrl);  
            
            if (remoteFile != null && remoteFile.exists()) {  
            	
            	String fileName = remoteFile.getName();  
                File localFile = new File(localDir + File.separator + fileName);  
                in = new BufferedInputStream(new SmbFileInputStream(remoteFile));  
                out = new BufferedOutputStream(new FileOutputStream(localFile));  
                byte[] buffer = new byte[1024];  
                while (in.read(buffer) != -1) {  
                    out.write(buffer);  
                    buffer = new byte[1024];  
                }  
            } else {
            	
            	// 檔案不存在
            	logger.info(remoteUrl + "    檔案不存在!");
            }
            
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
        	
            try {
				out.close();
				out = null;
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
        }  
    }  
  
    /**
     * 向共享目錄上傳檔案  
     * @param remoteUrl
     * @param localFilePath
     */
    public static void smbPut(String remoteUrl, String localFilePath) {  
    	
        InputStream in = null;  
        OutputStream out = null; 
        
        try {  
        	
            File localFile = new File(localFilePath);  
            String fileName = localFile.getName();  
            SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName);  
            in = new BufferedInputStream(new FileInputStream(localFile));  
            out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));  
            byte[] buffer = new byte[1024];  
            while (in.read(buffer) != -1) {  
                out.write(buffer);  
                buffer = new byte[1024];  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
        	
            try {
				out.close();
				out = null;
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
        }  
    }  
 
}  

RemoteAccessData.java與jcifs-1.3.14.jar下載