How to create Directory on server in Java Android

How to create Directory on server in Java Android
April 7, 2020 No Comments Android Development,Development Pushpendra Kumar

Hi Guys, Welcome to the new trick. Here you learn How to create Directory on server in Java Android. Here I have took a basic example of a directory. In other word, I will tell you the very sort trick for uploading the big data on the server without api interaction.

Create Directory on server in Java Android

So let’s begin with the practical programming. This technique really will save your life if you are working on the big application and you are worried about server hanging issue. Then go through with this technique. At the very first we will create a directory on the server. So let’s begin with the step by step.

  • Create a New Android Application – If you did not have your own code!
  • Download a JAR Library from the given link
  • Add unzip the file and copy file inside your libs folder. which is as – application_root_folder -> app->libs
  • Now add dependency in your android project.
  • Open Project Structure, Click on File. After that you will see project structure, open that.
  • Now click on Dependency and do as mention in below image.
  • how to create directory in java
  • Click on Dependency -> Click on App -> + button below declared dependency.
  • Select Jar file and then check apache-commons-net
  • how to create directory in java android
  • All right you have done almost. Now click on DONE after that Apply and after that OK

Now it’s time for coding 😎😎😎😎

Add Internet permission to your AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="app.upload">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Great.. 👍 

Now we code into Java. And your java file looks like that.

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

import org.apache.commons.net.ftp.FTPClient;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new CreateDir().execute();
    }

    static class CreateDir extends AsyncTask<Void, Void, Void> {
        String TAG = "MAIN_ACTIVITY";

        @Override
        protected Void doInBackground(Void... voids) {

            String server = "XXX.XX.XX.XXX"; // YOUR IP ADDRESS OR DOMAIN NAME EX - http://example.com OR https://example.com
            String username = "XXXXXXXXXXXXXX"; // Your ftp username EX - root OR Ubuntu or XYZ
            String password = "XXXXXXXXXXXXXXXX"; // Your ftp password EX - NCSJN123 OR *#&YBSUSDN

            FTPClient ftpClient = new FTPClient();
            try {
                ftpClient.connect(server);
                ftpClient.login(username, password);
                ftpClient.enterLocalPassiveMode();

                Log.d(TAG, "CONNECTED");

                String dirPath = "/public_html/upload_files/new_dir";
                boolean created = ftpClient.makeDirectory(dirPath);

                Log.d(TAG, String.valueOf(created));

                ftpClient.logout();
                ftpClient.disconnect();

                Log.d(TAG, "DISCONNECTED");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}

😎 😎😎 COOL 😎😎😎😎 

NOTE * – If you are facing any issue something like permission denied or Host name is not associated. Then Uninstall your application first and again install. But make sure you follow the complete tutorial as per suggestion.

In Conclusion, You have done with all the functionality. But if still you are facing any issue then you can watch the complete video here.

Tags
About The Author
Pushpendra Kumar I am passionate about mobile application development and professional developer at Colour Moon Technologies Pvt Ltd (www.thecolourmoon.com). This website I have made so that I can meet with new challenges and can share here.

Leave a reply

Your email address will not be published. Required fields are marked *