SoFunction
Updated on 2025-04-07

Detailed explanation of JSP obtaining local image instance

Detailed explanation of JSP obtaining local image instance

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

The specific code is as follows:



public void showPicture(){ 
    String id = ().getParameter("id");//The primary key id of the entity class of the image path from the front desk    HttpServletResponse response = ();// struts2 get response    if(id != null && !"".equals(id)){ 
       = (id); 
      String pic_path = ();//Picture path      FileInputStream is; 
      try { 
        is = new FileInputStream(pic_path); 
        int i = (); // Get the file size        byte data[] = new byte[i]; 
        (data); // Read data        (); 
        ("image/*"); // Set the returned file type        OutputStream toClient = (); // Get the object that outputs binary data to the client        (data); // Output data        (); 
      } catch (FileNotFoundException e) { 
        (); 
      } catch (IOException e) { 
        (); 
      } 
    } 
  } 

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

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

If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!