1
115KB 이미지 파일을 서버에 업로드 할 때 오류가 발생했습니다. (stackoverflow의 가장 많은 대답은 파일을 다운로드하는 것입니다. onFailure : java.net.ProtocolException : 그것은 오류 정보는 다음과 같습니다) 것과 같은 경우 모르는 스트림의 예기치 않은 종료java.net.ProtocolException : 예기치 않은 종료가 okhttp에 의해 서버에 이미지를 업로드 할 때 발생했습니다
관련 코드 :
public void upLoadImageFile(String uploadUrl, File file, Map<String, String> maps, final HWUploadListener listener) {
final CallbackHandler handler = new CallbackHandler(listener);
try {
MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
if (maps == null) {
builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"image\";filename=\"file.jpg\""),
RequestBody.create(MediaType.parse("image/jpeg"), file)).build();
} else {
for (String key : maps.keySet()) {
builder.addFormDataPart(key, maps.get(key));
}
builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"image\";filename=" + file.getName()), RequestBody.create(MediaType.parse("image/jpeg"), file)
);
}
RequestBody body = builder.build();
final Request request = new Request.Builder().url(uploadUrl).post(body).build();
final Call call = mOkHttpClient.newBuilder().writeTimeout(50, TimeUnit.SECONDS).build().newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
UtilUtils1.log("HuowuSdk", "onFailure :" + e.toString());
handler.uploadFailure(e.toString());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
String result = response.body().string();
handler.uploadSuccess(result);
} else {
handler.uploadFailure(response.message());
}
}
});
} catch (Exception e) {
UtilUtils1.log("HuowuSdk", e.toString());
handler.uploadError(e.toString());
}
}
답변을 감사합니다! 여기이 줄
내가 가진 당신의 answer.But 주셔서 감사합니다 전에 제한 시간을 기록했습니다. 문제는 여전히 발생했습니다. –