package com.login.example;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.UnknownHostException;
import jcifs.UniAddress;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
import jcifs.smb.SmbSession;
public class LonginExample {
public static String userName ="administrator";
public static String password ="administrator";
public static String domainIP ="Tiancom.net";
NtlmPasswordAuthentication auth;{
getInParam();
}
private void getInParam(){
try {
UniAddress dc = UniAddress.getByName(domainIP);
System.out.println("UniAddress:"+dc);
auth = new NtlmPasswordAuthentication(domainIP, userName, password);
System.out.println("auth:"+auth.getDomain());
System.out.println("username:"+auth.getUsername());
System.out.println("password:"+auth.getPassword());
SmbSession.logon(dc,auth);
} catch (UnknownHostException e) {
e.printStackTrace();
System.out.println("登录失败!!!");
} catch (SmbException e) {
e.printStackTrace();
System.out.println("无法访问!!!");
}
}
public static void main(String[] args) {
final LonginExample test = new LonginExample();
//单次执行
//从共享目录拷贝文件到本地
test.smbGet("smb://192.168.0.10/share/测试.txt","D:UAPNETFileHYCZ");
//向共享目录上传文件
test.smbPut("smb://192.168.0.10/share","D:/UAPNETFile/测试.txt");
//线程循环执行
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 从共享目录拷贝文件到本地
* */
public void smbGet(String remoteUrl, String localDir) {
InputStream in = null;
OutputStream out = null;
try {
getInParam();
SmbFile remoteFile = new SmbFile(remoteUrl, auth);
if (remoteFile == null) {
System.out.println("共享文件不存在");
return;
}
String fileName = remoteFile.getName();
File localFile = new File(localDir + File.separator + fileName);
in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
out = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] buffer = new byte[1024];
int i = 0;
while ((i = in.read(buffer)) != -1) {
out.write(buffer, 0, i);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.flush();
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 向共享目录上传文件
* */
public void smbPut(String remoteUrl,String localFilePath) {
InputStream in = null;
OutputStream out = null;
try {
File localFile = new File(localFilePath);
String fileName = localFile.getName();
SmbFile remoteFile = new SmbFile(remoteUrl, auth);
//如果文件夹不存在,先建立文件夹
if (!remoteFile.exists()) {
remoteFile.mkdirs();
}
remoteFile = new SmbFile(remoteUrl+fileName, auth);
//如果文件不存在先建立文件
if(!remoteFile.exists()){
remoteFile.createNewFile();
}
in = new BufferedInputStream(new FileInputStream(localFile));
System.out.println("SmbFileOutputStream"+new SmbFileOutputStream(remoteFile));
out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
byte[] buffer = new byte[1024];
while(in.read(buffer)!=-1){
out.write(buffer);
buffer = new byte[1024];
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
一下是转载的代码,没有用域,参考了下
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;
public class ReadShareFile {
public static void main(String[] args) {
new Thread() {
public void run() {
smbGet("smb://Administrator:01@192.168.0.47/share/测试.txt",
"D:UAPNETFile");
smbPut("smb://Administrator:01@192.168.0.47/share",
"D:/远程上传.txt");
System.out.println("操作已经完成");
}
}.start();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//获取远程共享的文件内容
public static void smbPut(String remoteUrl, String localFilePath) {
InputStream in = null;
OutputStream out = null;
try {
File localFile = new File(localFilePath);
String fileName = localFile.getName();
SmbFile remoteFile = new SmbFile(remoteUrl +"/"+ fileName);
in = new BufferedInputStream(new FileInputStream(localFile));
out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
byte[] buffer = new byte[1024];
while (in.read(buffer) != -1) {
out.write(buffer);
buffer = new byte[1024];
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//向远程共享文件夹上传内容
public static void smbGet(String remoteUrl, String localDir) {
InputStream in = null;
OutputStream out = null;
try {
SmbFile remoteFile = new SmbFile(remoteUrl);
if (remoteFile == null) {
System.out.println("共享文件不存在");
return;
}
String fileName = remoteFile.getName();
File localFile = new File(localDir + File.separator + fileName);
in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
out = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] buffer = new byte[1024];
while (in.read(buffer) != -1) {
out.write(buffer);
buffer = new byte[1024];
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}