-1

I am trying to upload a picture from a Samsung to an FTP server using the Apache FTPClient. But it does not seem to work. I don't know why, the code is fine. I am trying to choose an image to upload it on the server, the selectedImage variable is the complete path of the picture file.

FTPClient ftpClient = new FTPClient();
ftpClient.connect(InetAddress.getByName("ftpserver"));
ftpClient.login("user", "password");
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

FileInputStream buffIn = null;
buffIn = new FileInputStream(new File(getRealPathFromURI(selectedImage)));
ftpClient.enterLocalPassiveMode();
link = "http://ftpname/home/user/public_html/image/ayri.jpg";
ftpClient.storeFile(link, buffIn);
buffIn.close();
ftpClient.logout();
ftpClient.disconnect();
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992

2 Answers2

0

The first argument of FTPClient.storeFile is a path to file to store the uploaded data to, not an HTTP URL.

What that path is, depends on how your FTP server is configured.

It can be an absolute path like:

link = "/home/user/public_html/image/ayri.jpg";

Or if the user account is chrooted, it can be:

link = "/public_html/image/ayri.jpg";

Or something completely different. See How to get HTTP URL of file uploaded to FTP server (an opposite problem, but the answer will give you an idea anyway).

(In any case, obviously the name of variable link is wrong, it should be path.)

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
-1

So i've found myself the answer, for those of you who will encounter this problem, first for POST or UPLOADING files, always use Asynctask, if not it will never work. And just check that in your application you have permission for file management, you can see in settings -> Application -> Your app -> Permission -> Storage check yes. And that's it.

Thank you stackoverflow for helping !