我使用HttpURLConnection将大文件(最多160MB)上传到服务器。 有这种奇怪的IOException随机出现。 这是代码。
录制视频文件(最长5分钟)。然后上传它我启动一个IntentService。它使用HttpURLConnection。这是我如何发起HttpURLConnection。
try {
URL url=new URL(getString(postUrlResId));
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setConnectTimeout(60000);
//httpURLConnection.setUseCaches(false);
httpURLConnection.setFixedLengthStreamingMode(postLength);
httpURLConnection.setRequestMethod("POST");
//httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
//httpURLConnection.setRequestProperty("Cache-Control", "no-cache");
httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=*****");
dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
dataOutputStream.writeBytes(header);
-----------
----file writting to dataOutputStream
---------
}
dataOutputStream.writeBytes(footer);
RESPONSE_UPLOAD_VIDEO_BEING_PROCESSED_IN_SERVER();
if(httpURLConnection.getResponseCode() == 200 && httpURLConnection.getResponseMessage().equalsIgnoreCase("OK")) {
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
StringBuffer serverRes = new StringBuffer();
String inputLine ;
while ((inputLine = bufferReader.readLine()) != null)
{
serverRes.append(inputLine);
}
PRINT_LOG(serverRes.toString());
RESPONSE_UPLOAD_COMPLETE(serverRes.toString());
}else {
RESPONSE_ERROR();
}
} catch (MalformedURLException e) {
e.printStackTrace();
PRINT_ERROR(" MalformedURLException. ");
RESPONSE_ERROR();
} catch (IOException e) {
e.printStackTrace();
PRINT_ERROR(" IOException. may be no internet connection ");
RESPONSE_ERROR();
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
if (dataOutputStream != null) {
try {
dataOutputStream.flush();
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
现在这个电话" httpURLConnection.getOutputStream()"有时抛出这个例外:
03-13 15:50:23.088:W / System.err(27349):java.net.SocketException:sendto failed:ECONNRESET(由peer重置连接) 03-13 15:50:23.088:W / System.err(27349):at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:506) 03-13 15:50:23.088:W / System.err(27349):at libcore.io.IoBridge.sendto(IoBridge.java:475) 03-13 15:50:23.088:W / System.err(27349):at java.net.PlainSocketImpl.write(PlainSocketImpl.java:507) 03-13 15:50:23.088:W / System.err(27349):at java.net.PlainSocketImpl.access $ 100(PlainSocketImpl.java:46) 03-13 15:50:23.093:W / System.err(27349):at java.net.PlainSocketImpl $ PlainSocketOutputStream.write(PlainSocketImpl.java:269) 03-13 15:50:23.093:W / System.err(27349):at java.io.OutputStream.write(OutputStream.java:82) 03-13 15:50:23.093:W / System.err(27349):at libcore.net.http.HttpEngine.writeRequestHeaders(HttpEngine.java:646) 03-13 15:50:23.093:W / System.err(27349):at libcore.net.http.HttpEngine.initRequestBodyOut(HttpEngine.java:346) 03-13 15:50:23.093:W / System.err(27349):at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:301) 03-13 15:50:23.093:W / System.err(27349):at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239) 03-13 15:50:23.093:W / System.err(27349):at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80) 03-13 15:50:23.093:W / System.err(27349):at com.karaoke.service.UploadService.onHandleIntent(UploadService.java:145) 03-13 15:50:23.093:W / System.err(27349):在android.app.IntentService $ ServiceHandler.handleMessage(IntentService.java:65) 03-13 15:50:23.093:W / System.err(27349):在android.os.Handler.dispatchMessage(Handler.java:99) 03-13 15:50:23.093:W / System.err(27349):在android.os.Looper.loop(Looper.java:137) 03-13 15:50:23.093:W / System.err(27349):在android.os.HandlerThread.run(HandlerThread.java:60) 03-13 15:50:23.098:W / System.err(27349):引起:libcore.io.ErrnoException:sendto failed:ECONNRESET(由peer重置连接) 03-13 15:50:23.098:W / System.err(27349):at libcore.io.Posix.sendtoBytes(Native Method) 03-13 15:50:23.098:W / System.err(27349):at libcore.io.Posix.sendto(Posix.java:146) 03-13 15:50:23.098:W / System.err(27349):at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177) 03-13 15:50:23.098:W / System.err(27349):at libcore.io.IoBridge.sendto(IoBridge.java:473)
大多数情况下,这通常是我第一次开始上传服务时。但事件是随机的。一旦抛出此异常,如果再次使用相同的参数启动服务,它可以正常工作。更大的文件会增加异常的发生。我也注意到这个问题在三星设备中更频繁发生,我尝试过htc evo 3d和nexus 5。 似乎无法找到这种问题的任何理由。无法在网上找到类似的问题。
更新 所以我不确切地知道为什么会出现这个错误但是当我将我的基本网址从域名网址切换到基于IP的网址时,它始终正常运行。仍然希望对可能发生这种情况的原因有一些解释。
答案 0 :(得分:0)
httpURLConnection.disconnect();
httpURLConnection.connect();
我完全不知道为什么你在调用这两种方法,但我建议删除这两行代码将解决这个问题。