Skip to content

Commit 90c11ac

Browse files
authored
Merge pull request #8 from hxcan/hxcan-patch-1
Add files via upload
2 parents 45275cd + 40ccf1f commit 90c11ac

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

builtinftp/src/main/java/com/stupidbeauty/ftpserver/lib/ControlConnectHandler.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class ControlConnectHandler
5757
private byte[] dataSocketPendingByteArray=null; //!< 数据套接字数据内容 排队。
5858
private String currentWorkingDirectory="/"; //!< 当前工作目录
5959
private int data_port=1544; //!< 数据连接端口。
60+
private String ip; //!< ip
6061
private boolean allowActiveMode=true; //!< 是否允许主动模式。
6162
private File writingFile; //!< 当前正在写入的文件。
6263
private boolean isUploading=false; //!< 是否正在上传。陈欣
@@ -102,16 +103,17 @@ private void receiveDataSocket( ByteBufferList bb)
102103
{
103104
e.printStackTrace();
104105
}
105-
} //private void receiveDataSocket( ByteBufferList bb)
106+
} // private void receiveDataSocket( ByteBufferList bb)
106107

107-
public ControlConnectHandler(Context context, boolean allowActiveMode, InetAddress host)
108+
public ControlConnectHandler(Context context, boolean allowActiveMode, InetAddress host, String ip)
108109
{
109110
this.context=context;
110111
this.allowActiveMode=allowActiveMode;
111112
this.host=host;
113+
this.ip=ip; // Remember ip for data server.
112114

113-
setupDataServer(); // 启动数据传输服务器。
114-
}
115+
setupDataServer(); // 启动数据传输服务器。
116+
}
115117

116118
/**
117119
* 打开指向客户端特定端口的连接。
@@ -386,16 +388,22 @@ else if (command.equals("TYPE")) // 传输类型
386388
else if (command.equals("PASV")) // 被动传输
387389
{
388390
setupDataServer(); // 初始化数据服务器。
389-
390-
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
391-
String ipAddress = Formatter.formatIpAddress(wifiManager.getConnectionInfo().getIpAddress());
392391

393-
String ip = ipAddress.replace(".", ",");
394-
392+
String ipAddress = ip;
393+
394+
395+
if (ipAddress==null) // Have not set ip.
396+
{
397+
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
398+
ipAddress = Formatter.formatIpAddress(wifiManager.getConnectionInfo().getIpAddress());
399+
} // else // Not set ip.
400+
401+
String ipString = ipAddress.replace(".", ",");
402+
395403
int port256=data_port/256;
396404
int portModule=data_port-port256*256;
397405

398-
String replyString="227 Entering Passive Mode ("+ip+","+port256+","+portModule+") "; // 回复内容。
406+
String replyString="227 Entering Passive Mode ("+ipString+","+port256+","+portModule+") "; // 回复内容。
399407

400408
Log.d(TAG, "reply string: " + replyString); //Debug.
401409

@@ -941,9 +949,6 @@ public void onCompleted(Exception ex)
941949
}
942950
});
943951

944-
//发送初始命令:
945-
// send_data "220 \n"
946-
947952
binaryStringSender.sendStringInBinaryMode("220 StupidBeauty FtpServer"); // 发送回复内容。
948953
} //private void handleAccept(final AsyncSocket socket)
949954

@@ -958,8 +963,6 @@ private void setupDataServer()
958963

959964
data_port=randomIndex;
960965

961-
// try // 绑定端口。
962-
// {
963966
AsyncServer.getDefault().listen(host, data_port, new ListenCallback()
964967
{
965968
@Override
@@ -979,9 +982,6 @@ public void onCompleted(Exception ex)
979982
{
980983
if(ex != null)
981984
{
982-
// 09-07 07:57:47.473 18998 19023 W System.err: java.lang.RuntimeException: java.net.BindException: Address already in use
983-
984-
// throw new RuntimeException(ex);
985985
ex.printStackTrace();
986986

987987
setupDataServer(); // 重新初始化。

builtinftp/src/main/java/com/stupidbeauty/ftpserver/lib/FtpServer.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class FtpServer
3131
private static final String TAG="FtpServer"; //!< 输出调试信息时使用的标记
3232
private InetAddress host;
3333
private int port;
34+
private String ip; //!< ip
3435
private boolean allowActiveMode=true; //!< 是否允许主动模式。
3536
private File rootDirectory=null; //!< 根目录。
3637

@@ -55,18 +56,24 @@ public FtpServer(String host, int port, Context context, boolean allowActiveMode
5556
} //public FtpServer(String host, int port, Context context, boolean allowActiveMode)
5657

5758
public FtpServer(String host, int port, Context context, boolean allowActiveMode, ErrorListener errorListener)
59+
{
60+
this(host, port, context, allowActiveMode, errorListener, null);
61+
} //public FtpServer(String host, int port, Context context, boolean allowActiveMode)
62+
63+
public FtpServer(String host, int port, Context context, boolean allowActiveMode, ErrorListener errorListener, String externalIp)
5864
{
5965
this.context=context;
6066
this.allowActiveMode=allowActiveMode;
6167
this.errorListener=errorListener; // 记录错误事件监听器。
62-
68+
this.ip=externalIp; // Remember the external ip.
69+
6370
rootDirectory=context.getFilesDir(); // 默认在家目录下工作。
64-
65-
try
71+
72+
try
6673
{
6774
this.host = InetAddress.getByName(host);
6875
}
69-
catch (UnknownHostException e)
76+
catch (UnknownHostException e)
7077
{
7178
throw new RuntimeException(e);
7279
}
@@ -75,7 +82,7 @@ public FtpServer(String host, int port, Context context, boolean allowActiveMode
7582

7683
setup();
7784
} //public FtpServer(String host, int port, Context context, boolean allowActiveMode)
78-
85+
7986
/**
8087
* Set user manager.
8188
*/
@@ -91,7 +98,7 @@ private void setup()
9198
@Override
9299
public void onAccepted(final AsyncSocket socket)
93100
{
94-
ControlConnectHandler handler=new ControlConnectHandler(context, allowActiveMode, host); // 创建处理器。
101+
ControlConnectHandler handler=new ControlConnectHandler(context, allowActiveMode, host, ip); // 创建处理器。
95102
handler.handleAccept(socket);
96103
handler.setRootDirectory(rootDirectory); // 设置根目录。
97104
handler.setEventListener(eventListener); // 设置事件监听器。

0 commit comments

Comments
 (0)