RemoteAccessData.java
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:password@11.132.3.13/prlife_ls_yanshou1_image/001/2013/12/05/10/00000053085901001/1/00000053085901001001.tif", "D:/download");
}
/**
* 路径格式:smb://192.168.75.204/test/新建 文本文档.txt
* smb://username:password@192.168.0.77/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下载