Detailed examples of JSP capturing local images

Time:2019-5-9

Detailed examples of JSP capturing local images

IE version 7 or above does not support writing local hard disk addresses directly on SRC to display pictures. Because we only display pictures in the foreground by reading the binary stream in response in the background.

The code is as follows:

public void showPicture(){ 
    String id = ServletActionContext. getRequest (). getParameter ("id"); and // the primary key ID of the stored image path entity class from the foreground 
    HttpServletResponse = ServletActionContext. getResponse (); //struts2 gets response 
    if(id != null && !"".equals(id)){ 
      this.classicCases = this.classicCasesManager.findClassicCasesById(id); 
      String pic_path = this. classicCases. getImagesLocalPath ();// Picture Path 
      FileInputStream is; 
      try { 
        is = new FileInputStream(pic_path); 
        Int i = is. available (); // Get file size 
        byte data[] = new byte[i]; 
        Is. read (data); // read data 
        is.close(); 
        Response.setContentType ("image/*"); // Sets the file type returned 
        OutputStream to Client = response. getOutputStream (); // Gets the object that outputs binary data to the client 
        ToClient. write (data); // Output data 
        toClient.close(); 
      } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
      } 
    } 
  }

The JSP page is simple, and the path format is http://localhost:8080/projectName/*.action:prama=XXX.


<img alt="" src="<%=basePath %>ClassicCasesAction!showPicture.action?id=${classicCases.id}"> 

If you have any questions, please leave a message or go to our community for discussion. Thank you for reading. I hope I can help you. Thank you for your support.