`

Servlet读取图片

 
阅读更多

Servlet中对图片流的输出

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String isbn = request.getParameter("isbn");
		BookDao bookDao = new BookDao();
		try {
			InputStream image = bookDao.getImage(isbn);
			OutputStream out = response.getOutputStream();
			int num;
			byte buf[] = new byte[1024];
			while ((num = image.read(buf)) != -1) {
				out.write(buf, 0, num);
				System.out.println(111);
			}
			out.flush();
			out.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

 在jsp页面中,只有使用<img>标签获取图片信息即可

<img alt="" src="${pageContext.request.contextPath}/ShowServlet?isbn=<%=isbn%>">

 在jsp中的路径设置,可以参考如下

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics