System.Xml.XmlException:解析时意外的文件结尾

时间:2015-06-05 13:29:34

标签: java xml-parsing jsch stax

我有一个方法processData(),在尝试使用filepath sftp通过jsch发送文件之前获取已完成的put()

class Processer {

public void processData() {

                Path fileToSend = writer.buildFile(data, users, str);
                try {
                    sender.connect();
                    if (sender.send(fileToSend)) {
                    } else {
                        logger.error("Upload Failed");
                    }
                } catch (JSchException e) {
                   //log error
                } finally {
                    sender.dispose();
                }                   
    }

}

class WriterClass { 

   public Path buildFile(Map<String, List<Data>> data, Map<String, User> users) {

        XMLOutputFactory factory = XMLOutputFactory.newInstance();
        XMLStreamWriter writer = null;
        Path filePath = Paths.get(outputDir);
        Path fileDir = null;
        Path file = null;

        try {
            fileDir = Files.createDirectories(filePath);
        } catch (IOException e) {
            logger.error("IO error {}", e);
        }

        try {

            file = Paths.get(fileDir.toString(), date + ".XML");
            writer = new IndentingXMLStreamWriter(factory.createXMLStreamWriter(new FileWriter(file.toFile())));

            writer.writeStartDocument();
            writer.writeStartElement("OPEN");

            //for each entry in set 
            writeDetail(writer, data); //writes character data

            writer.writeEndElement(); 
            writer.writeEndDocument();
            writer.flush();
            writer.close();

        } catch (XMLStreamException e) {
            logger.error(e.getMessage());
        } catch (IOException ex) {
            logger.error(ex.getMessage());
        }

        return file;
    }

}

当文件通过sftp传输时,xml文件在远程位置未完成且无法解析。它在本地路径上显示完整。任何人都可以指出我是否遗漏了任何东西,或writer对象的关闭或冲洗是否错误?

1 个答案:

答案 0 :(得分:0)

我发现没有发送完整文件的原因是因为jsch中count()的{​​{1}}方法正在返回SftpProgressMonitor。由于定期调用false方法,它应该返回true以完成传输。 See Here