做一个 backup

package net.jnas;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException; import jcifs.Config;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbAuthException;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream; import org.apache.log4j.Logger; public class SambaJavaApi { //jcifs.smb.SmbAuthException: Logon failure: unknown user name or bad password.
//jcifs.smb.SmbException: Access is denied.
//寫得時候,smb_dir必須是文件名,而不是路徑 private static String smb_dir = "lnas_service/aa/aaa.doc";//"/home/test/share";
private static String password = "";
private static String user = "lk2";
private static String ip = "192.168.4.49";
private Logger logger = Logger.getLogger(this.getClass().getName());
private String local_tempDir = "E:\\work\\samba\\test\\11.doc"; public static void main(String[] args) { SambaJavaApi sa = new SambaJavaApi(); user = "admin";
password = "admin";
sa.uploadViaShare(ip , user, password, smb_dir);
} public void uploadViaShare(final String ip,final String user,final String password,final String dir)
{
logger.debug("Share(SMB) download!"); String newDir = dir;
String url = "";
FileInputStream fos = null;
SmbFileOutputStream smbIs = null;
byte [] buffer = new byte[];
int readBytes = ;
int totalBytes = ; if (!dir.endsWith("/")) //directory must end with "/"
newDir = dir+"/";
url = "smb://"+user+":"+password+"@"+ip+""+newDir; NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(ip, user, password); long startTime = System.currentTimeMillis();
try {
url = "smb://"+ip +"/"+ dir;
System.out.println(url); Config.setProperty("jcifs.netbios.wins", ip); SmbFile shareDir = new SmbFile(url, auth);
shareDir.connect();
// SmbFile shareDir = new SmbFile(url);
if(shareDir.isDirectory())
{
smbIs = new SmbFileOutputStream(shareDir);
fos = new FileInputStream(new File(local_tempDir));
while((readBytes = fos.read(buffer)) > )
{
smbIs.write(buffer,,readBytes);
totalBytes += readBytes;
}
smbIs.close();
fos.close();
long endTime = System.currentTimeMillis();
long timeTaken = endTime-startTime;
logger.debug(totalBytes +"bytes downloaded in " + timeTaken/ + " seconds at "+ (( totalBytes / ) / Math.max( , ( timeTaken / ))) + "Kb/sec");
}
}catch(MalformedURLException urle)
{
logger.debug("Incorrect URL format!");
}catch (SmbException smbe) {
smbe.printStackTrace();
logger.debug(this.getClass().getName()+"||"+smbe.getMessage());
}catch(IOException ioe)
{
ioe.printStackTrace();
logger.debug(this.getClass().getName()+"||"+ioe.getMessage());
}finally
{
try
{
smbIs.close();
fos.close();
}catch(Exception smbe)
{
logger.debug(this.getClass().getName()+"||"+smbe.getMessage());
}
} } public void downloadViaShare(final String ip,final String user,final String password,final String dir)
{
logger.debug("Share(SMB) download!"); String newDir = dir;
String url = "";
SmbFile [] fileList = null;
FileOutputStream fos = null;
SmbFileInputStream smbIs = null;
byte [] buffer = new byte[];
int readBytes = ;
int totalBytes = ; if (!dir.endsWith("/")) //directory must end with "/"
newDir = dir+"/";
url = "smb://"+user+":"+password+"@"+ip+""+newDir; NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(ip, user, password); long startTime = System.currentTimeMillis();
try {
url = "smb://"+ip +"/"+ dir; System.out.println(url); // final NtlmPasswordAuthentication AUTH = new NtlmPasswordAuthentication("domainname", "username", "password");
Config.setProperty("jcifs.netbios.wins", ip); // Properties props = new Properties();
// InputStream in = new SmbFileInputStream(new SmbFile("smb://10.10.1.1:445/rootfolder/path/filename.properties", AUTH));
// props.load(in); SmbFile shareDir = new SmbFile(url, auth);
shareDir.connect();
// SmbFile shareDir = new SmbFile(url);
if(shareDir.isDirectory())
{
fileList = shareDir.listFiles();
for(int i=;i<fileList.length;i++)
{
if(fileList[i].isFile())
{
smbIs = new SmbFileInputStream((SmbFile)fileList[i]);
fos = new FileOutputStream(new File(local_tempDir +File.separator+fileList[i].getName()));
while((readBytes = smbIs.read(buffer)) > )
{
fos.write(buffer,,readBytes);
totalBytes += readBytes;
}
smbIs.close();
fos.close(); // break;
// logger.debug(fileList[i].getName() + " is downloaded!");
// try
// {
// fileList[i].delete();
// }catch(SmbAuthException smbae )
// {
// logger.debug(fileList[i].getName()+" can not be deleted!");
// }
}
}
long endTime = System.currentTimeMillis();
long timeTaken = endTime-startTime;
logger.debug(totalBytes +"bytes downloaded in " + timeTaken/ + " seconds at "+ (( totalBytes / ) / Math.max( , ( timeTaken / ))) + "Kb/sec");
}
}catch(MalformedURLException urle)
{
logger.debug("Incorrect URL format!");
}catch (SmbException smbe) {
smbe.printStackTrace();
logger.debug(this.getClass().getName()+"||"+smbe.getMessage());
}catch(IOException ioe)
{
ioe.printStackTrace();
logger.debug(this.getClass().getName()+"||"+ioe.getMessage());
}finally
{
try
{
smbIs.close();
fos.close();
}catch(Exception smbe)
{
logger.debug(this.getClass().getName()+"||"+smbe.getMessage());
}
} } // private void Asasf() {
// // TODO Auto-generated method stub
// String strprog = "STRING CREATED| "; //debug log string
// try {
// strprog += "NTLM| ";
// NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("username:password");
// strprog += "SMB| ";
// SmbFile file = new SmbFile("smb://<pcname>/foldername/filename.txt",auth);
//
// strprog += "EXIST| ";
// String there = String.valueOf(file.exists());
//
// strprog += "View| ";
// TextView pp;
// pp = (TextView) findViewById(R.id.tv);
// pp.setText(there);
//
//
//
// } catch (Exception e) {
// // TODO Auto-generated catch block
// strprog += "ERROR! ";
// TextView ll;
// ll = (TextView) findViewById(R.id.tv);
//
//
// ll.setText(strprog + e.getStackTrace().toString() + " " + e.getMessage() + " " + e.getLocalizedMessage() );
// }
// }
}

