Получение NullPointerException с SmbFileInputStream - PullRequest
0 голосов
/ 16 ноября 2018

Попытка изменить Excel, который находится на другой машине.Передавая IP-адрес, имя пользователя, пароль и путь к файлу для доступа и изменения файла, но получая исключение NullPointerException на new SmbFileInputStream(sFile).В чем причина?

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)

Ответы [ 2 ]

0 голосов
/ 16 ноября 2018

Ваша версия 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();
}

Для записи файла, я думаю, вы сможете понять это: -)

0 голосов
/ 16 ноября 2018

Скорее всего, проблема в том, что ваш sFile объект null.

Проверьте путь к файлу, который вы предоставляете.

...