如何设置内容长度在android系统?(How to set content-length in a

2019-06-23 13:39发布

我想通过获得内容大小request.getContentLength()在服务器端JSP页面。 但request.getContentLength()总是返回-1,我不为什么?

Android的代码片段:

URL uri = new URL(actionUrl);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
//conn.setChunkedStreamingMode(100);
conn.setConnectTimeout(setTimeOut>0?setTimeOut:timeoutConnection);
conn.setReadTimeout(setTimeOut>0?setTimeOut:timeoutConnection);  
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "keep-alive");

//conn.setRequestProperty("content-length", "10");
//conn.addRequestProperty("content-length", "20");
conn.setFixedLengthStreamingMode(30);

conn.setRequestProperty("Charsert", ENCODING);
conn.setRequestProperty("Content-Type", "multipart/form-data"
                + ";boundary=" + java.util.UUID.randomUUID().toString());
conn.connect();

Answer 1:

您正在使用conn.setChunkedStreamingMode(100) ,这将有效地使块传输编码为100个字节的块当内容lenght事先是未知的。

使用conn.setFixedLengthStreamingMode(INT LEN) ,如果你事先知道你要在请求主体发送的内容的长度。



文章来源: How to set content-length in android?