使用jcifs 操作共享文件 常见问题

在使用jcifs Java库上传本地文件到Windows Server 2012共享目录时遇到'SmbException: The network name cannot be found.'的问题。原因是Win10和WinServer 2012的文件路径分隔符不同。解决方案是将路径分隔符从File.separator改为'/',即SmbFile构造函数中的路径字符串修改为'SmbFile remoteFile = new SmbFile(remotePhotoUrl+"/"+fileName,auth);'" 130314325,10203170,C++指针与引用练习:判断、单选题解析,"['C++', '算法']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

转载于:https://www.cnblogs.com/Peacoke/p/8820665.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值