异常出现的场景:
(1)ssh项目,提供下载功能。项目使用tomcat部署;
(2)写了一个测试类来测试下载功能,执行时报异常:
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
下载类在struts中的配置(截取):
<action name="downloadOneFile" class="downloadOneFileAction"> <result type="stream" name="success"> <param name="inputName">downloadFile</param> <param name="contentType"></param> <param name="contentDisposition">attachment;filename=${filename}</param> <param name="bufferSize">4096</param> </result> </action>
在测试代码中使用 HttpURLConnection模拟浏览器发送http请求,读取应答体的部分代码如下:
上述代码是有问题的,本来bis中有1k的字节,结果我试图读取2k的字节,所以就报错了,原因找到了,怎么解决呢?
修改为:
private static byte[] readDataFromLength2(HttpURLConnection huc, int contentLength) throws Exception { InputStream in = huc.getInputStream(); BufferedInputStream bis = new BufferedInputStream(in); // 数据字节数组 byte[] receData = new byte[contentLength]; int readLength = 0; // 数据数组偏移量 int offset = 0; readLength = bis.read(receData, offset, contentLength); // 已读取的长度 int readAlreadyLength = readLength; while (readAlreadyLength < contentLength) { readLength = bis.read(receData, readAlreadyLength, contentLength - readAlreadyLength); readAlreadyLength = readAlreadyLength + readLength; } return receData; }
解决问题的过程:
1,刚开始以为是编码的问题,怀疑BufferedInputStream bis = new
BufferedInputStream(in);中BufferedInputStream的构造方法的第二个参数是编码(如GBK,UTF-8等),结果证明没有这个参数;
2,bis.read的第三个参数刚开始以为是索引,结果发现是长度(读取的字节);
3,while中比较时,应该是readAlreadyLength和contentLength进行比较。
参阅附件中com.http.util.HttpSocketUtil .附件中的项目是使用maven 构建的。
注意:读取BufferedInputStream时 使用while循环是必要的。
相关推荐
纠结了半天的 java.lang.IllegalStateException: getOutputStream() has already。这个问题困扰了半天,在网上查阅了大量资料 出这个错误一般就是下面2个.....
1、错误信息 异常:Caused by: java.lang....Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract com.iot.framework.core.response.CommResponse ...
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but...
NULL 博文链接:https://anminer.iteye.com/blog/845067
1. java.lang.IllegalStateException: No wrapped connection. 2.java.lang.IllegalStateException: Adapter is detached. 原因: 1.单线程一次执行一个请求可以正常执行,如果使用多线程,同时执行多个请求时就会...
解决java.lang.IllegalStateException: unread block data的架包
HTTP Status 500 – Internal Server ... nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.util.Date': no matching editors or co
spring 总的概述。。。入门好教程~~~~~~~~~~~~~
weblogic12c部署webservice错误问题WSSERVLET11: 无法解析运行时描述符: java.lang.IllegalStateException: MASM0001:
Cause: java.lang.IllegalStateException: Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath.:java.lang.IncompatibleClassChangeError: class ...
今天公司springboot项目准备部署到...java.lang.IllegalStateException: 启动子级时出错 at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:720) at org.apache.catalina.core.Con
IllegalStateException: The specified child already has a parent.我的博客中有文章讲解
这个存储库提供了一种在处理片段传输和后台任务时避免“java.lang.IllegalStateException:Can not perform this action after onSaveInstanceState”的方法。 您可以在的非常权威的阅读有关该问题和可能的解决方案...
各种数据库在JDBC中的连接方法.及在struts与hibernate中的配置..
因工作的缘故,有机会接触了Java本地方法调用(JNI)。本文中记下了初试JNI的过程和一点点体会。 使用Java的JNI调用C/C++的动态联结库有个固定的步骤,下面将以一个最简单的HelloWorld例子程序来说明调用过程。这个...
The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact. ...
SteganoImage 1.0 绿色版
里面包含了java通过xfire访问webservice用到的jar包,已经打包好了