okhttp 上传文件socket关闭怎么解决
展开全部
使用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();
原因是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();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询