首页 文章

HttpURLConnection是连接重置或断开管道

提问于
浏览
2

我正在尝试使用android的post方法将字节数组上传到http服务器 . 但我总是得到连接重置或管道错误 . 请帮我理解我做错了什么 .

int bufferBytes =  131072;  // 128 KB
Random r = new Random( System.currentTimeMillis() );
int fileName  = 10000 + r.nextInt(20000);

String urlParameters  = ""+ StringUtils.leftPad("abcdefgh", bufferBytes, "abcdefgh");
byte[] postData       = urlParameters.getBytes( StandardCharsets.UTF_8 );

String CRLF = "\r\n";
String boundary = "-----------------------******";

OutputStream out = null;
HttpURLConnection urlConnection = null;
PrintWriter writer = null;
try {


URL url = new URL("host");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setConnectTimeout(10*1000) ; // 15 Sec time out
System.setProperty("http.keepAlive", "false");

urlConnection.setChunkedStreamingMode(1024);
//urlConnection.setRequestProperty("Keep-Alive", "false");
urlConnection.setRequestProperty("connection", "close");
urlConnection.setUseCaches (false);

urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Accept", "text/xml,application/x ml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
urlConnection.setConnectTimeout(15*1000) ; // 15 Sec time out
urlConnection.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
urlConnection.setRequestProperty("Accept-Encoding", "identity");
urlConnection.setRequestProperty("Transfer-Encoding", "chunked") ;

urlConnection.connect();
out = urlConnection.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(out, "UTF-8"), true);

// Send normal param.
writer.append("100000").append(CRLF);
writer.append(boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"success_action_status\"").append(CRLF);
writer.append("201").append(CRLF);
writer.append(boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"Content-Type\"").append(CRLF);
writer.append("image/jpeg").append(CRLF);
writer.append(boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"x-amz-acl\"").append(CRLF);
writer.append("public-read").append(CRLF);
writer.append(boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"key\"").append(CRLF);
writer.append("images/237615.jpg").append(CRLF);
writer.append(boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"FileName\"").append(CRLF);
writer.append("ul+"+fileName+".jpg").append(CRLF);
writer.append(boundary).append(CRLF);

writer.append("Content-Disposition: form-data; name=\"file\"; filename=ul" +fileName).append(CRLF);
writer.append("Content-Type: image/jpeg").append(CRLF); // Text file itself must be saved in this charset!
writer.append(CRLF).flush();
out.flush(); // Important before continuing with writer!
writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.



long startTime = System.currentTimeMillis();
while (true){
    out.write(postData);
    out.flush();
    if(System.currentTimeMillis() - startTime > 10000)
    break;
}

Log.d("Upload" , " Upload is success");

} catch (Exception e) {
    e.printStackTrace();
    System.out.println(e.getMessage());
}finally {
    try {
        if(writer != null)
            writer.close();
        if(out != null)
        out.close();
        if(urlConnection != null)
            urlConnection.disconnect();
    }catch (Exception e){
        e.printStackTrace();
    }
}

如果我评论下面的行我得到java.net.SocketException:连接重置否则我得到java.net.SocketException:断管

System.setProperty("http.keepAlive", "false");

1 回答

  • 0

    不要设置内容长度 . 在分块传输模式下根本不需要它,并且在Java HttpURLConnection中根本不需要它,它在必要时为您设置它 .

相关问题