file.exists()){Toast.makeText(this,"文件不存在",Toast.LENGTH_SHORT).show();return;}RequestBody muiltipartBody=newMultipartBody.Builder()/如果提交的是表单一定要设置这句.setType(MultipartBody.FORM).addFormDataPart("username","admin")/第一个双引号是键值...
OkHttpClientclient=newOkHttpClient();Filefile=newFile("path/to/file.jpg");RequestBodyrequestBody=newMultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file",file.getName(),RequestBody.create(MediaType.parse("image/jpeg"),file)).build();Requestrequest=newRequest.Builder().url(...
文件上传示例: OkHttpClient client = new OkHttpClient(); File file = new File("path/to/file"); RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("application/octet-stream"), file)...
.addFormDataPart("username", "admin")// .addFormDataPart("password", "admin")// .addFormDataPart("myfile", "1.png", RequestBody.create(MediaType.parse("application/octet-stream"), file)) .build(); 上面添加用户民和密码的部分和我们上面学习的提交键值对的方法很像,我们关键要注意以下几点: (...
String fileName=f.getName(); builder.addFormDataPart( fileFormName,fileName,RequestBody.create(MediaType.parse("application/octet-stream"),f)); } } RequestBody body=builder.build(); Requestrequest=newRequest.Builder() .url(url) .method("POST", body) ...
FileProgressRequestBody 以2KB为单位上传,对外暴露回调ProgressListener来发布进度。接着写一个上传管理类:HttpUploader,先构造Request对象: protectedRequestgenerateRequest(Stringurl){// 构造上传请求,模拟表单提交文件StringformData=String.format("form-data;name=file; filename=%s",FileUtil.pickFileNameFromPath(file...
.addFormDataPart("file",file.getName(),RequestBody.create(MediaType.parse("file/*"), file))//添加文件 .build(); finalRequest request =newRequest.Builder() .url(URLContant.CHAT_ROOM_SUBJECT_IMAGE) .post(multipartBody) .build(); client.newCall(request).enqueue(newCallback() {。。。});...
@TestpublicvoidwhenSendMultipartRequest_thenCorrect()throws IOException{RequestBody requestBody=newMultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("username","test").addFormDataPart("password","test").addFormDataPart("file","file.txt",RequestBody.create(MediaType.parse("application...
* @param file 文件 * @param blockSize 分块大小 * @return 分块数据 */ public static byte[] getBlock(long offset, File file, int blockSize) { byte[] result = new byte[blockSize]; RandomAccessFile accessFile = null; try { accessFile = new RandomAccessFile(file, "r"); ...
.addFormDataPart("file", file.getName(), RequestBody.create(file, MediaType.parse("image/png"))) .addFormDataPart("param1", "参数1") .addFormDataPart("param2", "参数2") .build(); Request request = new Request.Builder() .url(requestPath) ...