In this video it shows the code to delete a file over FTP server from your Android App’s Java code.
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Complete source code and other details:
GitHub: https://github.com/programmerworld1990/deletefromftp
Maven Dependency: https://mvnrepository.com/artifact/commons-net/commons-net/3.10.0
package com.programmerworld.deletefromftp;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import org.apache.commons.net.ftp.FTPClient;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private EditText editTextUserName, editTextPassword;
private TextView textViewStatus;
@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);
editTextPassword = findViewById(R.id.editTextPassword);
editTextUserName = findViewById(R.id.editTextUserName);
textViewStatus = findViewById(R.id.textView);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
return insets;
});
}
public void buttonDeleteFile(View view){
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect("192.168.1.1");
ftpClient.login(editTextUserName.getText().toString(), editTextPassword.getText().toString());
ftpClient.changeWorkingDirectory("usb1_1/Files/");
Boolean booleanStatus = ftpClient.deleteFile("images1.jpg");
textViewStatus.setText(String.valueOf(booleanStatus));
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.DeleteFromFTP"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
plugins {
alias(libs.plugins.androidApplication)
}
android {
namespace = "com.programmerworld.deletefromftp"
compileSdk = 34
defaultConfig {
applicationId = "com.programmerworld.deletefromftp"
minSdk = 33
targetSdk = 34
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
dependencies {
implementation(libs.appcompat)
implementation(libs.material)
implementation(libs.activity)
implementation(libs.constraintlayout)
testImplementation(libs.junit)
androidTestImplementation(libs.ext.junit)
androidTestImplementation(libs.espresso.core)
// https://mvnrepository.com/artifact/commons-net/commons-net
implementation(libs.commons.net)
}
[versions]
agp = "8.3.0"
commonsNet = "3.10.0"
junit = "4.13.2"
junitVersion = "1.1.5"
espressoCore = "3.5.1"
appcompat = "1.6.1"
material = "1.11.0"
activity = "1.8.0"
constraintlayout = "2.1.4"
[libraries]
commons-net = { module = "commons-net:commons-net", version.ref = "commonsNet" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }
constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="150dp"
android:layout_height="116dp"
android:text="Hello World!"
android:textAlignment="center"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.559" />
<EditText
android:id="@+id/editTextUserName"
android:layout_width="220dp"
android:layout_height="70dp"
android:layout_marginStart="100dp"
android:layout_marginTop="34dp"
android:ems="10"
android:inputType="text"
android:text="ftpadmin"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="222dp"
android:layout_height="56dp"
android:layout_marginStart="107dp"
android:layout_marginTop="41dp"
android:ems="10"
android:hint="Enter Password"
android:inputType="textPassword"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextUserName" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="148dp"
android:layout_marginTop="60dp"
android:onClick="buttonDeleteFile"
android:text="Delete File"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextPassword" />
</androidx.constraintlayout.widget.ConstraintLayout>
Screenshot: