Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HttpURLConnection连接重置或管道断裂_Java_Android_Http_Post_Httpurlconnection - Fatal编程技术网

Java HttpURLConnection连接重置或管道断裂

Java HttpURLConnection连接重置或管道断裂,java,android,http,post,httpurlconnection,Java,Android,Http,Post,Httpurlconnection,我正在尝试从android使用post方法将字节数组上传到http服务器。但我总是得到连接重置或断管错误。请帮我理解我哪里做错了 int bufferBytes = 131072; // 128 KB Random r = new Random( System.currentTimeMillis() ); int fileName = 10000 + r.nextInt(20000); String urlParameters = ""+ StringUtils.leftPad("ab

我正在尝试从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:Connection reset,否则我将得到java.net.SocketException:break-pipe

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

不要设置内容长度。在分块传输模式中根本不需要它,在Java HttpURLConnection中也根本不需要它,Java HttpURLConnection会在必要时为您设置它。

检查您的服务器是否正确插入了SSL。我已经在标题中添加了内容长度。setRequestProperty(“内容长度”,Integer.toString(postData.Length));但是仍然得到java.net.SocketException:断管错误