From 40ccf1f10be91b7d5e45ef348d75acfb3807dc0f Mon Sep 17 00:00:00 2001 From: hxcan <75723435+hxcan@users.noreply.github.com> Date: Wed, 21 Sep 2022 13:08:10 +0800 Subject: [PATCH] Add files via upload --- .../ftpserver/lib/ControlConnectHandler.java | 36 +++++++++---------- .../stupidbeauty/ftpserver/lib/FtpServer.java | 19 ++++++---- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/builtinftp/src/main/java/com/stupidbeauty/ftpserver/lib/ControlConnectHandler.java b/builtinftp/src/main/java/com/stupidbeauty/ftpserver/lib/ControlConnectHandler.java index 7b333c3..e0e1b93 100644 --- a/builtinftp/src/main/java/com/stupidbeauty/ftpserver/lib/ControlConnectHandler.java +++ b/builtinftp/src/main/java/com/stupidbeauty/ftpserver/lib/ControlConnectHandler.java @@ -57,6 +57,7 @@ class ControlConnectHandler private byte[] dataSocketPendingByteArray=null; //!< 数据套接字数据内容 排队。 private String currentWorkingDirectory="/"; //!< 当前工作目录 private int data_port=1544; //!< 数据连接端口。 + private String ip; //!< ip private boolean allowActiveMode=true; //!< 是否允许主动模式。 private File writingFile; //!< 当前正在写入的文件。 private boolean isUploading=false; //!< 是否正在上传。陈欣 @@ -102,16 +103,17 @@ private void receiveDataSocket( ByteBufferList bb) { e.printStackTrace(); } - } //private void receiveDataSocket( ByteBufferList bb) + } // private void receiveDataSocket( ByteBufferList bb) - public ControlConnectHandler(Context context, boolean allowActiveMode, InetAddress host) + public ControlConnectHandler(Context context, boolean allowActiveMode, InetAddress host, String ip) { this.context=context; this.allowActiveMode=allowActiveMode; this.host=host; + this.ip=ip; // Remember ip for data server. - setupDataServer(); // 启动数据传输服务器。 - } + setupDataServer(); // 启动数据传输服务器。 + } /** * 打开指向客户端特定端口的连接。 @@ -386,16 +388,22 @@ else if (command.equals("TYPE")) // 传输类型 else if (command.equals("PASV")) // 被动传输 { setupDataServer(); // 初始化数据服务器。 - - WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); - String ipAddress = Formatter.formatIpAddress(wifiManager.getConnectionInfo().getIpAddress()); - String ip = ipAddress.replace(".", ","); - + String ipAddress = ip; + + + if (ipAddress==null) // Have not set ip. + { + WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); + ipAddress = Formatter.formatIpAddress(wifiManager.getConnectionInfo().getIpAddress()); + } // else // Not set ip. + + String ipString = ipAddress.replace(".", ","); + int port256=data_port/256; int portModule=data_port-port256*256; - String replyString="227 Entering Passive Mode ("+ip+","+port256+","+portModule+") "; // 回复内容。 + String replyString="227 Entering Passive Mode ("+ipString+","+port256+","+portModule+") "; // 回复内容。 Log.d(TAG, "reply string: " + replyString); //Debug. @@ -941,9 +949,6 @@ public void onCompleted(Exception ex) } }); - //发送初始命令: -// send_data "220 \n" - binaryStringSender.sendStringInBinaryMode("220 StupidBeauty FtpServer"); // 发送回复内容。 } //private void handleAccept(final AsyncSocket socket) @@ -958,8 +963,6 @@ private void setupDataServer() data_port=randomIndex; -// try // 绑定端口。 -// { AsyncServer.getDefault().listen(host, data_port, new ListenCallback() { @Override @@ -979,9 +982,6 @@ public void onCompleted(Exception ex) { if(ex != null) { -// 09-07 07:57:47.473 18998 19023 W System.err: java.lang.RuntimeException: java.net.BindException: Address already in use - -// throw new RuntimeException(ex); ex.printStackTrace(); setupDataServer(); // 重新初始化。 diff --git a/builtinftp/src/main/java/com/stupidbeauty/ftpserver/lib/FtpServer.java b/builtinftp/src/main/java/com/stupidbeauty/ftpserver/lib/FtpServer.java index 6913d59..7fd0228 100644 --- a/builtinftp/src/main/java/com/stupidbeauty/ftpserver/lib/FtpServer.java +++ b/builtinftp/src/main/java/com/stupidbeauty/ftpserver/lib/FtpServer.java @@ -31,6 +31,7 @@ public class FtpServer private static final String TAG="FtpServer"; //!< 输出调试信息时使用的标记 private InetAddress host; private int port; + private String ip; //!< ip private boolean allowActiveMode=true; //!< 是否允许主动模式。 private File rootDirectory=null; //!< 根目录。 @@ -55,18 +56,24 @@ public FtpServer(String host, int port, Context context, boolean allowActiveMode } //public FtpServer(String host, int port, Context context, boolean allowActiveMode) public FtpServer(String host, int port, Context context, boolean allowActiveMode, ErrorListener errorListener) + { + this(host, port, context, allowActiveMode, errorListener, null); + } //public FtpServer(String host, int port, Context context, boolean allowActiveMode) + + public FtpServer(String host, int port, Context context, boolean allowActiveMode, ErrorListener errorListener, String externalIp) { this.context=context; this.allowActiveMode=allowActiveMode; this.errorListener=errorListener; // 记录错误事件监听器。 - + this.ip=externalIp; // Remember the external ip. + rootDirectory=context.getFilesDir(); // 默认在家目录下工作。 - - try + + try { this.host = InetAddress.getByName(host); } - catch (UnknownHostException e) + catch (UnknownHostException e) { throw new RuntimeException(e); } @@ -75,7 +82,7 @@ public FtpServer(String host, int port, Context context, boolean allowActiveMode setup(); } //public FtpServer(String host, int port, Context context, boolean allowActiveMode) - + /** * Set user manager. */ @@ -91,7 +98,7 @@ private void setup() @Override public void onAccepted(final AsyncSocket socket) { - ControlConnectHandler handler=new ControlConnectHandler(context, allowActiveMode, host); // 创建处理器。 + ControlConnectHandler handler=new ControlConnectHandler(context, allowActiveMode, host, ip); // 创建处理器。 handler.handleAccept(socket); handler.setRootDirectory(rootDirectory); // 设置根目录。 handler.setEventListener(eventListener); // 设置事件监听器。