使用jcifs 操作共享文件 常見問題


問題描述:使用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);便可以進行文件上傳


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM