发布网友 发布时间:2022-04-20 08:00
共1个回答
热心网友 时间:2023-07-14 01:17
使用okhttp上传文件时,每次上传超过5M的文件就会失败, java.net.SocketException: sendto failed: EPIPE (Broken pipe)
原因是okhttp的请求头RequestBody出现了问题
原代码:
RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("file", file.getName(), RequestBody.create(null, file))
.addFormDataPart("filekey", fileKey)
.addFormDataPart("isSend", "true")
.addPart(Headers.of("Content-Disposition", "form-data; name=\"another\";filename=" + file.getName() + "")
,RequestBody.create(MediaType.parse("application/octet-stream"), file))
.build();
改正后的代码,删除了addPart
RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("file", file.getName(), RequestBody.create(null, file))
.addFormDataPart("filekey", fileKey)
.addFormDataPart("isSend", "true")
.build();