Apache Mina Ftplet - onUploadStart未保存文件

时间:2017-12-29 12:15:24

标签: apache mina

当覆盖ftplet的onUploadStart方法以将文件内容存储到数据库中时,上传的文件不会按预期保存在文件系统中。 ftpclient正在接收正确的响应,但文件未保存。

>>> import log
>>> logger = log.setup_logger('root')
>>> logger.debug('debug')
>>> logger.warning('warning')
2017-12-29 12:07:16,561 - WARNING - <stdin> - warning
>>> logger.info('info')

读取的内容存储在数据库中,但在该操作之后,当FTP服务器尝试继续STOR操作时,我得到了NullPointerException:

@Override
public FtpletResult onUploadStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
    log.debug("onUploadStart");

    OutputStream outputStream = new ByteArrayOutputStream();
    DataConnectionFactory connectionFactory = session.getDataConnection();
    session.write(new DefaultFtpReply(150, "Getting data connection."));
    try {
        DataConnection dataConnection = connectionFactory.openConnection();

        if (dataConnection == null) {
            session.write(new DefaultFtpReply(425, "Cannot open data connection."));
            return FtpletResult.SKIP;
        }
        dataConnection.transferFromClient(session, outputStream);

        String uploadedContent = outputStream.toString();

        insertInDatabase(uploadedContent);
        session.write(new DefaultFtpReply(226, "Data transfer okay."));
    } catch (Exception e) {
        e.printStackTrace();
        session.write(new DefaultFtpReply(551, "Data transfer failed."));
        return FtpletResult.SKIP; 
    } finally {
        connectionFactory.closeDataConnection();
        outputStream.close();

    }
    return FtpletResult.SKIP; 
}

由于

1 个答案:

答案 0 :(得分:0)

我遇到了同样的异常,包括你的代码和ftpserver-core-1.1.1 - 但是只有当我离开onUploadStart()时 - 没有FtpletResult.SKIP的方法。因此,我不确定为什么会这样。

您是否可以选择覆盖onUploadEnd(),从上传的文件中读取数据并在之后删除它?这应该避免这个问题。