java Samba文件操作

samba操作需要samba.jcifs库和commons-io支持
/**samba服务测试*/
public class TestSamba {

    /**测试向samba服务器上传文件*/
    @Test
    public void testPut(){
        try {
            /*ip,user,pass应该通过配置文件获取*/
            String ip="102.168.11.133";
            UniAddress ua=UniAddress.getByName(ip);
            NtlmPasswordAuthentication auth=new NtlmPasswordAuthentication(
                    ip,"user","pass");
            SmbSession.logon(ua, auth);//验证能否登陆成功
            //创建smb文件一定要smb协议
            SmbFile sf=new SmbFile("smb://"+ip+"/smb/stu.tar",auth);
            IOUtils.copyLarge(new FileInputStream("d:/abc.txt"), 
                    sf.getOutputStream());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (SmbException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**测试从服务器下载文件*/
    @Test
    public void testGet(){
        try {
            /*ip,user,pass应该通过配置文件获取*/
            String ip="102.168.11.133";
            UniAddress ua=UniAddress.getByName(ip);
            NtlmPasswordAuthentication auth=new NtlmPasswordAuthentication(
                    ip,"user","pass");
            SmbSession.logon(ua, auth);//验证能否登陆成功
            //创建smb文件一定要smb协议
            SmbFile sf=new SmbFile("smb://"+ip+"/smb/stu.tar",auth);
            IOUtils.copyLarge(sf.getInputStream(), 
                    new FileOutputStream("d:/abc.zip"));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (SmbException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


你可能感兴趣的:(java Samba文件操作)