Detailed explanation of Java reading local files and displaying them in JSP files
When we are new to IMG tags, we know that by setting the src attribute of the img tag, we can display the pictures we want to display on the page. The value of src can be the absolute on the disk directory, the relative path under the project, or the image path on the network. When storing and accessing a small number of pictures, it is most convenient and practical to store pictures using relative paths. However, when there are too many pictures, this method will be somewhat constrained.
When the number of pictures in the system is too large, if these pictures are still published as part of the project, it will inevitably greatly extend the release time and update time of the project. For some systems with particularly high time limit requirements, it is not advisable to use relative paths to store pictures. Especially when the system releases patches, the system can only be updated in incremental ways. If the system is published in an overlay manner, it may cause the system image files to be lost.
Based on the above reasons, many systems will store a large number of images in the fixed directory of the server. In this way, when building a cluster environment, you can access public resources and avoid wasting hard disk space, but the problem also arises here. When we publish a good system to access pictures in a fixed directory on the disk, there is a prompt that there is no permission to access it. In order to ensure local security, the system does not allow direct access to pictures.
At first glance, some of the above questions are incredible. But if you think about it carefully, it makes sense for the system to do this. On the server, many system applications are deployed. It is impossible to have access to a system to easily access files on disk. In fact, the system on the server has permission to read files under other disk paths, but there is no way to let them display.
To solve the above problem, we can use streaming to read the image and then output it to the HTML page.
See the specific code below:
<img name="" alt="Avatar Photo" src="${}" style="width:160px;height:160px;border:1px solid" /> <button class="btn btn-primary" type="button" title="Upload pictures" onclick="uploadPersonPic()">Upload pictures</button> <script> $(function() { //Read avatar photos if($("#oid").val()!=="") { $("#personImg").attr("src","favccxx/person/loadUserPhoto?="+$("#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 (() != null) { // Set pictures try { Person person = ("oid", ()); FileInputStream is = new FileInputStream(()); imageStream = new BufferedInputStream(new FileInputStream(())); } catch (Exception e) { } } return SUCCESS; }
The above is an example of Java reading local files and displaying them. If you have any questions, please leave a message or go to the community of this site to communicate and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!