Java reads local files and displays them in JSP files

  • 2020-10-07 18:41:31
  • OfStack

Java reads the local file and displays it in the JSP file

When we first learned about the IMG tag, we knew that by setting the src property of the img tag, we could display the image we wanted on the page. The value of src can be an absolute on the disk directory, a relative path under the project, or an image path on the network. In the case of access to a small number of images, the use of relative path storage images in the case of the most convenient and practical. But when there are too many images, this approach becomes more restrictive.

When the number of images in the system is too large, if these images are still published as part 1 of the project, it is bound to greatly prolong the release time and update time of the project. For some systems that require extremely high timings, it is not advisable to store images in relative paths. In particular, when the system issued patches, can only use incremental way to update the system. If the system is published by overwriting, it may cause the loss of system image files.

For all the above reasons, many systems store a large number of images in a fixed directory on the server, so that when setting up a cluster environment, they can access common resources and avoid wasting hard disk space, but this is where the problem comes in. We posted good system access to the disk under the fixed directory of the image, there is no permission to access the prompt. To ensure local security, the system does not allow direct access to the image.

At first glance, the above questions are a little weird. But if you think about it, it makes sense for the system to do that. On the server, a number of system applications are deployed. It is impossible to have a single system access that allows you to access files on disk. In fact, the system on the server has permission to read files in other disk paths, but there is no way to make it appear.

To solve the above problem, we can use the stream to read the image and then output it to the HTML page.

The specific code is shown below:


<img name="person.personImg" id="personImg" alt=" Profile pictures " src="${person.personImg}" style="width:160px;height:160px;border:1px solid" /> 
<button id="btnImg" class="btn btn-primary" type="button" title=" To upload pictures " onclick="uploadPersonPic()"> To upload pictures </button> 
 
<script> 
  $(function() { 
    // Read profile photos  
    if($("#oid").val()!=="") 
    { 
      $("#personImg").attr("src","favccxx/person/loadUserPhoto?externalPerson.oid="+$("#oid").val()+"&Time="+(new Date().getTime())); 
      $("#personImg").css("display",""); 
      $("#btnImg").css("display",""); 
    }else{ 
      $("#personImg").css("display","none"); 
      $("#btnImg").css("display","none");      
    } 
  } 
</script> 

Java code:


@Action(value = "loadUserPhoto", results = { @Result(name = "success", type = "stream", params = { 
      "contentType", "image/jpeg", "inputName", "imageStream" }) }) 
  public String getImage() { 
    if (person.getOid() != null) { 
      //  Set the picture  
      try { 
        Person person = personService.findOne("oid", externalPerson.getOid()); 
        FileInputStream is = new FileInputStream(person.getPersonImg()); 
        imageStream = new BufferedInputStream(new FileInputStream(person.getPersonImg())); 
      } catch (Exception e) { 
      } 
    } 
    return SUCCESS; 
  } 

Above is the example of java to read local files and display, if you have any questions, please leave a message or to this site community exchange discussion, thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: