One of the tasks of my training project is to transfer a file from an Android smartphone to an FTP server. Googling, I found that this can be done using the Apache Commons library. The library successfully connected to the project, added a resolution to AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
Further, I used the example of uploading a file to an FTP server:
public static void ftpConn(String hostAddress, String log, String password) throws FileNotFoundException { FTPClient fClient = new FTPClient(); FileInputStream fInput = new FileInputStream("Android/data/название_пакета/Yes.txt"); String fs = "Yes.txt"; try { fClient.connect(hostAddress); fClient.enterLocalPassiveMode(); fClient.login(log, password); fClient.storeFile(fs, fInput); fClient.logout(); fClient.disconnect(); } catch (IOException ex) { System.err.println(ex); } }
The event handler on the button, on clicking on which the file will be sent:
try { ftpConn("n***.n****.ru","*****","*****"); } catch (FileNotFoundException ex) { Toast.makeText(getApplicationContext(), "Неудача", Toast.LENGTH_SHORT).show(); }
The file that I need to send I put in the internal memory of the device: Android / data / package_name / Yes.txt. I tried to prescribe various file paths into the string:
FileInputStream fInput = new FileInputStream("Android/data/название_пакета/Yes.txt");
But nothing worked. The application caught an exception and displayed Toast: "Failure."
Tell me, please, what's the problem? I think the error is that I incorrectly specify the path to the file.
PS: Launched on Nexus 5 with Android 6.
PSS: This example of sending a file to FTP tried to run on a computer - everything worked out the first time.
"/mnt/sdcard0/Android/data/название_пакета/Yes.txt"
; Well, when you find the right path to the file, your application will most likely need read access to external files READ_EXTERNAL_STORAGE , and starting with android 6, these rights must not only be written in the manifest, but checked and, if necessary, requested before reading the file. - xkor