我正在编写一个使用多部分请求上传文件的程序。我在Java中使用okhttp3。
is = new FileInputStream("test.txt");
byte []content = IOUtils.toByteArray(is);
String uri = "URI"+"/api/files/upload?filename=test.txt&targetFolderId="+sourceID;
final MediaType MEDIA_TYPE = MediaType.parse("multipart/form-data");
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addPart(
Headers.of("Content-Disposition", "form-data;"),
RequestBody.create(MEDIA_TYPE, content)
)
.build();
Request request = new Request.Builder()
.header("Authorization", "Bearer " + accessToken)
.url(uri)
.post(requestBody)
.build();
response = client.newCall(request).execute();
int responseCode = response.code();
但是我在上面的代码中所关注的是:
读取大文件并将其作为整体存储在内存中。如何更改它以从文件中获取字节块,存储在内存中然后上传,直到文件中的所有字节都被上传?