<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-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 閱讀(1374) 評論(0)  編輯  收藏 所屬分類: Jsp/Servlet/Javabean
    已訪問數:
    free counters
    主站蜘蛛池模板: 国产成人免费网站在线观看| 亚洲视频在线观看| 国内精自视频品线六区免费 | 黄色网址大全免费| 亚洲午夜视频在线观看| 亚洲第一永久AV网站久久精品男人的天堂AV| 日韩视频在线观看免费| 色老头综合免费视频| 精品亚洲456在线播放| 亚洲天堂中文资源| 亚洲精品无码Av人在线观看国产| 在线免费观看国产视频| 国产精彩免费视频| 在线看片免费人成视频播| 亚洲av乱码中文一区二区三区| 亚洲国产精品专区| 综合自拍亚洲综合图不卡区| 亚洲精品成人片在线观看精品字幕 | 国产精品99久久免费观看| 免费看又黄又爽又猛的视频软件| 亚洲人精品亚洲人成在线| 亚洲成a人片7777| 亚洲一区二区三区日本久久九| 久久精品国产亚洲综合色| 最新亚洲成av人免费看| 亚洲AV无码成H人在线观看| 国产伦一区二区三区免费| 国产成人精品免费直播| 日韩一级免费视频| 国产男女爽爽爽爽爽免费视频| 国产精品区免费视频| 日韩插啊免费视频在线观看| 一级毛片成人免费看免费不卡| 成人网站免费看黄A站视频| 在线免费播放一级毛片 | www国产亚洲精品久久久日本| 日韩成全视频观看免费观看高清 | 精品国产_亚洲人成在线高清| 亚洲午夜久久久影院| 亚洲人成人77777网站| 国产AV无码专区亚洲A∨毛片|