问题描述:使用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);便可以进行文件上传