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 ...
随机推荐
- 写了一个RenderInBackground的脚本
某主管希望write节点有多线程渲染的功能,而nuke中的render in background功能恰恰可以多个渲染任务同时执行,于是我考虑使用这个方法来实现. 调 试过程中发现renderinba ...
- HTTP协议之chunk介绍
http chunked 当客户端向服务器请求一个静态页面或者一张图片时,服务器可以很清楚的知道内容大小,然后通过Content-Length消息首部字段告诉客户端需要接收多少数据.但是如果是动态页面 ...
- webGL之three.js入门1
开场白 最近开始学前端,看了极客学院的前端教学视频,其实有C++或者java基础的人学前端还是很快的.但是html的标签和CSS的样式还是得多code才能熟练,熟能生巧,学以致用. 还在看js,因为有 ...
- Azure SQL Database (26) 使用Query Store对Azure SQL Database监控
<Windows Azure Platform 系列文章目录> 我们在使用Azure SQL Database的时候,需要对数据库的性能进行监控,这时候就可以有两种方法: 1.第一种方法, ...
- java中经常使用的Swing组件总结
1.按钮(Jbutton) Swing中的按钮是Jbutton,它是javax.swing.AbstracButton类的子类,swing中的按钮可以显示图像,并且可以将按钮设置为窗口的默认图标,而且 ...
- centos 7 免密登录
本文转载自:https://www.cnblogs.com/hobinly/p/6039844.html 环境示例 Centos7 192.168.1.101 master Centos7 192. ...
- Qt学习——QListWidget控件的使用
转载:GDUTLYP Qt提供QListWidget类列表框控件用来加载并显示多个列表项.QListWidgetItem类就是列表项类. 一般列表框控件中的列表项有两种加载方式: 一种是由用户手动添加 ...
- 身高安排方法(基础dfs)
P1085 时间限制: 0 Sec 内存限制: 128 MB提交: 64 解决: 44[提交][状态][讨论版][命题人:外部导入] 题目描述 Matrix67发现身高接近的人似乎更合得来.Mat ...
- [转][CEF]自动播放视频
1.CEF 默认不支持 mp4,可以在网上下载热心网友提供的修改版. 查看支持的特性的方法:在 CEF 中访问网页 :http://html5test.com/ 2. 打开网页后视频自动播放: mut ...
- docker下centos安装ping命令
https://blog.csdn.net/king_gun/article/details/78423115 [问题] 从docker hub上拉取到则镜像centos:6.7在执行ping命令是报 ...