Java reads local files and displays them in JSP files.

Source: Internet
Author: User

When we are new to the IMG label, we know that by setting the src attribute of the img label, we can display the image to be displayed on the page. The src value can be the absolute value in the disk directory, the relative path in the project, or the image path on the network. When you access a small number of images, it is most convenient and practical to store images using relative paths. However, when there are too many images, this method is obviously a little constrained.


When the number of images in the system is too large, if you still use these images as part of the project for release, the release time and update time of the project will be greatly extended. For some systems with extremely high time limit requirements, it is not advisable to store images using relative paths. In particular, the system can only be updated incrementally when patches are released. If overwrite is used to publish the system, the system image files may be lost.


For the above reasons, many systems will store a large number of images in the fixed directory of the server. In this way, when a cluster environment is set up, public resources can be accessed to avoid the waste of hard disk space, but the problem arises here. The system prompts that the system has no permission to access images in a fixed directory on the disk. To ensure local security, the system does not allow direct access to images.


At first glance, the above problems are incredible. But if you think about it, the system does make sense. Many system applications are deployed on the server. It is impossible to have a system access permission, so you can access files on the disk at will. In fact, the system on the server has the permission to read files from other disk paths, but it cannot be displayed.


To solve the problem above, we can use the stream method to read the image and then output it to the HTML page. F. The key implementation code below is as follows:


JSP page display code:


 <button id =" btnImg "class =" btn-primary "type =" button "title =" Upload image "$ (function () {// read the avatar photo 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 using the Struts2 framework:

@ Action (value = "loadUserPhoto", results = {@ Result (name = "success", type = "stream", params = {"contentType", "image/jpeg ", "inputName", "imageStream"}) public String getImage () {if (person. getOid ()! = Null) {// set the image 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 ;}


This article from the "dust wind with the shadows of the Sky" blog, please be sure to keep this source http://genuinecx.blog.51cto.com/2890523/1282451

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.