SMB/CIFS协议简介:
API地址: http://jcifs.samba.org/src/docs/api/
smb代码:
package com.jeyo.cnms.sim.smb;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;
import jcifs.util.LogStream;
/**
* SMB、CIFS协议 SMB :Server Message Block CIFS:Common Internet File System
*
* @author owner
*
*/
public class Smb {
private static LogStream log = LogStream.getInstance();
private String url = "";
private String localDirectory = "";
private SmbFile smbFile = null;
private SmbFileOutputStream smbOut = null;
private static Smb smb = null;
/**
*
* @param url 服务器路径
* @param localDirectory 本地文件路径
*/
private Smb(String url, String localDirectory) {
this.url = url;
this.localDirectory = localDirectory;
this.init();
}
public static synchronized Smb getInstance(String url,String filepath){
if(smb == null)
return new Smb(url,filepath);
return smb;
}
public void init() {
try {
log.println("开始连接...url:" + this.url);
smbFile = new SmbFile(this.url);
smbFile.connect();
log.println("连接成功...url:" + this.url);
} catch (MalformedURLException e) {
e.printStackTrace();
log.print(e);
}catch(IOException e){
e.printStackTrace();
log.print(e);
}
}
/**
* 上传文件到服务器
* @param filename 文件名
* @return -1 上传失败,0为成功
*/
public int uploadFile(String filename) {
int flag = -1;
BufferedInputStream bf = null;
try {
this.smbOut = new SmbFileOutputStream(this.url+filename, false);
log.println("开始传输文件 filename: " + this.localDirectory+filename);
File file = new File(this.localDirectory+filename);
bf = new BufferedInputStream(
new FileInputStream(file));
byte[] bt = new byte[8192];
int n = bf.read(bt);
while ( n != -1) {
this.smbOut.write(bt, 0, n);
this.smbOut.flush();
n = bf.read(bt);
}
flag = 0;
log.println("文件传输结束...");
} catch (SmbException e) {
e.printStackTrace();
log.println(e);
} catch (MalformedURLException e) {
e.printStackTrace();
log.println(e);
} catch (UnknownHostException e) {
e.printStackTrace();
log.println("找不到主机...url:" + this.url);
} catch (FileNotFoundException e) {
e.printStackTrace();
log.println("找不到本地文件," + this.localDirectory);
} catch (IOException e) {
e.printStackTrace();
log.println(e);
} finally {
try {
if (this.smbOut != null)
this.smbOut.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bf != null)
bf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return flag;
}
public boolean isExists(String filename) throws SmbException, MalformedURLException{
boolean flag = false;
smbFile = new SmbFile(this.url + filename);
flag = this.smbFile.exists();
return flag;
}
}
使用代码:
package com.jeyo.cnms.sim.smb;
public class BcpFileTransfer {
private static String url = ""; //
private String localDerictroy; // 本地路径
private static String remoteDerictory; //服务器带盘符的路径
private Smb smb = null;
static {
BcpProperties bcpProperties = BcpProperties.getInstance();
String username = bcpProperties.getValue("host.username"); // host:用户名
String password = bcpProperties.getValue("host.password"); // host:密码
String hostUrl = bcpProperties.getValue("host.url"); // 远程目录
// "smb://username:password@10.60.1.119/temp/filename"
url="smb://"+username+":"+password+"@"+hostUrl;
System.out.println(url);
remoteDerictory = bcpProperties.getValue("host.directory"); // 远程带盘符的路径
}
/**
*
* @param localDerictroy 本地路径
*/
public BcpFileTransfer(String localDerictroy) {
this.localDerictroy = localDerictroy;
smb = Smb.getInstance(url, this.localDerictroy);
}
/**
* 将文件上传到目标服务器,返回文件明。
* @param filename
* @return 在目标服务器生成的带盘符文件路径
* @throws Exception
*/
public String uploadFile(String filename) throws Exception{
String file = null;
try{
int flag = smb.uploadFile(filename);
if(flag == -1)
throw new RuntimeException("传输BCP文件失败...");
file = remoteDerictory + filename;
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
return file;
}
public static void main(String[] args) throws Exception {
BcpFileTransfer bcp = new BcpFileTransfer("d://");
System.out.println(bcp.uploadFile("ttRS_SimCardResourceTemp.fmt"));
System.out.println(bcp.uploadFile("1_1034462_1345030.txt"));
System.out.println(bcp.uploadFile("JDK150中文第五版.chm"));
}
}
1.注意事项:登陆服务器的密码不支持强密码(如密码含有 &……%¥# 等字符,都当成分隔符处理)。
如:url="smb://"+username+":"+password+"@"+hostUrl;
password 不能含有特殊字符