当前位置: 代码迷 >> Java Web开发 >> java.lang.IllegalStateException: getWriter() has already been called for this response
  详细解决方案

java.lang.IllegalStateException: getWriter() has already been called for this response

热度:476   发布时间:2016-04-17 12:28:36.0
关于Hibernate 读取图片问题!!!!!!
这是我读取图片的代码!!!!!!
public class ServletIMG extends HttpServlet {

private Session session;
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
try{
Login user=(Login)session.load(Login.class,"kobe");
InputStream in=user.getPhoto().getBinaryStream();//将图片转换成流形式
ServletOutputStream fileout=response.getOutputStream();
byte [] buf=new byte[1024];
int len;
while((len=in.read(buf))!=-1){
fileout.write(buf, 0, len);
}
in.close();
out.close();
}
catch(Exception e){
e.printStackTrace();
}
}

public void init() throws ServletException {
try{
Configuration config=new Configuration().configure();
SessionFactory sf=config.buildSessionFactory();
session=sf.openSession();
Transaction ts=session.beginTransaction();
}
catch(HibernateException e){
e.printStackTrace();
}
}

}
???????????????????????????????
这是我配置文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
  Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
  <class name="model.Login" table="login" catalog="dreamserver">
  <id name="UName" type="java.lang.String">
  <column name="u_name" length="20" />
  <generator class="assigned" />
  </id>
  <property name="UPass" type="java.lang.String">
  <column name="u_pass" length="20" not-null="true" />
  </property>
  <property name="photo" type="java.sql.Blob">
  <column name="photo" />
  </property>
  </class>
</hibernate-mapping>

错误提示!!!!!!!!!!!!!!!!!!!!!
java.lang.IllegalStateException: getWriter() has already been called for this response
哪位朋友 能告诉我为什么啊??????
刚学Hibernate 希望大家能帮忙

------解决方案--------------------
response.getOutputStream()和response.getWriter();只能执行其中的一个,不能同时执行。
  相关解决方案