0
我特别需要在Android(noob)中通过cURL进行文件上传。对于像curl -F "[email protected]" https://server.com
之类的东西。如何在Android中运行cUrl调用?
我可以在java中得到替代片段吗?
我特别需要在Android(noob)中通过cURL进行文件上传。对于像curl -F "[email protected]" https://server.com
之类的东西。如何在Android中运行cUrl调用?
我可以在java中得到替代片段吗?
虽然可以直接从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();
看到https://stackoverflow.com/questions/22138881/file-upload-using-httppost-android – hamilyon