我目前正在尝试开发一个可穿戴应用,该应用可以通过FTP将带有加速度计和陀螺仪数据的文件上传到我的PC。
我使用以下代码在Eclipse中的PC上运行FTP服务器:
public static void main(String[] args) throws FtpException {
ListenerFactory listenerFactory = new ListenerFactory();
listenerFactory.setPort(2221);
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
UserManager userManager = userManagerFactory.createUserManager();
BaseUser user = new BaseUser();
user.setName("Anonymous");
user.setPassword("");
user.setHomeDirectory("C:\\temp");
List<Authority> authorities = new ArrayList<Authority>();
authorities.add(new WritePermission());
user.setAuthorities(authorities);
userManager.save(user);
FtpServerFactory factory = new FtpServerFactory();
factory.setUserManager(userManager);
factory.addListener("default", listenerFactory.createListener());
FtpServer server = factory.createServer();
server.start();
}
我正在使用此方法连接到FTP服务器:
public void connectToFTPServer() throws IOException {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Log.i("CONNECT FTP SERVER", "Connecting to FTP Server");
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalActiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
}
这种上传传感器数据的方法:
public void uploadFileTest()throws IOException {
try
{
if (ftpClient.login("Anonymous", ""))
{
String data = this.getFilesDir() + "/" + "SensorData.txt";
FileInputStream in = new FileInputStream(new File(data));
boolean result = ftpClient.storeFile("SensorData.txt", in);
in.close();
if (result) Log.v("upload result", "succeeded");
ftpClient.logout();
ftpClient.disconnect();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
可穿戴设备在Android Studio中的仿真器上运行,即使好像建立了FTP服务器连接,当我尝试上传传感器数据时,我也在FTP服务器上遇到以下错误消息。
[main] INFO org.apache.ftpserver.impl.DefaultFtpServer - FTP server started
[NioProcessor-3] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - CREATED
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - OPENED
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 220 Service ready for new user.
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - RECEIVED: USER Anonymous
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 331 User name okay, need password for Anonymous.
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - RECEIVED: PASS *****
[pool-3-thread-1] INFO org.apache.ftpserver.command.impl.PASS - Login success - Anonymous
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 230 User logged in, proceed.
[pool-3-thread-2] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - RECEIVED: TYPE I
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 200 Command TYPE okay.
[NioProcessor-4] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - CREATED
[pool-3-thread-2] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - OPENED
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 220 Service ready for new user.
[pool-3-thread-2] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - RECEIVED: USER Anonymous
[pool-3-thread-2] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 331 User name okay, need password for Anonymous.
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - RECEIVED: PASS *****
[pool-3-thread-1] INFO org.apache.ftpserver.command.impl.PASS - Login success - Anonymous
[pool-3-thread-2] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 230 User logged in, proceed.
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - RECEIVED: TYPE I
[pool-3-thread-2] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 200 Command TYPE okay.
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - RECEIVED: PORT 10,0,2,15,157,216
[pool-3-thread-2] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 200 Command PORT okay.
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - RECEIVED: STOR SensorData.txt
[pool-3-thread-1] WARN org.apache.ftpserver.impl.IODataConnectionFactory - FtpDataConnection.getDataSocket()
java.net.SocketException: Network is unreachable: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at org.apache.ftpserver.impl.IODataConnectionFactory.createDataSocket(IODataConnectionFactory.java:312)
at org.apache.ftpserver.impl.IODataConnectionFactory.openConnection(IODataConnectionFactory.java:258)
at org.apache.ftpserver.command.impl.STOR.execute(STOR.java:133)
at org.apache.ftpserver.impl.DefaultFtpHandler.messageReceived(DefaultFtpHandler.java:211)
at org.apache.ftpserver.listener.nio.FtpHandlerAdapter.messageReceived(FtpHandlerAdapter.java:62)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$TailFilter.messageReceived(DefaultIoFilterChain.java:858)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:542)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.access$1300(DefaultIoFilterChain.java:48)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.messageReceived(DefaultIoFilterChain.java:947)
at org.apache.ftpserver.listener.nio.FtpLoggingFilter.messageReceived(FtpLoggingFilter.java:85)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:542)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.access$1300(DefaultIoFilterChain.java:48)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.messageReceived(DefaultIoFilterChain.java:947)
at org.apache.mina.core.filterchain.IoFilterEvent.fire(IoFilterEvent.java:74)
at org.apache.mina.filter.logging.MdcInjectionFilter.filter(MdcInjectionFilter.java:135)
at org.apache.mina.filter.util.CommonEventFilter.messageReceived(CommonEventFilter.java:70)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:542)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.access$1300(DefaultIoFilterChain.java:48)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.messageReceived(DefaultIoFilterChain.java:947)
at org.apache.mina.filter.codec.ProtocolCodecFilter$ProtocolDecoderOutputImpl.flush(ProtocolCodecFilter.java:398)
at org.apache.mina.filter.codec.ProtocolCodecFilter.messageReceived(ProtocolCodecFilter.java:234)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:542)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.access$1300(DefaultIoFilterChain.java:48)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.messageReceived(DefaultIoFilterChain.java:947)
at org.apache.mina.core.filterchain.IoFilterEvent.fire(IoFilterEvent.java:74)
at org.apache.mina.core.session.IoEvent.run(IoEvent.java:63)
at org.apache.mina.filter.executor.OrderedThreadPoolExecutor$Worker.runTask(OrderedThreadPoolExecutor.java:770)
at org.apache.mina.filter.executor.OrderedThreadPoolExecutor$Worker.runTasks(OrderedThreadPoolExecutor.java:762)
at org.apache.mina.filter.executor.OrderedThreadPoolExecutor$Worker.run(OrderedThreadPoolExecutor.java:704)
at java.lang.Thread.run(Unknown Source)
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 150 File status okay; about to open data connection.
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 425 Can't open data connection.
[pool-3-thread-2] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - CLOSED
请指导我,我已经寻找了可能的解决方法达数小时之久。我已经允许该端口进入Windows防火墙,并尝试了其他方法。
谢谢大家。
答案 0 :(得分:0)
活动模式可以在移动/可穿戴设备上运行的可能性几乎为零。处于活动模式的FTP需要启用到设备的传入连接,而这又需要在客户端(或客户端网络)上配置防火墙和NAT。通常,移动设备上的应用程序位于众多此类防火墙和NAT之后(既在设备本身上,也主要在运营商网络内)。而且您对这些没有任何控制。
改为使用被动模式。
有关详细信息,请阅读我在network configuration needed for FTP active and passive modes上的文章。
我知道您评论了,被动模式也不适合您。但这不是尝试活动模式的原因。您应该尝试修复被动模式(这种方式更可能起作用),而不是浪费时间尝试修复主动模式。因此,请考虑发布有关被动模式的新问题,其中包含特定的错误消息和您拥有的其他任何调试信息。