2017-07-19 106 views

回答

1

雖然可以直接從Java運行該命令,但不應該這樣做。每部手機都不一樣,有些Android手機可能沒有安裝curl

您可以使用庫,如Apache或OkHttp來做到這一點。下面是與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();