0

I Specifically need to do file upload via cURL in Android(noob). For Something like curl -F "file=@temp.txt" https://server.com.

Can i get the alternative an snippet in java?

SoroushA
  • 2,043
  • 1
  • 13
  • 29

1 Answers1

1

While it is possible to run that command directly from Java, you should not do that. Each phone is different and it is very much possible that some Android phones might not have curl installed on them.

You can use libraries such as Apache or OkHttp to do so. Here is a snippet that works with OkHttp 3.2.0:

//file is your opened file and url is the server url
OkHttpClient client = new OkHttpClient.Builder().build();
RequestBody formBody = new MultipartBody.Builder()
    .setType(MultipartBody.FORM)
    .addFormDataPart("file", file.getName(),
        RequestBody.create(MediaType.parse("text/plain"), file))
    .build();
Request request = new Request.Builder().url(url).post(formBody).build();
Response response = client.newCall(request).execute();
SoroushA
  • 2,043
  • 1
  • 13
  • 29