Home > Mobile >  A great god, genuflect is begged HttpUrlConnection uploading large files error, getResponseCode has
A great god, genuflect is begged HttpUrlConnection uploading large files error, getResponseCode has

Time:12-03

Project to upload the file to the server, set up a file nagix maximum 10 m, if the mobile client upload file for more than 10 m, will upload failure, I am using the android native HttpUrlConnection network request, I want to upload more than 10 m file, will be able to get the response code and error message, but in fact, when I get the response code, the code is an exception is thrown, exception for the following three (with modified parameters, the exception is one of the three)
 
The error asyncExecute: Write error: SSL=0 x7ec7b80008: I/O error during the system call, the Connection reset by peer

Javax.net.ssl.SSLException: Write error: SSL=0 x7e67fae408: I/O error during the system call, Broken pipe

Java.net.SocketException: Connection reset

Through fiddler caught, then, I get the exception is as follows:
 



413 Request Entity Too Large


<body>




Nginx/1.17.5 & lt;/center>




Then ask about the background, for the sake of load balancing, specially on the nagix set the limit of 10 m,
Probably the reason is, upload more than 10 m file, nginx check file ever exceeds the maximum limit Settings, connection is disconnected, so when getResponseCode or getInputStream directly thrown exception,
In the face of such situation, how can I get a response code?
HttpUrlConnection setup code is as follows:
 
URL originUrl=new URL (URL);
String protocol=originUrl. GetProtocol ();
If (TextUtils. Equals (protocol, "HTTP")) {
Port=80;
}
MURL=new URL (protocol, originUrl getHost (), port, originUrl. GetFile ());
MConn=(HttpURLConnection mURL). The openConnection ();

If (mConn instanceof HttpsURLConnection) {
SelfSignedCertificate=true;
SSLCustomSocketFactory factory=new SSLCustomSocketFactory (selfSignedCertificate? SSLCustomSocketFactory. GetSocketFactory ()
: (SSLSocketFactory) SSLSocketFactory getDefault ());
(HttpsURLConnection mConn). SetSSLSocketFactory (factory);
(HttpsURLConnection mConn). SetHostnameVerifier (new HostnameVerifier () {
@ Override
Public Boolean verify (String hostname, SSLSession session) {
return true;
}
});
}
MConn. SetRequestMethod (" POST ");
MConn. SetDoOutput (true);
MConn. SetDoInput (true);
MConn. SetUseCaches (false);
MConn. SetConnectTimeout (30000);
MConn. SetReadTimeout (30000);
MConn. SetRequestProperty (" the user-agent ", "XXXX");//XXXX doesn't involved in the project information display
MConn. SetRequestProperty (" Connection ", "Keep Alive -");
MConn. SetRequestProperty (" Charset ", "utf-8");
MConn. SetRequestProperty (" the content-type ", "multipart/form - data; A boundary="+ boundary);
MConn. SetRequestProperty (" Expect ", "100 - the Continue");
MConn. SetChunkedStreamingMode (0);
//add the incoming request header
For (Map. Entry Item: headers. EntrySet ()) {
MConn. SetRequestProperty (item. GetKey (), the item. The getValue ());
}
MConn. The connect ();
//add files
DataOutputStream out=new DataOutputStream (connect getOutputStream ());
//the incoming parameter
Out. Write (params. GetBytes ());
Out. The flush ();
StringBuffer strBuf=new StringBuffer ();
For (String key: paramsMap keySet ()) {
StrBuf. Append (TWO_HYPHENS);
StrBuf. Append (BOUNDARY);
StrBuf. Append (LINE_END);
StrBuf. Append (" the Content - Disposition: the form - data; Name=\ \ "" + key +" \ "");
StrBuf. Append (LINE_END);

StrBuf. Append (" content-type: "+" text/plain ");
StrBuf. Append (LINE_END);
StrBuf. Append (" the Content - Length: "+ paramsMap. Get (key). The Length ());
StrBuf. Append (LINE_END);
StrBuf. Append (LINE_END);
StrBuf. Append (paramsMap. Get (key));
StrBuf. Append (LINE_END);
}
String paramsString=strBuf. ToString ();
Out. Write (paramsString getBytes ());
Out. The flush ();
The String fileName=UriUtils. GetFileNameByUri (mContext fileUri);
String mimeType=UriUtils. GetMimeType (mContext fileUri);
Long fileLength=UriUtils. GetFileLength (mContext fileUri);
if(! TextUtils. IsEmpty (filename)) {
FileName=fileName;
}
//add file header
Out. Write (getFileHeaderParamsString (fileKey, fileName, mimeType, fileLength). GetBytes ());
//add files body
String filePath=UriUtils. GetFilePath (fileUri);
InputStream in=null;
Try {
if (! TextUtils. IsEmpty (filePath) & amp; & The new File (filePath.) the exists ()) {
In=new FileInputStream (new File (filePath));
} else {
In=mContext. GetContentResolver (). OpenInputStream (fileUri);
}
Byte [] TMP=new byte [2048].
Int l;
Long sum=0;
While ((l=in. Read (TMP))!=1) {
Out. Write (TMP, 0, l);
The sum +=l;
If (callback!=null) {
The callback. OnProgress (fileLength, sum);
}
}
} the catch (IOException e) {
e.printStackTrace();
} the finally {
If (in!=null) {
In the close ();
}
}
//add the end-of-file
Out. Write (getFileEndString (.) getBytes ());
Out. The flush ();
HttpResponse response=new HttpResponse ();
//uploading large files here it throws an exception, but the exception, not want then, there is no corresponding code
The response. Code=mConn. GetResponseCode ();
If (response. Code==HttpURLConnection. HTTP_OK) {
The response. ContentLength=mConn. GetContentLength ();
The response. The inputStream=mConn. GetInputStream ();
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related