随笔-167  评论-65  文章-0  trackbacks-0

在做jsp图片上传到数据库和显示时,遇到

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

异常,查了下,原因在于我是在jsp里写的OutputStream的,应该放到Servlet中,jsp的本质不就是Servlet吗?..为什么放在不同为位置就会不一样了..郁闷~

还有这个异常,关于涉及到IO操作时会经常遇到,遇到和我相似的,不妨试试!

出错时的代码:写在jsp中的,功能是从数据库中读取图片 showimage.jsp

<%@ page contentType="text/html; charset=utf-8"%>
<%@ page import="java.io.*,java.sql.*,bean.*"%>
<%
    int id=Integer.parseInt(request.getParameter("id"));
    String sql = " SELECT photo FROM test WHERE id="+id;
    PreparedStatement pstmt = null;
    ConnectMysql connectMysql = new ConnectMysql();
    
    try {
            pstmt = connectMysql.openConnection().prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();            
            ServletOutputStream outs = response.getOutputStream();
            response.setContentType("image/jpeg");
        if (rs.next()) {
            Blob b = rs.getBlob("photo");
            long size = b.length();
            byte[] bs = b.getBytes(1, (int) size);
            outs.write(bs);
            outs.flush();            
            outs.close();
            rs.close();
        } else {
            rs.close();
        }
    } finally {
        pstmt.close();
        connectMysql.closeConnection();
    }
%>

改写到servlet中后,没出错:ImageServlet

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

        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        //PrintWriter out = response.getWriter();
        
        
        int id=Integer.parseInt(request.getParameter("id"));
        String sql = " SELECT photo FROM test WHERE id="+id;
        PreparedStatement pstmt = null;
        ConnectMysql connectMysql = new ConnectMysql();
        
        try {
                pstmt = connectMysql.openConnection().prepareStatement(sql);
                ResultSet rs = pstmt.executeQuery();            
                ServletOutputStream outs = response.getOutputStream();
                response.setContentType("image/jpeg");
            if (rs.next()) {
                Blob b = rs.getBlob("photo");
                long size = b.length();
                byte[] bs = b.getBytes(1, (int) size);
                outs.write(bs);
                outs.flush();            
                outs.close();
                rs.close();
            } else {
                rs.close();
            }
        } catch(Exception e){
            e.printStackTrace();
        }finally {
            connectMysql.closeConnection();
        }
        
        //out.flush();
        //out.close();
    }

注意用了OutputStream就不能使用PrintWriter了,因为都是输出流

顺便把mysql的存取图片介绍完整!

servlet部分:

接收处理客户端上传过来的photo:

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

        boolean flag = false;
        String path="";
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        try{        
        String photo=request.getParameter("photo").replace('\\', '/');
        Test test = new Test();
        test.setPhoto(photo);
        flag = DaoFactory.getTestDaoInstance().insert(test);
        if(flag){
            path = "success.jsp";
        }else{
            path = "fail.jsp";
        }
        request.getRequestDispatcher(path).forward(request, response);
        }catch(Exception e){
            e.printStackTrace();
        }

        out.flush();
        out.close();
    }

vo部分:

package vo;

public class Test {
    
    String photo;

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }
}

dao部分:

向数据库中插入图片:

    public boolean insert(Test test) throws Exception {
        Boolean flag=false;
        String sql = "INSERT INTO test(name,password,email,photo) VALUES(?,?,?,?)";
        PreparedStatement pstmt = null;
        ConnectMysql connectMysql = new ConnectMysql();
        FileInputStream fis;
        File file;
        try {
            file = new File(test.getPhoto());
            fis = new FileInputStream(file);
            
            pstmt = connectMysql.openConnection().prepareStatement(sql);
            pstmt.setString(1, "test");
            pstmt.setString(2, "test");
            pstmt.setString(3, "test");
            pstmt.setBinaryStream(4, fis, (int) file.length());
            pstmt.executeUpdate();
            pstmt.close();
            flag=true;
        } catch (Exception e) {
            e.printStackTrace();
            } finally {
            connectMysql.closeConnection();
        }
        return flag;
    }

调用的代码:

<td width="152" rowspan="3"><img src="showimage.jsp?id=0" width="140" height="140"></td>

把上面的showimage.jsp 改为ImageServlet就可以了,注意web.xml中servlet配置的路径.

ref:

http://www.coderanch.com/t/289883/JSP/java/IllegalStateException-getOutputStream-has-already-been



write by feng
posted on 2009-05-27 15:48 fl1429 阅读(1354) 评论(0)  编辑  收藏 所属分类: Jsp/Servlet/Javabean

只有注册用户登录后才能发表评论。


网站导航:
 
已访问数:
free counters