一般情况下不需要为response 设置content-length
通过ie访问,都会计算返回内容的长度,赋值给content-length,并按此长度来显示返回的内容。
但有些浏览器可能不会自动计算,譬如STB内一些低级的浏览器,这时就需要手动计算并赋值。
如 response.setContentLength(content.length());
注意上面“并按此长度来显示返回的内容”,也就是说如果计算长度不对,将会导致显示问题;如 实际长度是300,但setContentLength(200);那就只会读取前200个长度的字符。
程序强制读取会报错,举个列子
testServlet
HttpServletResponse response = (HttpServletResponse)arg1; String content = "i am linxh,i love you"; System.out.println(content.length()); response.setContentLength(content.length()-1);//故意少一位 response.getOutputStream().write(content.getBytes());
客户端
... String content = "i am linxh,i love you"; byte[] buffer = new byte[content.length()]; in.readFully(buffer); System.out.println("buufer="+new String(buffer));
此时由于返回的位数少一位,就会报 java.io.EOFException
呵呵,一般这种很少遇到,最近碰到了,所以记录一下。
推荐阅读
代码之余轻松一下:当前热门-人民的名义
相关推荐
中间件根据主体大小将Content-Length标头注入响应中。 要求 PHP> = 7.2 安装 该软件包可以通过Composer作为安装和自动加载。 composer require middlewares/content-length 例子 $ dispatcher = new Dispatcher (...
HTTP_Response消息Content-Type头部规范
The response mode in HTTP protocol is explained clearly and vividly
HTTP_Response消息Content-Type头部规范.htm
例子 var responseLength = require ( 'koa-response-length' ) ;app . use ( responseLength ( ) ) ;app . use ( function * ( ) { this . body = someStream ( ) ; // also works with strings, buffers, json etc...
用于Content-Disposition HTTP标头的小巧且经过全面测试的JS解析器,对于nodejs和浏览器环境零依赖。 该库解析标头字符串,使用可以提取的属性构造对象。 没有验证,没有转换(decodeURIComponent()除外)只是...
将“ Content-Security-Policy”或“ Content-Security-Policy-Report-Only”标头添加到响应中。 另请参阅: 通常,您只需要有限的数目,也不需要任何init参数。 如果未定义init参数,则Header将如下所示: ...
去取内容类型 解析Content-Type标头并向请求和响应对象添加访问器安装 npm install --save go-fetch-content-type用法 var HttpClient = require('go-fetch');var contentType = require('go-fetch-content-type');...
资源来自pypi官网。 资源全名:django-excel-response3-1.44.tar.gz
资源来自pypi官网。 资源全名:django-excel-response3-1.42.tar.gz
air-t07-the-incident-response-class-of-2018-tactics-and-talesfrom-the-frontline
Understand the components of incident response Go beyond the incident response plan Turn the plan into a program that needs vision, leadership, and culture to make it successful Be effective in ...
2021科教版4下测试卷.pdf
aws-lambda-http-response 创建该项目是为了促进在Amazon Lambda中组装Api Gateway返回对象,以及自动触发响应。 如何使用 安装 Npm $ npm i aws-lambda-http-response --save 例子 进口 // import const ...
Optimised-Wind-Energy-Response-to-Grid-Faults-master
Laravel开发-response-cache Laravel 5响应缓存中间件
decompress-response-main-源码.rar
HttpClient response nullprivate static void close(HttpMethod method) {method.rel
IIS上启用Gzip压缩(HTTP压缩)
「APT」air-t07-the-incident-response-class-of-2018-tactics-and-talesfrom-the-frontline - 数据安全 Android 云安全 安全认证 网络安全 法律法规