我正在尝试从oracle数据库中获取图像。 流程是:JSP(读取photo_id) - > JSP - >它从HTML图像标记的src属性内部调用Servlet。
ImageGetter.jsp(要求提供照片ID)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>your Pic</title>
</head>
<body>
<form id="form2" enctype="multipart/form-data" action="getPhoto.jsp" method="get">
<table>
<tr>
<td>Enter Photo Id :</td>
<td><input type="text" name="id"/></td>
</tr>
</table>
<p/>
<input type="submit" value="fetch Photo"/>
</form>
</body>
</html>
getPhoto.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Your photo</title>
</head>
<body>
<% String p_id=request.getParameter("id");%>
<table>
<tr><td><%=p_id%></td></tr>
<tr><td><img src="/getPic?photoid=<%=p_id%>" /></td></tr>
</table>
</body>
</html>
img标签的src属性中的getPic是servlet。
getPic.java
package com.kp.imagehandler;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.kp.imagehandler.Image;
/**
* Servlet implementation class getPic
*/
public class getPic extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public getPic() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String imageId = request.getParameter("photoid");
System.out.println(imageId);
InputStream photoStream = (new Image()).getImageStream(imageId);
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
// Open streams
input = new BufferedInputStream(photoStream, 200000);
output = new BufferedOutputStream(response.getOutputStream(),
200000);
// Write file contents to response.
byte[] buffer = new byte[200000];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} finally {
output.close();
input.close();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
Image.java:
package com.kp.imagehandler;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Image {
// Init ---------------------------------------------------------------------------------------
public InputStream getImageStream(String pid)
{
InputStream IS=null;
try {
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@10.75.122.69:1521:devdb",
"s3", "s3");
Statement stmt = connection.createStatement ();
ResultSet rset = stmt.executeQuery ("SELECT photo FROM photo_holder where id="+pid);
while(rset.next())
{
IS=rset.getBinaryStream("photo");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return IS;
}
// Implement default getters and setters here.
}
web.xml:
<servlet>
<description>getPic</description>
<display-name>getPic</display-name>
<servlet-name>getPic</servlet-name>
<servlet-class>com.kp.imagehandler.getPic</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getPic</servlet-name>
<url-pattern>/getPic</url-pattern>
</servlet-mapping>
表格定义:
create table PHOTO_HOLDER
(
ID NUMBER(5) not null,
TITLE VARCHAR2(50),
PHOTO BLOB
)
但是在点击获取照片之后,我只是获取了照片ID而不是图像。 在这里,我不确定servlet是否会被解雇。
请提前帮助。谢谢。
答案 0 :(得分:0)
为图片形成的网址可能存在问题。
请在浏览器中查看生成的JSP的源代码,再次验证它。
只需调试代码或使用日志记录来调查问题。
在Servlet / JSP路径之前附加上下文路径后再试一次,如下所示:
<form id="form2" enctype="multipart/form-data" action="<%=request.getContextPath() %>/getPhoto.jsp" method="get">
<img src="<%=request.getContextPath() %>/getPic?photoid=<%=p_id%>" />