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 ...
随机推荐
- 【巷子】---react-redux---【react】
一.Redux与组件 react-redux是一个第三方插件使我们在react上更方便的来使用redux这个数据架构 React-Redux提供connect方法,用于从UI组件生成容器组件,conn ...
- a:hover应用精粹
原本想把题目叫做“纯CSS相册2”的,但在实现过程中试验了许多东西,干脆全部写出来分享了.大家知道,能兼容IE6的具有动态切换能力的CSS属性也只有hover伪类了,但hover伪类在IE仅对链接生效 ...
- 【RPC】使用Hessian构建RPC的简单示例
服务接口和实现 public interface HelloService { // 服务方法 String sayHello(String name); } public class HelloSe ...
- 易出错的bug避免
1:for(var i:int=0;i<p.numChildren;i++) { p.removeChildAt(i); } 或 for(var i:int=0;i& ...
- Oracle事务隔离级别处理差异
Oracle事务隔离是事务读操作不同程度的数据隔离,分为READ_UNCOMMITTED.READ_COMMITTED(默认).SERIALIZABLE. Oracle事务隔离级别SERIALIZAB ...
- Java学习——Applet菜单
程序功能:在窗口中添加菜单栏,在菜单栏添加菜单项,并添加下拉菜单和 2 级菜单,通过选择菜单项可以执行不同操作,生成如下图所示窗口. package cys; import java.awt.*; i ...
- Android 使用自定义字体
整个项目要使用第三方字体首先将字体文件放到assets文件夹下 因为整个项目要用第三方字体这里我重写了 TextView Button EditText 三个控件 以TextView 为例代码如下 ...
- linux system()函数详解
system(3) - Linux man page Name system - execute a shell command Synopsis #include <stdlib.h> ...
- sas基础系列(2)-时间差精度获取
data a; interval='month'; start='14FEB2013'd; end='13MAR2013'd; months_default=intck(interval, start ...
- CentOS之文档的压缩与打包
.rar压缩文件linux中不识别,.zip在windows和Linux中动能使用. .gz:由gzip压缩工具压缩的文件 .bz2:bzip2压缩工具压缩的文件 .tar:由tar打包程序打包的文件 ...