WepsTech
The world of development
Hello Guys, Welcome to this tutorial. I hope you are doing well. Here once again I came with a new sort trick. Which is as How to upload a file on the server in android without using any API services. In this tutorial, I will create a JSON file and I will upload this file to the server.
This trick would be very useful for you. If you are a professional developer or if you are trying to be a professional developer. Because let’s assume you are working on a big application. Something like OYO, FACEBOOK, TIKTOK or something like that on which too much user is coming concurrently. Then there is a chance for getting your server hang. And your server could be stopped. If you are using API services for uploading files, video, images or any other type of file.
If you do no know that how to create directory on server in Java Android then visit on this link. Because it is very necessary for going to the next step. So I can trust that you have read the suggested tutorial. So now let’s begin with file uploading on the server.
First we will create a JSON file then we will write data inside the file into the string format. And after that we will upload that file to the server with the help of FTPClient.
JSONObject object = new JSONObject(); object.put("name", "PUSHPENDRA"); object.put("sir_name", "SAINI"); object.put("age", "26"); object.put("address", "HNo 383, M.Bad SRE 24729"); File file = new File(getFilesDir(), "jsonFile.json"); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file)); bufferedWriter.write(object.toString()); bufferedWriter.close(); InputStream inputStream = new FileInputStream(file); ftpClient.storeFile(dirPath + "/jsonFile.json", inputStream); inputStream.close();
😎 Wonderful, So above code is part of the code. Bellow is the complete file looks like as.
import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import androidx.appcompat.app.AppCompatActivity; import org.apache.commons.net.ftp.FTPClient; import org.json.JSONObject; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.InputStream; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new CreateDir().execute(); } class CreateDir extends AsyncTask<Void, Void, Void> { String TAG = "MAIN_ACTIVITY"; @Override protected Void doInBackground(Void... voids) { String server = getString(R.string.server); // YOUR IP ADDRESS OR DOMAIN NAME EX - http://example.com OR https://example.com String username = getString(R.string.username); // Your ftp username EX - root OR Ubuntu or XYZ String password = getString(R.string.password); // 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/json"; boolean created = ftpClient.makeDirectory(dirPath); //START OF FILE UPLOADING JSONObject object = new JSONObject(); object.put("name", "PUSHPENDRA"); object.put("sir_name", "SAINI"); object.put("age", "26"); object.put("address", "HNo 383, M.Bad SRE 24729"); File file = new File(getFilesDir(), "jsonFile.json"); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file)); bufferedWriter.write(object.toString()); bufferedWriter.close(); InputStream inputStream = new FileInputStream(file); ftpClient.storeFile(dirPath + "/jsonFile.json", inputStream); inputStream.close(); //END OF FILE UPLOADING ftpClient.logout(); ftpClient.disconnect(); } catch (Exception e) { e.printStackTrace(); } return null; } } }
😊 Great, You have achieved you task. Above json file upload is just an example. In Other word, This is just a scenario for uploading a file on the server without using any API Service. And with the help of this trick you can upload video file, audio file, images file or any other type of file as well.
In Conclusion, If you are still have any doubt the you can go through with the video tutorial. I hope this will help you for your future task as well. If you are suffering with any other bug in that then you can comment bellow. 😊😊😊😊
Below are few more important topics –
HAPPY CODING – 😊😊
стоимость опрессовки отопления
Very soon this website will be famous amid all blog people, due to
it’s good articles or reviews