
java.lang.IllegalStateException: getOutputStream() has already been called for this response 怎么结
action代码publicActionForwardexecute(ActionMappingmapping,ActionFormform,HttpServletReq...
action代码
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
int bytesun = 0;
int byteread = 0;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=db_hr";
String userName = "sa";
String password = "123";
Connection conn = DriverManager.getConnection(url,userName,password);
Statement stmt = conn.createStatement();
String name = request.getParameter("name");
String sql = "select name,pic from upload where name =" +"'"+ name +"'";
System.out.println(name);
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()){
InputStream inStream = rs.getBinaryStream("pic");
//设置输出的格式
String fileName = name.toString();
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachement;filname="+fileName);//设置默认名
//循环取出流中的数据
byte[] b = new byte[100];
int len;
while((len = inStream.read(b))>0)
{response.getOutputStream().write(b,0,len);}
inStream.close();
response.getOutputStream().flush();
response.getOutputStream().close();
}
} catch (Exception e) {
e.printStackTrace();
}
return mapping.findForward("success");
}
jsp代码
<form action="/sr/filedownloadAction.do" method="post">
<br/>
<table>
<tr>
<td>要导出的文件名:</td>
<td><%Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=db_hr";
String userName = "sa";
String password = "123";
Connection conn = DriverManager.getConnection(url,userName,password);
Statement stmt = conn.createStatement();
String name = request.getParameter("name");
String sql = "select name from upload";
ResultSet rs = stmt.executeQuery(sql);
%>
<select name="name">
<option value="">--请选择要下载的文件--</option>
<% while(rs.next()){%>
<option value="<%=rs.getString(1) %>"><%=rs.getString(1) %></option>
<% }%>
</select></td>
</tr>
<tr>
<td> </td>
<td>
<input type="button" value="导出" onclick="javascript:test(this.form);" /><br/>
</td>
</tr>
</table></td>
运行之后进行文件下载时总会出现java.lang.IllegalStateException: getOutputStream() has already been called for this response异常,网上说加out.clear()那个方法是jsp也面的 我这个要加在哪呢 展开
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
int bytesun = 0;
int byteread = 0;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=db_hr";
String userName = "sa";
String password = "123";
Connection conn = DriverManager.getConnection(url,userName,password);
Statement stmt = conn.createStatement();
String name = request.getParameter("name");
String sql = "select name,pic from upload where name =" +"'"+ name +"'";
System.out.println(name);
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()){
InputStream inStream = rs.getBinaryStream("pic");
//设置输出的格式
String fileName = name.toString();
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachement;filname="+fileName);//设置默认名
//循环取出流中的数据
byte[] b = new byte[100];
int len;
while((len = inStream.read(b))>0)
{response.getOutputStream().write(b,0,len);}
inStream.close();
response.getOutputStream().flush();
response.getOutputStream().close();
}
} catch (Exception e) {
e.printStackTrace();
}
return mapping.findForward("success");
}
jsp代码
<form action="/sr/filedownloadAction.do" method="post">
<br/>
<table>
<tr>
<td>要导出的文件名:</td>
<td><%Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=db_hr";
String userName = "sa";
String password = "123";
Connection conn = DriverManager.getConnection(url,userName,password);
Statement stmt = conn.createStatement();
String name = request.getParameter("name");
String sql = "select name from upload";
ResultSet rs = stmt.executeQuery(sql);
%>
<select name="name">
<option value="">--请选择要下载的文件--</option>
<% while(rs.next()){%>
<option value="<%=rs.getString(1) %>"><%=rs.getString(1) %></option>
<% }%>
</select></td>
</tr>
<tr>
<td> </td>
<td>
<input type="button" value="导出" onclick="javascript:test(this.form);" /><br/>
</td>
</tr>
</table></td>
运行之后进行文件下载时总会出现java.lang.IllegalStateException: getOutputStream() has already been called for this response异常,网上说加out.clear()那个方法是jsp也面的 我这个要加在哪呢 展开
1个回答
展开全部
response的outputStream已经被调用获取过了, 不能重复调用response.getOutputStream方法.
建议在第一个getOutputStream方法之前定义一个OutputStream out = response.getOutputStream(); 把所有response.getOutputStream()改为out
建议在第一个getOutputStream方法之前定义一个OutputStream out = response.getOutputStream(); 把所有response.getOutputStream()改为out
更多追问追答
追问
这个方法我试过了 还是会报异常
追答
错误提示中有没有指明是哪行报错?你说的那个out.clear()方法加在jsp的最后试试看
out.clear();
out=pageContext.pushBody();
并且我之前说的不能重复调用response.getOutputStream方法.也要改一下
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询