問題描述:使用jcifs jar包操作共享文件,將本地文件上傳至服務器共享區遇見Exception:jcifs.smb.SmbException: The network name cannot be found. 但是同樣的代碼將文件上傳至虛擬機共享區就可以。 本機操作系統:win10,服務器:winserver 2012,虛擬機:winserver 2008
代碼如下:
InputStream in = null;
OutputStream out = null;
//本地文件位置
File localFile = new File("F:"+File.separator+"test"+File.separator+"cc.jpg");
String fileName = localFile.getName();
//存放文件的共享文件夾目錄
String remotePhotoUrl = "smb://192.168.56.142/Windchill/test";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", "winserver", "!qaz2wsx");
SmbFile remoteFile = new SmbFile(remotePhotoUrl+File.separator+fileName,auth);
//建立連接
remoteFile.connect();
in = new BufferedInputStream(new FileInputStream(localFile));
out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
byte[] buffer = new byte[2048];
int len = 0;
while((len = in.read(buffer, 0, buffer.length))!=-1) {
out.write(buffer, 0, len);
}
out.flush();
out.close();
in.close();
問題原因:win10 與winserver 2012 文件分隔符有區別
解決辦法: SmbFile remoteFile = new SmbFile(remotePhotoUrl+File.separator+fileName,auth);
改為:SmbFile remoteFile = new SmbFile(remotePhotoUrl+"/"+fileName,auth);便可以進行文件上傳