当前位置:网站首页>Display the server hard disk image to the browser through Servlet

Display the server hard disk image to the browser through Servlet

2022-07-07 23:36:00 Programmer community


In fact, this example is very simple , But someone asked me today , And I also wrote a small example , Just send it by the way !
Display a picture in the browser , Use a label

<img src="">

 

img Element embeds an image into a web page .
Please note that , Technically speaking ,<img> Tags don't insert images into web pages , It's about linking images from a web page .<img> The label creates the space occupied by the referenced image .
<img> Tags have two required properties :src attribute and alt attribute .

 

HTML And XHTML Differences between
stay HTML in ,<img> The tag has no end tag .
stay XHTML in ,<img> The tag must be closed correctly .
stay HTML 4.01 in , It is not recommended to use image Elemental "align"、"border"、"hspace" as well as "vspace" attribute .
stay XHTML 1.0 Strict DTD in , I won't support it image Elemental "align"、"border"、"hspace" as well as "vspace" attribute .

 

SRC There are many paths to :
Point to other sites ( such as src="http://www.******.com/***.jpg")
Point to files in the site ( such as src="/i/image.gif")
What many novices overlook is , Actually IMG Just tell the browser to have realistic pictures here , The browser gets the data stream of the picture through the path and then displays it
Simply speaking ,SRC In fact, the browser made a request , Then the request returns the data stream of the image to the browser

 

therefore ,SRC It can also be a request , It can be Servlet It can also be Action, Here we use Servlet Let's take a simple example
JSP page :

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">    <title> Picture shows </title>  </head>  <body>    <img src="<%=basePath %>servlet/ImageShowServlet">  </body></html>

 

Web.xml To configure :

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"  xmlns="http://java.sun.com/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  <servlet>    <servlet-name>ImageShowServlet</servlet-name>    <servlet-class>servlet.ImageShowServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>ImageShowServlet</servlet-name>    <url-pattern>/servlet/ImageShowServlet</url-pattern>  </servlet-mapping>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

 

Servlet It's simple :

package servlet;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * @ explain   The Servlet Input the picture of the local hard disk into the pipeline  * @author cuisuqiang * @version 1.0 * @since */@SuppressWarnings("serial")public class ImageShowServlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException {  OutputStream os = response.getOutputStream();  File file = new File("C:\\abc.jpg");  FileInputStream fips = new FileInputStream(file);  byte[] btImg = readStream(fips);  os.write(btImg);  os.flush(); }  /**  *  Read the stream data in the pipeline   */ public byte[] readStream(InputStream inStream) {  ByteArrayOutputStream bops = new ByteArrayOutputStream();  int data = -1;  try {   while((data = inStream.read()) != -1){    bops.write(data);   }   return bops.toByteArray();  }catch(Exception e){   return null;  } }}

 

Is to get the byte stream of the file on the local hard disk , Then write it into the pipeline ! 

I recommend you to read more about “ action servlet img flow Cui suqiang ” The article

原网站

版权声明
本文为[Programmer community]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130557548378.html