everyone,I am suffering this problem for 4 days..Help is needed...
I am developing an android program that intends to upload a file to a FTP server.
My developing environment: ubuntu12.04 + eclipse
I want to upload the file in a service. Now, the service can be started successfully. here is the code of the upload method in service class.
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;
}
here is the problems:
test on android device: successful
test on emulator
if the emulator and the FTP server are not in the same computer:successful
if the emulator and the FTP server are in the same computer: fail
so let's focus on this case..and some different options happens
if I connect real IP address(10.238.154.173) exception happens at connect method, here is the 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
if I connect the IP(10.0.2.2) I can connect successful.but the storefile method failed.
I have google it for several days, all failed.
Any suggestion could be appreciated! Thanks!