有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用jcifs读取文件的最简单方法

我正在尝试使用外部jcifs library从网络共享读取文件。我能找到的大多数用于读取文件的示例代码都非常复杂,可能是不必要的。我找到了一种将写入文件的简单方法,如下所示。有没有使用类似语法读取文件的方法

SmbFile file= null;
try {
    String url = "smb://"+serverAddress+"/"+sharename+"/TEST.txt";
    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, username, password);
    file = new SmbFile(url, auth);
    SmbFileOutputStream out= new SmbFileOutputStream(file);
    out.write("test string".getBytes());
    out.flush();
    out.close();
} catch(Exception e) {
    JOptionPane.showMessageDialog(null, "ERROR: "+e);
}

共 (3) 个答案

  1. # 1 楼答案

        try {
            String url = "smb://" + serverAddress + "/" + sharename + "/test.txt";
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(DOMAIN, USER_NAME, PASSWORD);
            String fileContent = IOUtils.toString(new SmbFileInputStream(new SmbFile(url, auth)), StandardCharsets.UTF_8.name());
            System.out.println(fileContent);
        } catch (Exception e) {
            System.err.println("ERROR: " + e.getMessage());
        }
    
  2. # 2 楼答案

    我可以使用以下内容阅读一些pdf文件:

        private final Singleton<CIFSContext> contextoDdetran = new Singleton<>() {
            @Override
            public CIFSContext inicializar() {
                NtlmPasswordAuthenticator autenticador = new NtlmPasswordAuthenticator(smbDomain, smbUser, smbPassword);
                return SingletonContext.getInstance().withCredentials(autenticador);
            }
        };
    
        public byte[] readSmbFile(String fileName) {
            try {
                SmbFile file = new SmbFile(fileName, this.contextoDdetran.get());
                return file.getInputStream().readAllBytes();
            } catch(Exception e) {
                final String msgErro = String.format("Error reading file '%s': %s", fileName, e.getMessage());
                logger.error(msgErro, e);
                throw new IllegalStateException(msgErro);
            }
        }
    
  3. # 3 楼答案

    SmbFile file = null;
    byte[] buffer = new byte[1024];
    try {
        String url = "smb://"+serverAddress+"/"+sharename+"/TEST.txt";
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, username, password);
        file = new SmbFile(url, auth);
        try (SmbFileInputStream in = new SmbFileInputStream(file)) {
            int bytesRead = 0;
            do {
                bytesRead = in.read(buffer)
                // here you have "bytesRead" in buffer array
            } 
            while (bytesRead > 0);
        }
    } catch(Exception e) {
        JOptionPane.showMessageDialog(null, "ERROR: "+e);
    }
    

    或者更好,假设您正在使用Java SDK中的BufferedReader处理文本文件:

    try (BufferedReader reader = new BufferedReader(new InputStreamReader(new SmbFileInputStream(file)))) {
        String line = reader.readLine();
        while (line != null) {
            line = reader.readLine();
        }
    }
    

    写下:

    try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new SmbFileOutputStream(file)))) {
        String toWrite = "xxxxx";
        writer.write(toWrite, 0, toWrite.length());
    }