Servlet代码:
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class profile_photo extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession ses=request.getSession();
String email = (String)ses.getAttribute("ses_email");
String imgLen="";
Connection cn;
PreparedStatement ps;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn=DriverManager.getConnection("jdbc:odbc:sm","system","gecbsp");
ps=cn.prepareStatement("select photo_loc from smreg3 where email = "+
"'"+email+"'");
ResultSet rs = ps.executeQuery();
if(rs.next()){
imgLen = rs.getString(1);
System.out.println(imgLen.length());
}
ps=cn.prepareStatement("select photo_loc from smreg3 where email = "+
"'"+email+"'");
ResultSet rs2 = ps.executeQuery();
if(rs2.next()){
int len = imgLen.length();
byte [] rb = new byte[len];
InputStream readImg = rs2.getBinaryStream(1);
int index=readImg.read(rb, 0, len);
System.out.println("index"+index);
ps.close();
response.reset();
response.setContentType("image/jpg");
response.getOutputStream().write(rb,0,len);
response.getOutputStream().flush();
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
Jsp代码:
<img src="${pageContext.request.contextPath}/profile_photo" />
错误:
java.lang.IllegalStateException: getWriter() has already been called for this response
at org.apache.catalina.connector.Response.getOutputStream(Response.java:573)
at org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:183)
at profile_photo.doGet(profile_photo.java:59)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
问题:
如何从ImageServlet中检索JSP中的图像? 以上方法是否正确从数据库中检索图像?或者有更好的方法吗?
答案 0 :(得分:0)
// try below change
response.setContentType("application/octet-stream");
response.setContentLength(len);
OutputStream outputStream = response.getOutputStream();
outputStream.write(rb,0,len);
outputStream.flush();
答案 1 :(得分:0)
当您声明out
但似乎没有使用它时,您应该删除该行
PrintWriter out = response.getWriter();
拨打getOutputStream()
后,您无法在回复时致电getWriter()
。