WebConnectHelper
一个操作共享盘复制文件的工具类
<dependency>
<groupId>org.samba.jcifs</groupId>
<artifactId>jcifs</artifactId>
<version>1.3.3</version>
</dependency>
实现代码
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;
import java.io.*;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WebConnectHelper {
//获取电脑序号ID 的正则表达式
private final String MATC_IP = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)";
public String getMATC_IP() {
return MATC_IP;
}
/**
* 复制文件到共享盘
* @param oldPath 需要复制文件路径
* @param newPath 文件复制到路径 不要文件名
* @param userName 共享盘账号
* @param passWord 共享盘密码
* @throws FileNotFoundException
*/
public void copuFile(String oldPath,String newPath,String userName,String passWord) throws FileNotFoundException {
File file = new File(oldPath);
String fileName = file.getName();
String createPath = newPath;
newPath = newPath + "/" + fileName;
String diskIP = matcAll(this.getMATC_IP(),newPath).get(0);
OutputStream outs=null;
InputStream ins=null;
try {
File localfile = new File(oldPath);
ins = new FileInputStream(localfile);
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(diskIP, userName, passWord);
SmbFile smbFilePath = new SmbFile(createPath, auth);
if (!smbFilePath.exists()){
smbFilePath.mkdirs();
}
SmbFile smbfile = new SmbFile(newPath, auth);
if (smbfile.exists()){
System.out.println("File is exists");
}else{
smbfile.connect();
outs= new SmbFileOutputStream(smbfile);
byte[] buffer = new byte[4096];
int len = 0;
while ((len = ins.read(buffer,0,buffer.length)) != -1){
outs.write(buffer,0,len);
}
outs.flush();
}
}catch (Exception e){
throw new FileNotFoundException(e.getMessage());
}finally {
if (null != outs){
try {
outs.close();
}catch (IOException e){
e.printStackTrace();
}
}
if (null != ins){
try {
ins.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
/**
* 获取电脑序号ID
* @param regex
* @param content
* @return
*/
private List<String> matcAll(String regex, String content){
List<String> list = new LinkedList<String>();
Matcher m = matcher(regex, content);
while (m.find()){
list.add(m.group());
}
return list;
}
/**
* 返回一个 mathcer
* @param regex
* @param content
* @return
*/
private Matcher matcher(String regex,String content){
return compile(regex,true).matcher(content);
}
/**
* 编译一个正则表达式
* @param regex
* @param isInsensitive
* @return
*/
private Pattern compile(String regex,boolean isInsensitive){
if (isInsensitive){
return Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
} else {
return Pattern.compile(regex);
}
}
}
测试
public boolean testSendAttachment1() throws FileNotFoundException {
WebConnectHelper connectHelper = new WebConnectHelper();
String oldPath = "复制文件路径";
//注意 共享盘路径前面要添加 smb: 例如: smb:127.0.0.1\\TVtoGIF\\vidio\\不干了.gif
String newPath = "共享盘路径";
String username = "username";
String password = "password";
connectHelper.copuFile(oldPath,newPath,username,password);
return true;
}