在做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 |