<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
    主站蜘蛛池模板: 精品国产呦系列在线观看免费| 亚洲人成在线影院| 亚洲第一综合天堂另类专| 99久久人妻精品免费二区| 亚洲AV无码不卡无码| a级毛片无码免费真人久久| 国产成人A亚洲精V品无码| 又粗又长又爽又长黄免费视频 | 国产大片91精品免费看3| 亚洲精品无码久久久久A片苍井空| 18禁免费无码无遮挡不卡网站| 亚洲伊人久久大香线蕉在观| 国产卡一卡二卡三免费入口| 亚洲丰满熟女一区二区哦| 免费一级成人毛片| 中文字幕不卡免费视频| 久久亚洲国产伦理| 免费观看美女用震蛋喷水的视频| 亚洲天堂福利视频| 日本牲交大片免费观看| 一级做a爱过程免费视| 国产V亚洲V天堂A无码| 最刺激黄a大片免费网站| 激情亚洲一区国产精品| 日本高清免费aaaaa大片视频| 美女视频黄a视频全免费网站色| 综合亚洲伊人午夜网| 精品国产污污免费网站 | 俄罗斯极品美女毛片免费播放| 一级做a毛片免费视频| 亚洲福利在线视频| 毛片免费观看视频| 九九综合VA免费看| 亚洲福利视频一区二区三区| 国内精品免费视频自在线| 一级毛片免费一级直接观看| 亚洲av综合av一区| 成人人观看的免费毛片| 三级毛片在线免费观看| 一本色道久久88亚洲精品综合 | 在线看片免费人成视久网|