okhttp上传文件报错,上传文件大小跟接受文件大小不一致。
关闭文件读写流。
文件名称过长!改短就行。
附上传文件代码:
public class ClientUploadUtils {
public static ResponseBody upload(String url, File file,String num) throws Exception {
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("logFile", file.getName(),
RequestBody.create(MediaType.parse("multipart/form-data"), file))
.addFormDataPart("serial_no",num)
.build();
Request request = new Request.Builder()
.header("Authorization", "Client-ID " + UUID.randomUUID())
.url(url)
.post(requestBody)
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);else Log.i("logCatch","upload Success");
return response.body();
}
}
网友评论