我正在开发一个打算将文件上传到FTP服务器的android程序。
我的开发环境:ubuntu12.04 + eclipse
我想在服务中上传文件。现在,该服务可以成功启动。 这是服务类中上传方法的代码。
public String ftpUpload(String url, String port, String username,String password,
String remotePath, String filePath,String fileName) {
FTPClient ftpClient = new FTPClient();
FileInputStream fis = null;
String returnMessage = "0";
try{
System.out.println("我看你能不能连接上!!!!");
ftpClient.connect(url,21);
System.out.println("我草还真能连上!!!!!!");
int reply = ftpClient.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)){
ftpClient.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
boolean loginResult = ftpClient.login(username, password);
int returnCode = ftpClient.getReplyCode();
Log.v("returnCode",""+returnCode);
System.out.println(loginResult);
if(loginResult && FTPReply.isPositiveCompletion(returnCode)){
ftpClient.listFiles("");
ftpClient.makeDirectory(remotePath);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.changeWorkingDirectory(remotePath);
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("UTF-8");
fis = new FileInputStream(filePath+"/"+fileName);
ftpClient.enterLocalPassiveMode();
System.out.println("last step!!!!!!");
ftpClient.storeFile("IwantYou", fis);
returnMessage = "1";
}
else
returnMessage = "0";
}
catch (IOException e){
e.printStackTrace();
throw new RuntimeException("擦,还真没连上!!!",e);
}
finally{
try{
ftpClient.disconnect();
}
catch(IOException e){
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
return returnMessage;
}
这是问题所在:
在Android设备上测试:成功
在模拟器上测试
如果模拟器和FTP服务器不在同一台计算机上:成功
如果模拟器和FTP服务器位于同一台计算机上:失败
所以让我们关注这个案例......并且会发生一些不同的选择
如果我连接真实IP地址(10.238.154.173),则发生异常 connect方法,这里是logcat:
W/System.err( 1074): org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
W/System.err( 1074): at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:313)
W/System.err( 1074): at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:290)
W/System.err( 1074): at org.apache.commons.net.ftp.FTP._connectAction_(FTP.java:392)
W/System.err( 1074): at org.apache.commons.net.ftp.FTPClient._connectAction_(FTPClient.java:764)
W/System.err( 1074): at org.apache.commons.net.SocketClient.connect(SocketClient.java:169)
W/System.err( 1074): at org.apache.commons.net.SocketClient.connect(SocketClient.java:189)
W/System.err( 1074): at com.example.hehe.FirstService.ftpUpload(FirstService.java:64)
W/System.err( 1074): at com.example.hehe.FirstService$1.run(FirstService.java:36)
W/System.err( 1074): at java.lang.Thread.run(Thread.java:841)
I/System.out( 1074): something is wrong!
W/System.err( 1074): java.lang.RuntimeException: 擦,还真没连上!!!
W/System.err( 1074): at com.example.hehe.FirstService.ftpUpload(FirstService.java:94)
W/System.err( 1074): at com.example.hehe.FirstService$1.run(FirstService.java:36)
W/System.err( 1074): at java.lang.Thread.run(Thread.java:841)
W/System.err( 1074): org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
W/System.err( 1074): at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:313)
W/System.err( 1074): at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:290)
W/System.err( 1074): at org.apache.commons.net.ftp.FTP._connectAction_(FTP.java:392)
W/System.err( 1074): at org.apache.commons.net.ftp.FTPClient._connectAction_(FTPClient.java:764)
W/System.err( 1074): at org.apache.commons.net.SocketClient.connect(SocketClient.java:169)
W/System.err( 1074): at org.apache.commons.net.SocketClient.connect(SocketClient.java:189)
W/System.err( 1074): at com.example.hehe.FirstService.ftpUpload(FirstService.java:64)
W/System.err( 1074): ... 2 more
如果我连接IP(10.0.2.2),我可以连接成功。但是 storefile方法失败。
我已经谷歌了几天,都失败了。
任何建议都值得赞赏!谢谢!