How to read a file from remote system using java?

后端 未结 8 1108
忘了有多久
忘了有多久 2020-12-06 05:18

I have a file copied in one computer and I need to access the file from other computer. I am not sure, which protocol or which technology to use for this? Please provide me

8条回答
  •  死守一世寂寞
    2020-12-06 05:59

    You can read from remote and write to remote using jcifs-1.3.15.jar jar in java but first you need to share location from remote system then it's possible.

    try{
                String strLine="";    
                NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("WORKGROUP", "username", "passwd"); // Authentication info here, domain can be null
        //        try (InputStream is = new SmbFile("smb://DESKTOP-0xxxx/usr/local/cache/abc.txt", auth).getInputStream()) {
                try (InputStream is = new SmbFile("smb://xx.xx.xx.xxx/dina_share/abc.txt", auth).getInputStream()) {
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                while ((strLine = br.readLine()) != null) {
                    System.out.println(strLine);
                }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String smbURL="smb://xx.xx.xx.xxx/dina_share/abcOther.txt";
                SmbFileOutputStream fos = new SmbFileOutputStream(new SmbFile(smbURL,auth));
                byte bytes[]="Wellcome to you".getBytes();
                fos.write(bytes);
            }catch(Exception e){
                e.printStackTrace();
            }
    

提交回复
热议问题