struts2中上传图片的问题
就是在一个页面上显示一则新闻,下面配图片,新闻和图片是两张表,通过新闻id关联,本来新闻能显示出来,后来把图片读取加到显示新闻的action里面后,就只能显示一张图片(本...
就是在一个页面上显示一则新闻,下面配图片,新闻和图片是两张表,通过新闻id关联,本来新闻能显示出来,后来把图片读取加到显示新闻的action里面后,就只能显示一张图片(本来有多张图片的),新闻显示什么的都没有了,报错是:
java.lang.IllegalStateException: getOutputStream() has already been called for this response
严重: Servlet.service() for servlet [default] in context with path [/NewSystem] threw exception
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
相关代码是:
action里面
public String execute(){
try{
news = DaoFactory.getNewsDao().findById(new_id);
List<InputStream> list = DaoFactory.getPicDao().getPicById(new_id);
for(InputStream in:list){
HttpServletResponse response = ServletActionContext.getResponse();
System.out.println(in.available());
byte[] img = new byte[in.available()];
in.read(img);
ServletOutputStream out = response.getOutputStream();
out.write(img);
}
}catch(Exception e){
e.printStackTrace();
return "error";
}
return "success";
}
jsp里面:
<div style="position:absolute;left:850px;top:330px">
<s:property value="news.new_aut"/> <s:property value="news.new_time"/>
</div>
<div style="position:absolute;left:250px;top:350px">
<s:textarea cols="90" rows="80" name="news.new_con" readonly="true" style="background:transparent;border-style:none;font-size:17px;font-family:楷体;"></s:textarea>
</div>
<div style="position:absolute;left:200px;top:500px">
<%out.clear();
out=pageContext.pushBody(); %>
<s:iterator value="list">
<img src="outPic?new_id=<s:property value="new_id"/>"/>
</s:iterator>
</div>
该怎么解决? 展开
java.lang.IllegalStateException: getOutputStream() has already been called for this response
严重: Servlet.service() for servlet [default] in context with path [/NewSystem] threw exception
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
相关代码是:
action里面
public String execute(){
try{
news = DaoFactory.getNewsDao().findById(new_id);
List<InputStream> list = DaoFactory.getPicDao().getPicById(new_id);
for(InputStream in:list){
HttpServletResponse response = ServletActionContext.getResponse();
System.out.println(in.available());
byte[] img = new byte[in.available()];
in.read(img);
ServletOutputStream out = response.getOutputStream();
out.write(img);
}
}catch(Exception e){
e.printStackTrace();
return "error";
}
return "success";
}
jsp里面:
<div style="position:absolute;left:850px;top:330px">
<s:property value="news.new_aut"/> <s:property value="news.new_time"/>
</div>
<div style="position:absolute;left:250px;top:350px">
<s:textarea cols="90" rows="80" name="news.new_con" readonly="true" style="background:transparent;border-style:none;font-size:17px;font-family:楷体;"></s:textarea>
</div>
<div style="position:absolute;left:200px;top:500px">
<%out.clear();
out=pageContext.pushBody(); %>
<s:iterator value="list">
<img src="outPic?new_id=<s:property value="new_id"/>"/>
</s:iterator>
</div>
该怎么解决? 展开
1个回答
展开全部
你在最后(即out.write() 后)应该加上out.close(), 因为Struts2的Action隐含了response成员变量,也会使用它,如果不关掉它,就返回给"success"视图,就有可能报你写的错了。
当然out.close()最好加在 finally里。
你试试看,不行再问。
当然out.close()最好加在 finally里。
你试试看,不行再问。
更多追问追答
追问
还是不行滴,一样的错~~
追答
再在out.close()之前加上out.flush()试试看。
像这样:
...
out.write(img);
out.flush();
}
out.close();
out.close()放到循环外面。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询