SambaJava API
做一个 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的更多相关文章
- 干货来袭-整套完整安全的API接口解决方案
在各种手机APP泛滥的现在,背后都有同样泛滥的API接口在支撑,其中鱼龙混杂,直接裸奔的WEB API大量存在,安全性令人堪优 在以前WEB API概念没有很普及的时候,都采用自已定义的接口和结构,对 ...
- 12306官方火车票Api接口
2017,现在已进入春运期间,真的是一票难求,深有体会.各种购票抢票软件应运而生,也有购买加速包提高抢票几率,可以理解为变相的黄牛.对于技术人员,虽然写一个抢票软件还是比较难的,但是还是简单看看123 ...
- 几个有趣的WEB设备API(二)
浏览器和设备之间还有很多有趣的接口, 1.屏幕朝向接口 浏览器有两种方法来监听屏幕朝向,看是横屏还是竖屏. (1)使用css媒体查询的方法 /* 竖屏 */ @media screen and (or ...
- html5 canvas常用api总结(三)--图像变换API
canvas的图像变换api,可以帮助我们更加方便的绘画出一些酷炫的效果,也可以用来制作动画.接下来将总结一下canvas的变换方法,文末有一个例子来更加深刻的了解和利用这几个api. 1.画布旋转a ...
- JavaScript 对数据处理的5个API
JavaScript对数据处理包括向上取整.向下取整.四舍五入.固定精度和固定长度5种方式,分别对应ceil,floor,round,toFixed,toPrecision等5个API,本文将对这5个 ...
- ES5对Array增强的9个API
为了更方便的对Array进行操作,ES5规范在Array的原型上新增了9个方法,分别是forEach.filter.map.reduce.reduceRight.some.every.indexOf ...
- javascript的api设计原则
前言 本篇博文来自一次公司内部的前端分享,从多个方面讨论了在设计接口时遵循的原则,总共包含了七个大块.系卤煮自己总结的一些经验和教训.本篇博文同时也参考了其他一些文章,相关地址会在后面贴出来.很难做到 ...
- 一百元的智能家居——Asp.Net Mvc Api+讯飞语音+Android+Arduino
大半夜的,先说些废话提提神 如今智能家居已经不再停留在概念阶段,高大上的科技公司都已经推出了自己的部分或全套的智能家居解决方案,不过就目前的现状而言,大多还停留在展厅阶段,还没有广泛的推广起来,有人说 ...
- 在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用
由于ASP.NET Web API具有与ASP.NET MVC类似的编程方式,再加上目前市面上专门介绍ASP.NET Web API 的书籍少之又少(我们看到的相关内容往往是某本介绍ASP.NET M ...
随机推荐
- FLIR ONE PRO热成像仪
FLIR ONE PRO热成像仪 https://www.chiphell.com/thread-1774218-1-1.html
- [蓝桥杯]ALGO-49.算法训练_寻找数组中最大值
题目描述: 问题描述 对于给定整数数组a[],寻找其中最大值,并返回下标. 输入格式 整数数组a[],数组元素个数小于1等于100.输出数据分作两行:第一行只有一个数,表示数组元素个数:第二行为数组的 ...
- LeetCode——2. Add Two Numbers
一.题目链接:https://leetcode.com/problems/add-two-numbers 二.题目大意: 给定两个链表,每个链表表示一个非负数,不过数字的顺序是反过来存储的,要求给出这 ...
- 迭代器和增强for
增强for 内部原理其实是个Iterator迭代器,所以在遍历的过程中,不能对集合中的元素进行增删操作. 格式: for(元素的数据类型 变量 : Collection集合or数组){ } 它用于遍 ...
- 峰Redis学习(4)Redis 数据结构(List的操作)
第四节:Redis 数据结构之List 类型 存储list: ArrayList使用数组方式 LinkedList使用双向链接方式 双向链接表中增加数据 双向链接表中删除数据 存储list常用 ...
- Python类方法、静态方法与实例方法 -----类里面不需要实例化参数 和没带self的函数 调用此函数的方法
来源: https://www.cnblogs.com/blackmatrix/p/5606364.html 静态方法是指类中无需实例参与即可调用的方法(不需要self参数),在调用过程中,无需将类实 ...
- JVM -verbose参数详解(转)
原文 http://www.javaranger.com/archives/367 java -verbose[:class|gc|jni] 在输出设备上显示虚拟机运行信息. java -verbos ...
- centos6.5远程桌面连接(VNC\SPice)
在Linux下用vnc远程桌面,centos中默认没有安装VNC 查询系统是否安装VNC # rpm -q tigervnc tigervnc-server 安装VNC服务 # yum install ...
- IPSec
一. +IPSec(IP Security)Internet 协议安全性 是IFTF制定的为保证在Internet上传送数据的安全保密性能的框架协议 +IPSec包括报文验证头协议AH(协议号51)和 ...
- vue 动态路由 Get传值
main.js //2.配置路由 注意:名字 const routes = [ { path: '/home', component: Home }, { path: '/news', compone ...