java - 通过FTPS协议(protocol)发送文件到服务器

标签 java android ftps

我正在创建一个生成 CSV 文件和一些 PDF 的应用程序。我希望我的应用程序通过 FTPS 协议(protocol)将这些文件发送到服务器。

我正在使用 Apache Commons Net FTP 库,当我未选中“使用 PORT P 时需要数据连接上的 TLS session 恢复”时,它工作得很好,但由于我启用了它,所以我无法发送我的文件。 出现错误: 450 数据连接的 TLS session 尚未恢复或 session 与控制连接不匹配

经过对这个网站的一些研究,我重写了 _prepareDataSocket_ 以解决这个问题,但现在它只是在服务器上创建空文件。

这是我的重写函数:

@Override
    protected void _prepareDataSocket_(final Socket socket) throws IOException {
        if (socket instanceof SSLSocket) {
            // Control socket is SSL
            final SSLSession session = ((SSLSocket) _socket_).getSession();
            if (session.isValid()) {
                final SSLSessionContext context = session.getSessionContext();
                try {
                    final Field sessionHostPortCache = context.getClass().getDeclaredField("sessionHostPortCache");
                    sessionHostPortCache.setAccessible(true);
                    final Object cache = sessionHostPortCache.get(context);
                    final Method method = cache.getClass().getDeclaredMethod("put", Object.class, Object.class);
                    method.setAccessible(true);
                    method.invoke(cache, String
                            .format("%s:%s", socket.getInetAddress().getHostName(), String.valueOf(socket.getPort()))
                            .toLowerCase(Locale.ROOT), session);
                    method.invoke(cache, String
                            .format("%s:%s", socket.getInetAddress().getHostAddress(), String.valueOf(socket.getPort()))
                            .toLowerCase(Locale.ROOT), session);
                } catch (NoSuchFieldException e) {
                    throw new IOException(e);
                } catch (Exception e) {
                    throw new IOException(e);
                }
            } else {
                throw new IOException("Invalid SSL Session");
            }
        }
    }

这就是 FileZilla 服务器显示的内容: FileZilla Response

最佳答案

关于java - 通过FTPS协议(protocol)发送文件到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56563779/

相关文章:

磁盘使用情况的 Java 分析

java - 在 Java 中停止 ArrayList 的迭代

java - Android从restful服务读取json

android - SimpleCursorAdapter 的 onClick

java - 将 Java Apache FTPClient 用于 FTP TLS 时获取 "Remote host closed connection during handshake"

java - 使用 spring mongoTemplate 进行条件/最大 mongo 查询

java - SurfaceView 的 setTransform()

android - 上传的 apk 中的权限多于 list 文件中的权限

linux - vsftpd 不在 netstat 中