java.lang.IllegalStateException: getOutputStream()

2020-01-29 10:30发布

I get the following exception when I'm trying to request loading images from server on client side:

241132533 [TP-Processor1] ERROR [/jspapps].[jsp] - Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException: getOutputStream() has already been called for this response

Can any one explain this exception to me and also how to get over it?

标签: java jsp
11条回答
Lonely孤独者°
2楼-- · 2020-01-29 11:12

I just stumbled upon this old question as I had the same issue. In the end it was quite easy to get rid of the exception: Just call out.clear() before:

out.clear();
...
// later, in a different method
ServletOutputStream out = response.getOutputStream();
...

out.clear() also helped me to get rid of all those empty lines from <%@page import=... and the like.

查看更多
戒情不戒烟
3楼-- · 2020-01-29 11:16

I had this code and fixed it like this:

@RequestMapping(value = "xyz", method = RequestMethod.POST)
public String generateReport(HttpServletResponse response, @Valid @ModelAttribute Form form, Errors errors, Model model) {
    if (errors.hasErrors()) {
        model.addAttribute(form);
        return "abcd/xyz";
    } else {
        someMethodWhichUsesResponse(response);
    }
Earlier:
    return "abcd/xyz";
Fixed by:
    return null;
}

I returned null because, from this method, I was expecting a download. As per the explanation given here, I fixed my problem.

查看更多
何必那么认真
4楼-- · 2020-01-29 11:20

Make sure eliminating all output in your view_image.jsp. Simple line breaks can be responsible for generating output.

For example, if you have these declarions...

<%@ page import ... %>
<%@ page import ... %>

... you should write them this way

<%@ page import ... %><%@ page import ... %><%
...%>

If you take a look to the compiled servlet code you shouldn't see out.write("\r\n") before your image response.

A better way would be to change your view_image.jsp into a Servlet, but if you can't do that, removing the line breaks in the jsp is a workaround.

查看更多
欢心
5楼-- · 2020-01-29 11:20
    <%@page import="java.sql.DriverManager"%>
    <%@page import="java.io.InputStream"%>
    <%@page import="java.sql.Connection"%>
    <%@page import="java.sql.PreparedStatement"%>
    <%@page import="java.sql.ResultSet"%>
    <%@page import="java.sql.Statement"%>

  <%
    Connection con=null;
    ResultSet rs = null;
    Statement st = null;
    String sql = null;
    try {
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("test","root","root"); 
            st = con.createStatement();
            sql = "select image from projects where projectid='1'";
            System.out.println(sql);
            rs = st.executeQuery(sql); 
            String imgLen = "";
            out.clear();
            while (rs.next()) 
            {
                imgLen = rs.getString(1);
                System.out.println(imgLen.length());
                int len = imgLen.length();
                byte[] rb = new byte[len];
                InputStream readImg = rs.getBinaryStream(1);
                int index = readImg.read(rb, 0, len);
                response.reset();
                response.setContentType("image/jpg");
                response.getOutputStream().write(rb, 0, len);
                response.getOutputStream().flush();
                response.getOutputStream().close();
            }
            st.close();
            rs.close();
            if (true) return;
        } catch (Exception e) {e.printStackTrace();}
    %>
查看更多
Evening l夕情丶
6楼-- · 2020-01-29 11:22

can any one explain this exception to me

You're attempting to write binary data to response.getOutputStream() using raw Java code inside a JSP file which itself is already using response.getWriter() to write any template text. See also the Throws part of the linked javadocs.

and also how to get over it?

Write Java code in a real Java class instead. Create a class which extendsHttpServlet, move all that Java code to there, map it in web.xml and change the request URL to call the servlet instead.

See also:

查看更多
登录 后发表回答