共享盘复制文件 - WebConnectHelper

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;
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值