SambaJava API的更多相关文章

  1. 干货来袭-整套完整安全的API接口解决方案

    在各种手机APP泛滥的现在,背后都有同样泛滥的API接口在支撑,其中鱼龙混杂,直接裸奔的WEB API大量存在,安全性令人堪优 在以前WEB API概念没有很普及的时候,都采用自已定义的接口和结构,对 ...

  2. 12306官方火车票Api接口

    2017,现在已进入春运期间,真的是一票难求,深有体会.各种购票抢票软件应运而生,也有购买加速包提高抢票几率,可以理解为变相的黄牛.对于技术人员,虽然写一个抢票软件还是比较难的,但是还是简单看看123 ...

  3. 几个有趣的WEB设备API(二)

    浏览器和设备之间还有很多有趣的接口, 1.屏幕朝向接口 浏览器有两种方法来监听屏幕朝向,看是横屏还是竖屏. (1)使用css媒体查询的方法 /* 竖屏 */ @media screen and (or ...

  4. html5 canvas常用api总结(三)--图像变换API

    canvas的图像变换api,可以帮助我们更加方便的绘画出一些酷炫的效果,也可以用来制作动画.接下来将总结一下canvas的变换方法,文末有一个例子来更加深刻的了解和利用这几个api. 1.画布旋转a ...

  5. JavaScript 对数据处理的5个API

    JavaScript对数据处理包括向上取整.向下取整.四舍五入.固定精度和固定长度5种方式,分别对应ceil,floor,round,toFixed,toPrecision等5个API,本文将对这5个 ...

  6. ES5对Array增强的9个API

    为了更方便的对Array进行操作,ES5规范在Array的原型上新增了9个方法,分别是forEach.filter.map.reduce.reduceRight.some.every.indexOf ...

  7. javascript的api设计原则

    前言 本篇博文来自一次公司内部的前端分享,从多个方面讨论了在设计接口时遵循的原则,总共包含了七个大块.系卤煮自己总结的一些经验和教训.本篇博文同时也参考了其他一些文章,相关地址会在后面贴出来.很难做到 ...

  8. 一百元的智能家居——Asp.Net Mvc Api+讯飞语音+Android+Arduino

    大半夜的,先说些废话提提神 如今智能家居已经不再停留在概念阶段,高大上的科技公司都已经推出了自己的部分或全套的智能家居解决方案,不过就目前的现状而言,大多还停留在展厅阶段,还没有广泛的推广起来,有人说 ...

  9. 在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用

    由于ASP.NET Web API具有与ASP.NET MVC类似的编程方式,再加上目前市面上专门介绍ASP.NET Web API 的书籍少之又少(我们看到的相关内容往往是某本介绍ASP.NET M ...

随机推荐

  1. Python关于self用法重点分析

    在介绍Python的self用法之前,先来介绍下Python中的类和实例…… 我们知道,面向对象最重要的概念就是类(class)和实例(instance),类是抽象的模板,比如学生这个抽象的事物,可以 ...

  2. java 菜单 中文 乱码问题解决方案(使用idea/eclipse) swing MenuItem乱码

    java swing MenuItem乱码 2016年12月30日 13:32:08751人阅读 评论(0) 收藏 举报   用java开发一个带有托盘图标的程序, 其它模块的中文显示都是正常的,比如 ...

  3. 【VSCode】Windows下VSCode编译调试c/c++【更新】

    便携版已更新,点此获取便携版 用于cpptools插件的配置文件更新 更新的launch.json // Available variables which can be used inside of ...

  4. nginx简单学习(tomcat)

    一.负载均衡的简单配置 1.下载nginx 2.tomcat*2 配置不同的端口用于正常启动,在jsp中<%= session.getId()%>可以查看jSessionId,tomcat ...

  5. Flume的基本概念

    Flume 概念 Flume 最早是Cludera提供的日志收集系统,后贡献给Apache.所以目前是Apache下的项目,Flume支持在日志系统中指定各类数据发送方,用于收集数据. Flume 是 ...

  6. ACM主要算法

    ACM主要算法ACM主要算法介绍 初期篇 一.基本算法(1)枚举(poj1753, poj2965)(2)贪心(poj1328, poj2109, poj2586)(3)递归和分治法(4)递推(5)构 ...

  7. angular4+中的数据绑定

    1,基本的属性绑定,下面的两种形式是一样的 2.dom属性和html属性 3.两者关系的进一步总结 4.dom属性绑定 5.html属性绑定

  8. 知识点:linux数据库备份

    服务端启用二进制日志 如果日志没有启开,必须启用binlog,要重启mysql,首先,关闭mysql,打开/etc/my.cnf,加入以下几行: [mysqld] log-bin 然后重新启动mysq ...

  9. matplotlib基础知识全面解析

    图像基本知识: 通常情况下,我们可以将一副Matplotlib图像分成三层结构: 1.第一层是底层的容器层,主要包括Canvas.Figure.Axes: 2.第二层是辅助显示层,主要包括Axis.S ...

  10. set函数&操作

    集合的交叉并补 交集, 共同的部分 set1 & set2 set1.intersection(set2) 差集 set1有set2没有的元素 set1 - set2 set1.differe ...