java - 使用 SmbFileInputStream 获取 NullPointerException

标签 java nullpointerexception jcifs

尝试修改另一台计算机上的 Excel。传递 IP 地址、用户名、密码和文件路径来访问和修改文件,但在 new SmbFileInputStream(sFile) 处收到 NullPointerException。这是什么原因?

NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domin", "username", "password");
                String path = "smb:\\\\<IPaddress>\\C$\\<FolderName>\\File%20-%20Input.xlsx";
                SmbFile sFile = new SmbFile(path, auth);



                try {
                    SmbFileInputStream inputStream = new SmbFileInputStream(sFile);
                    Workbook workbook = WorkbookFactory.create(inputStream);

                    Sheet sheet = workbook.getSheetAt(0);
                    int rowCount = sheet.getLastRowNum(),i=0;
                    Cell cell;
                    for(ForemostReservedDataDO obj : unsavedRecords){
                        i++;
                        Row row = sheet.createRow(rowCount+i);
                        cell = row.createCell(0);
                        cell.setCellValue(obj.getPolicyNum());
                        cell = row.createCell(1);
                        cell.setCellValue("Recreational Value");
                    }
                    inputStream.close();

                    SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
                    workbook.write(sfos);
                    workbook.close();
                    sfos.close();

                } catch (EncryptedDocumentException | InvalidFormatException | IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

全栈

SEVERE: Servlet.service() for servlet [spring-dispatcher] in context with path [/Foremost] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
    at jcifs.smb.ServerMessageBlock.writeString(ServerMessageBlock.java:213)
    at jcifs.smb.ServerMessageBlock.writeString(ServerMessageBlock.java:202)
    at jcifs.smb.SmbComNTCreateAndX.writeBytesWireFormat(SmbComNTCreateAndX.java:170)
    at jcifs.smb.AndXServerMessageBlock.writeAndXWireFormat(AndXServerMessageBlock.java:101)
    at jcifs.smb.AndXServerMessageBlock.encode(AndXServerMessageBlock.java:65)
    at jcifs.smb.SmbTransport.doSend(SmbTransport.java:439)
    at jcifs.util.transport.Transport.sendrecv(Transport.java:67)
    at jcifs.smb.SmbTransport.send(SmbTransport.java:655)
    at jcifs.smb.SmbSession.send(SmbSession.java:238)
    at jcifs.smb.SmbTree.send(SmbTree.java:119)
    at jcifs.smb.SmbFile.send(SmbFile.java:775)
    at jcifs.smb.SmbFile.open0(SmbFile.java:989)
    at jcifs.smb.SmbFile.open(SmbFile.java:1006)
    at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:73)
    at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:65)
    at com.Foremost.Controllers.DataDownController.saveReservedData(DataDownController.java:217)

最佳答案

您的 JCIFS 版本似乎已过时且与远程系统不兼容。升级到最新的 JCIFS(当前版本:2.1.3, https://github.com/codelibs/jcifs )或 jcifs-ng ( https://github.com/AgNO3/jcifs-ng ),链接的 JCIFS 现在是其分支。

以下是一些有关如何使用 jcifs-ng 通过 SMB 读取文件的示例代码:

String fileUrl = "smb://netserver/some/path/to/file.xls";

Properties cifsProps = new Properties();
cifsProps.setProperty("jcifs.smb.client.domain", "my.domain.int");
cifsProps.setProperty("jcifs.smb.client.username", USER_NAME);
cifsProps.setProperty("jcifs.smb.client.password", PASSWORD);

Configuration config = new PropertyConfiguration(cifsProps);
BaseContext context = new BaseContext(config);
SmbResource resource = context.get(fileUrl);

if (!(resource instanceof SmbFile)) {
    throw new CIFSException("File URL does not point to a file on a network share");
}

try (InputStream in = ((SmbFile) resource).getInputStream()) {
    // TODO read from in
} finally {
    context.close();
}

对于编写文件,我想你能够弄清楚:-)

关于java - 使用 SmbFileInputStream 获取 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53335868/

相关文章:

java - Swagger Codegen CLI Java 客户端 - 如何正确使用它

Android:在 android.app.ActivityThread$PackageInfo$ServiceDispatcher.doConnected(ActivityThread.java:1012) 出现 NullPointerException

java - toString 处的空指针异常错误

android - Android 无法创建 Samba 目录

java - spring-mvc @Size 注解无法正确解析错误消息

java - 为什么 Google Guice 无法初始化 i18n 对象并给我 null

java - 标记未显示在当前位置上

在 JDBC 中获取游标时出现 java.lang.NullPointerException

java - 使用 Apache Commons HttpClient 上传 SmbFile

java - jcifs.smb.SmbException : The parameter is incorrect error- Login form not for entering credential+NTLMv1 request