一、出错环境:
利用JAVA 通过 smb 向远程服务器(WINDOWS 2008)上传文件时报错
jcifs.smb.SmbAuthException: Access is denied.
at jcifs.smb.SmbTransport.checkStatus(SmbTransport.java:571)
二、原因:
我使用的远程服务器用户的权限不足。
三、处理方法:
1、调整用户权限为管理员,我报错的原因,主要就是当前用户“隶属于”之前有三个“Administrators”\"ora_dba"\"users",其实只保留Administrator 即可。如下图:
2、设置远程访问的文件夹为共享、完全控制,操作如下图。

四、java smb 集群上传示例
package common;
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.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
/**
*
* @author zby at 20130710
*
*/
public class Smbtest {
/**
* 从局域网中共享文件中得到文件并保存在本地磁盘上
* @param remoteUrl 共享电脑路径 如:smb://admin:sx8165@192.168.0.6/D$/DMSFILE/SPECIALPRODUCTDEMAND/18.jpg , smb为共享文件
* 注:如果一直出现连接不上,有提示报错,并且错误信息是 用户名活密码错误 则修改共享机器的文件夹选项 查看 去掉共享简单文件夹的对勾即可。
* @param localDir 本地路径 如:D:/
*/
public static void smbGet(String remoteUrl,String localDir){
InputStream in = null;
OutputStream out = null;
try {
SmbFile smbFile = new SmbFile(remoteUrl);
String fileName = smbFile.getName();
File localFile = new File(localDir+File.separator+fileName);
in = new BufferedInputStream(new SmbFileInputStream(smbFile));
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();
}
}
}
/**
* 把本地磁盘中的文件上传到局域网共享文件下
* @param remoteUrl 共享电脑路径 如:smb://admin:sx8165@192.168.0.6/D$/DMSFILE/SPECIALPRODUCTDEMAND/
* @param localFilePath 本地路径 如:D:/
*/
public static void smbPut(String remoteUrl,String localFilePath){
InputStream in = null;
OutputStream out = null;
try {
File localFile = new File(localFilePath);
String fileName = localFile.getName();
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("192.168.0.6","admin","sx8165");
SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName,auth);//方法一
//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 main(String[] args) {
smbPut("smb://admin:sx8165@192.168.0.6/D$/DMSFILE/SPECIALPRODUCTDEMAND/", "D:/18.jpg");
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.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
/**
*
* @author zby at 20130710
*
*/
public class Smbtest {
/**
* 从局域网中共享文件中得到文件并保存在本地磁盘上
* @param remoteUrl 共享电脑路径 如:smb://admin:sx8165@192.168.0.6/D$/DMSFILE/SPECIALPRODUCTDEMAND/18.jpg , smb为共享文件
* 注:如果一直出现连接不上,有提示报错,并且错误信息是 用户名活密码错误 则修改共享机器的文件夹选项 查看 去掉共享简单文件夹的对勾即可。
* @param localDir 本地路径 如:D:/
*/
public static void smbGet(String remoteUrl,String localDir){
InputStream in = null;
OutputStream out = null;
try {
SmbFile smbFile = new SmbFile(remoteUrl);
String fileName = smbFile.getName();
File localFile = new File(localDir+File.separator+fileName);
in = new BufferedInputStream(new SmbFileInputStream(smbFile));
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();
}
}
}
/**
* 把本地磁盘中的文件上传到局域网共享文件下
* @param remoteUrl 共享电脑路径 如:smb://admin:sx8165@192.168.0.6/D$/DMSFILE/SPECIALPRODUCTDEMAND/
* @param localFilePath 本地路径 如:D:/
*/
public static void smbPut(String remoteUrl,String localFilePath){
InputStream in = null;
OutputStream out = null;
try {
File localFile = new File(localFilePath);
String fileName = localFile.getName();
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("192.168.0.6","admin","sx8165");
SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName,auth);//方法一
//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 main(String[] args) {
smbPut("smb://admin:sx8165@192.168.0.6/D$/DMSFILE/SPECIALPRODUCTDEMAND/", "D:/18.jpg");
smbGet("smb://admin:sx8165@192.168.0.6/D$/DMSFILE/SPECIALPRODUCTDEMAND/18.jpg", "D:/load");
}
}
}
}