简体   繁体   中英

upload android local file to remote webserver using curl

I'm trying to upload a file from my android device using curl. The file is located at

/sdcard/Download/fileToUpload.txt

Below is the command I use through Terminal application:-

curl -i -X POST -H "Content-Type: multipart/form-data" \            
                -F "content=@/sdcard/Download/fileToUpload.txt" \
                 http://192.168.1.104/android/upload.php

I have tested the same command which worked fine from desktop. I'm banging my head from past few hours. Please let me know what is wrong.

PS: I'm worried where this question would be a stupid one.

edit:- I don't want to root my device. As this would be my last option.

Curl library is installed on your desktop but this doesn't mean that the same library is installed on your android device (Terminal app runs on your device). To install curl library on an Android device you should probably have to root your device, download curl binaries and install them on the device. You can follow this guide to understand how: https://appuals.com/install-curl-openssl-android/

You can use Retrofit , a very powerful library for Android, providing useful APIs to interface with servers through http requests.

In the specific case, you can use a post in multipart form data according to this template in an interface file

@Multipart
@POST("/android/upload.php")
Call<Void> sendFiles(
        @Part MultipartBody.Part[] files
);

Then, you have to instantiate retrofit in your specific code:

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(endpoint)
            .build();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM