Read remote file in java which needs username and password

前端 未结 4 656
南旧
南旧 2020-11-29 09:58

I am trying to read a remote file in java

File f = new File(\"//192.168.1.120/home/hustler/file.txt\");

The remote machine needs a Username

4条回答
  •  攒了一身酷
    2020-11-29 10:29

    Another alternative with jCIFS you can easily specify authentication parameters:

    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domain", "user", "password"); // Authentication info here, domain can be null
    try (InputStream is = new SmbFile("smb://192.168.1.120/home/hustler/file.txt", auth).getInputStream()) {
        // Read from 'is' ...
    } catch (IOException e) {
        // Handle IOException
    }
    

提交回复
热议问题