如何设置内容长度的Andr​​oid?长度、如何设置、内容、oid

2023-09-13 02:19:16 作者:~綄镁の恋づ

我想要得到的内容大小 request.getContentLength()服务器端JSP页面。 但request.getContentLength()始终返回-1,我不为什么?

Android的代码片段code:

  URL URI =新的URL(actionUrl);
HttpURLConnection的康恩=(HttpURLConnection类)uri.openConnection();
//conn.setChunkedStreamingMode(100);
conn.setConnectTimeout(setTimeout的大于0的setTimeout:timeoutConnection?);
conn.setReadTimeout(setTimeout的大于0的setTimeout:timeoutConnection?);
conn.setDoInput(真正的);
conn.setDoOutput(真正的);
conn.setUseCaches(假);
conn.setRequestMethod(POST);
conn.setRequestProperty(连接,保持活动);

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

conn.setRequestProperty(Charsert,编码);
conn.setRequestProperty(内容类型,多部分/表单数据
                +;边界=+ java.util.UUID.randomUUID()的toString());
conn.connect();
 

解决方案

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

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

I want to get content size by request.getContentLength() in server client JSP page. But request.getContentLength() always return -1, i do not why?

Android snippet code:

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();

解决方案

You are using conn.setChunkedStreamingMode(100) that will effectively enable the chunked transfer encoding in chunks of 100 bytes when the content-lenght is unknown in advance.

Use conn.setFixedLengthStreamingMode(int len) if you know in advance the length of the content you are going to send in the body of the request.