<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圖片上傳到數(shù)據(jù)庫和顯示時,遇到

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

    異常,查了下,原因在于我是在jsp里寫的OutputStream的,應該放到Servlet中,jsp的本質(zhì)不就是Servlet嗎?..為什么放在不同為位置就會不一樣了..郁悶~

    還有這個異常,關于涉及到IO操作時會經(jīng)常遇到,遇到和我相似的,不妨試試!

    出錯時的代碼:寫在jsp中的,功能是從數(shù)據(jù)庫中讀取圖片 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部分:

    向數(shù)據(jù)庫中插入圖片:

        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;
        }
    

    調(diào)用的代碼:

    <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
    已訪問數(shù):
    free counters
    主站蜘蛛池模板: 亚洲免费在线视频观看| 亚洲国产精品专区在线观看| 亚洲AV无码国产剧情| 久久久久亚洲精品中文字幕| 日本在线看片免费人成视频1000| 亚洲欧美日韩中文字幕在线一区| 狠狠亚洲狠狠欧洲2019| 成人免费a级毛片| 99久久成人国产精品免费| 亚洲国产激情在线一区| 亚洲日韩精品一区二区三区无码 | 久久久久国色AV免费看图片| 日韩在线视频线视频免费网站| 亚洲的天堂av无码| 精品国产香蕉伊思人在线在线亚洲一区二区 | 免费国产不卡午夜福在线| 99精品热线在线观看免费视频| 青青视频免费在线| 亚洲香蕉久久一区二区| 亚洲国产精品无码av| 免费国产不卡午夜福在线| 国产成在线观看免费视频| 热99RE久久精品这里都是精品免费 | 亚洲乱色熟女一区二区三区丝袜| 四虎免费大片aⅴ入口| 1000部羞羞禁止免费观看视频| 成全视成人免费观看在线看| 免费在线观看亚洲| 国产成人亚洲综合一区| 久久久无码精品亚洲日韩按摩| 久久精品国产亚洲Aⅴ蜜臀色欲| 日日夜夜精品免费视频| 亚洲国产精品免费观看| 色猫咪免费人成网站在线观看| 一个人免费观看日本www视频| 亚洲精品天堂无码中文字幕| 亚洲一区二区三区在线| 亚洲自偷自偷精品| 久久久综合亚洲色一区二区三区| 在线亚洲精品福利网址导航| 亚洲AV无码一区二区三区国产|