Back to Browse

How to upload a file to FTP server from your Android App Java code?

1.5K views
Mar 31, 2024
15:32

In this tutorial, it shows the steps to develop your Android app to upload an image file to the FTP server using FTP Client commands. I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: [email protected] Complete source code and other details/ steps of this video are posted in the below link: https://programmerworld.co/android/how-to-upload-a-file-to-ftp-server-from-your-android-app-java-code/ GitHub: https://github.com/programmerworld1990/uploadfiletoftp Maven Dependency: https://mvnrepository.com/artifact/commons-net/commons-net/3.10.0 However, the main Java code is copied below also for reference: package com.programmerworld.uploadfiletoftp; import static android.Manifest.permission.READ_MEDIA_IMAGES; import android.content.pm.PackageManager; import android.os.Bundle; import android.os.StrictMode; import android.os.storage.StorageManager; import android.os.storage.StorageVolume; import android.view.View; import android.widget.EditText; import android.widget.TextView; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; public class MainActivity extends AppCompatActivity { private EditText editTextUserName, editTextPassword; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) - { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); ActivityCompat.requestPermissions(this, new String[]{READ_MEDIA_IMAGES}, PackageManager.PERMISSION_GRANTED); editTextPassword = findViewById(R.id.editTextTextPassword); editTextUserName = findViewById(R.id.editTextUserName); textView = findViewById(R.id.textViewStatus); StrictMode.ThreadPolicy threadPolicy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(threadPolicy); return insets; }); } public void buttonUploadFile(View view){ StorageManager storageManager = (StorageManager) getSystemService(STORAGE_SERVICE); StorageVolume storageVolume = storageManager.getStorageVolumes().get(0); // 0 for internal Storage File fileImage = new File(storageVolume.getDirectory().getPath() + "/Download/images.jpg"); FTPClient ftpClient = new FTPClient(); try { InputStream inputStream = Files.newInputStream(fileImage.toPath()); ftpClient.connect("192.168.1.1"); ftpClient.login(editTextUserName.getText().toString(), editTextPassword.getText().toString()); ftpClient.changeWorkingDirectory("usb1_1/Uploads/"); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); ftpClient.sendCommand("OPTS UTF8 ON"); boolean booleanStatus = ftpClient.storeFile("image_remoteFile.jpg", inputStream); textView.setText(String.valueOf("Status - " + booleanStatus)); inputStream.close(); ftpClient.logout(); ftpClient.disconnect(); } catch (IOException e) { throw new RuntimeException(e); } } } --

Download

0 formats

No download links available.

How to upload a file to FTP server from your Android App Java code? | NatokHD