第一步:建立一個serverlet,用於獲得圖片流。代碼如下:
public class ImageServlet extends HttpServlet {
public ImageServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
// PrintWriter out = response.getWriter();
VideoService service=new VideoService();
String id=request.getParameter("id");
byte[] imageData=service.getImageDataById(id);
if (imageData != null)
{
response.setContentType("image/png");
OutputStream stream = response.getOutputStream();
stream.write(imageData);
stream.flush();
stream.close();
}
else
{
response.setContentType("text");
response.getWriter().write("attribute byArr not found");
}
}
public void init() throws ServletException {
// Put your code here
}
}
第二步:jsp部分代碼如下:
<html>
<head>
</head>
<body>
<%
VideoService service=new VideoService();
ArrayList<VideoBean> dataLst=service.getVideListInfoByType("100","1","5");
if(dataLst==null){
System.out.println("dataLst is null...");
}else{
for(int i=0;i<dataLst.size();i++){
%>
<%=dataLst.get(i).getName()%><br/>
<img alt="圖片" src="imageTest?id=<%=dataLst.get(i).getId()%>"><br/>
<%
System.out.println("name:"+dataLst.get(i).getName());
}
}
%>
</body>
</html>