做一个 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. PL/SQL Developer 使用小技巧

    1.PL/SQL Developer记住登陆密码 在使用PL/SQL Developer时,为了工作方便希望PL/SQL Developer记住登录Oracle的用户名和密码: 设置方法:tools- ...

  2. 在MacOSX系统上的一些工具和问题汇总

    Android 模拟器 1. 安装模拟器 点击链接:https://cloud.genymotion.com/page/launchpad/download 需要先注册登录一下. 2.安装Virtua ...

  3. IntelliJ Idea 跳出括号并且光标移到末尾的快捷键

    直接跳出的shift enter不管现在光标在哪个位置,直接新开一行 跳出双引号:shift + "跳出单引号:'跳出括号:shift + )跳出中括号:]以此类推.

  4. 结合《剑指offer(第二版)》面试题51来谈谈归并排序

    一.题目大意 给定一个数组A,对于数组A中的两个数字,如果排在前面的一个数字大于(必须大于,等于不算)后面的数字,则这两个数字组成一个逆序对.要求输出数组A中的逆序对的总数.例如,对于数组{7,5,6 ...

  5. 【spring boot】使用@Value映射properties文件属性

    描述 使用@Value映射properties文件属性到Java字段 重点 使用@PropertySource 注解指定*.properties文件位置: 使用@Value进行注入: my.prope ...

  6. Python——字符串2.0(实验)(python programming)

    直接打s,是程序员看到的:打print(),是用户看到的 列表 ] #列表索引,与数组唯一不同:等号左端可修改 转载自:https://www.cnblogs.com/wwwwwei/p/104816 ...

  7. [转][html5]网页横屏

    @@media screen and (orientation:portrait){ body{ transform: rotate(90deg); } } 将以上 CSS 加入网页,可以发现手机上查 ...

  8. jQuery 事件的命名空间简单了解

    原文地址:http://www.jb51.net/article/43626.htm   用 jQuery 绑定和解绑事件监听器都是非常简单的,怎样精确地解绑其中一个监听器?我们需要了解一下事件的命名 ...

  9. HDOJ 2008 数值统计

    #include<iostream> using namespace std; int main() { int n; ) { , y = , z = ; double t; ;i < ...

  10. [UE4]子弹碰撞

    在子弹actor蓝图“BP_LauncherBullet”中添加一个球体碰撞组件“Sphere Collision”,并设置为actor的根组件(直接拖放到默认根组件上面就会覆盖) 子弹碰撞预设 “B ...