/**
* 从远程向本地拷贝文件
* @param remoteUrl 远程共享文件
* @param localFilePath 本机文件
*/
public static void smbCreateLoaclFile(String remoteUrl ,String localFilePath){
InputStream in = null;
OutputStream out =null;
File localFile = null;
SmbFileInputStream smbIn = null;
FileOutputStream fileOut = null;
byte[] sbyte = null;
try{
SmbFile remoteFile = new SmbFile(remoteUrl);
if (remoteFile == null){
FileLogger.error(remoteUrl +" ,共享文件不存!");
return ;
}
String fileName = remoteFile.getName();
localFile = new File(localFilePath + "/" + fileName);
smbIn = new SmbFileInputStream(remoteFile);
in = new BufferedInputStream(smbIn);
fileOut = new FileOutputStream(localFile);
out = new BufferedOutputStream(fileOut);
sbyte = new byte[1024];
while(in.read(sbyte) != -1){
out.write(sbyte);
sbyte = new byte[1024];
}
out.flush();
}catch(Exception e){
FileLogger.error("从远程 " + remoteUrl + "向本地 " + localFilePath + "拷贝文件",e);
}finally{
try{
if(fileOut != null){
fileOut.close();
}
if(smbIn != null){
smbIn.close();
}
if(out != null){
out.close();
}
if(in != null){
in.close();
}
sbyte = null;
localFile = null;
fileOut = null;
smbIn = null;
out = null;
in = null;
}catch(Exception e){
FileLogger.error("从远程 " + remoteUrl + "向本地 " + localFilePath + "拷贝文件 close stream error ",e);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String remoteUrl = "smb://administrator:Platform1@10.112.15.154/EXPORT";
File file=new File("D://saveihc");
File[] files=file.listFiles();
for (File file2 : files) {
if(file2.isFile()){
System.out.println(file2.getAbsolutePath());
smbCreateRemotFile(remoteUrl, file2.getAbsolutePath());
}
}
//String localFilePath = "d:/SAVEIHC/1.txt";
// smbCreateRemotFile(remoteUrl, localFilePath);
}
这段代码在本地测试能存入共享文件夹,可丢上服务器运行就报错
[ CAUSE]: The network name cannot be found.
jcifs.smb.SmbTransport.checkStatus(SmbTransport.java:563)
at jcifs.smb.SmbTransport.send(SmbTransport.java:663)
at jcifs.smb.SmbSession.send(SmbSession.java:238)
at jcifs.smb.SmbTree.treeConnect(SmbTree.java:176)
at jcifs.smb.SmbFile.doConnect(SmbFile.java:911)
at jcifs.smb.SmbFile.connect(SmbFile.java:954)
at jcifs.smb.SmbFile.connect0(SmbFile.java:880)