java上传文件到共享目录

上传文件到共享目录

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>/地址

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值