上传文件到共享目录
import com.phy.bcs.BcsServiceInterfaceApplication;
import com.phy.bcs.service.ifs.ftp.camel.util.FileUtils;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.io.*;
import java.text.SimpleDateFormat;
@Component
@Slf4j
public class ShareDirectoryTool extends Thread {
//共享目录ip地址
private String host = "";
//用户名
private String userName = "";
//密码
private String possword = "";
//目的地文件夹(目录)
private String dstUrl = "";
//文件来源目录(会将此目录下符合正则表达式的文件拷走)
private String srcUrl = "";
//正则表达式
private String regular = "";
//根据情况传进来共享目录的参数可以实现定时上传目录中的文件
public ShareDirectoryTool(String host,String userName,String possword,String dstUrl,String srcUrl,String regular){
this.host = host;
this.userName = userName;
this.possword = possword;
this.dstUrl = dstUrl;
this.srcUrl = srcUrl;
this.regular = regular;
}
@Override
public void run(){
while(true){
try{
Thread.sleep(5000);
copyShareDirectory();
} catch(Exception e){
e.printStackTrace();
}
}
}
//可以将里面复制的方法考出来使用
public boolean copyShareDirectory(){
boolean ok = false;
InputStream in = null;
OutputStream out = null;
try {
//获取来源目录
File file = new File(srcUrl);
if (file.exists()){ //是否存在
File [] files = file.listFiles(); //获取目录下全部文件
for (int i = 0; i < files.length; i++) {
//判断是不是文件夹
if (files[i].isDirectory()){
log.info("目录:{},下文件:{},为文件夹",srcUrl,files[i].getName());
continue;
}
//是否符合正则
if (!files[i].getName().matches(regular)){
log.info("目录:{},下文件:{},不符合正则:{}",srcUrl,files[i].getName(),regular);
continue;
}
//备份文件() 源目录 //配置备份的目录 当前日志
//如果配置的备份目录为 fileBak今天日期为2021-02-21 那么改文件将会备份到 fileBak/20210221/下(文件名不变)
copyFile(files[i].getPath() , BcsServiceInterfaceApplication.ftpProperties.getShareDirectoryBak() + BcsServiceInterfaceApplication.datepath(files[i].getName()));
String remotePhotoUrl = "smb://"+userName+":"+possword+"@"+host+dstUrl;
//如果怕重名可以使用日期重名一下上传后的文件名
//SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS_");
//SmbFile smbFile = new SmbFile(remotePhotoUrl+"/"+format.format(new Date())+"_"+file.getName());
//不需要重命名
SmbFile smbFile = new SmbFile(remotePhotoUrl+"/"+file.getName());
smbFile.connect();//链接
in = new BufferedInputStream(new FileInputStream(file));
out = new BufferedOutputStream(new SmbFileOutputStream(smbFile));
byte [] buffer = new byte[4096];
int len = 0;
while ((len = in.read(buffer,0,buffer.length)) != -1){
out.write(buffer,0,len);
}
//刷新
out.flush();
ok = true;
//是否需要删除源文件默认上传成功后删除(可以更灵活的配置加个属性或参数)
if (ok){
log.info("文件:{},发送给IP:{},userName:{},possword:{},url:{}成功!!",files[i].getName(),host,userName,possword,dstUrl);
if(files[i].delete()){
log.info("删除源文件:{},成功!!",files[i].getName());
}else {
log.info("删除源文件:{},失败!!",files[i].getName());
}
}else {
log.info("文件:{},发送给IP:{},userName:{},possword:{},url:{}失败!!",files[i].getName(),host,userName,possword,dstUrl);
}
}
}else {
log.info("目录:{},不存在!!",srcUrl);
}
}catch(Exception e){
e.printStackTrace();
ok = false;
log.debug("上传共享目录出错:{}",e);
}finally {
try {
if (out != null){
out.close();
}
if (in != null){
in.close();
}
}catch (Exception e){
e.printStackTrace();
}
return ok;
}
}
//复制文件
public static void copyFile(String originpath, String newpath) throws IOException {
File file = new File(originpath);
if(file.isDirectory() || !file.exists() || !file.canWrite())
return;
org.apache.commons.io.FileUtils.copyFile(file, new File(newpath));
}
}
创建共享目录
创建共享目录:https://blog.csdn.net/weixin_43583693/article/details/115675116
有可能遇到的错
有可能遇到的错:jcifs.smb.SmbException:Failed to connect:0.0.0.0<00>/地址