Recently, IO streams are needed to read pictures in the project to provide foreground page display. Since I used to use the URL path to display pictures, I heard that IO streams need to read pictures in the project. However, the task has been issued, and as a programmer, I can only choose to execute it, so I found some information and read the API.
Hey, it feels quite simple. Since it is the first time IO streaming is used to read pictures for page display, so record the following code
Background code:
/** * IO stream reads the image by:long * @return */@RequestMapping(value = "/IoReadImage/{imgName}", method = RequestMethod.GET) public String IoReadImage(@PathVariable String imgName,HttpServletRequest request,HttpServletResponse response) throws IOException {ServletOutputStream out = null;FileInputStream ips = null;try {//Get the image storage path String imgPath = Constans.FOLDER_IMAGE + imgName;ips = new FileInputStream(new File(imgPath));response.setContentType("multipart/form-data");out = response.getOutputStream();//Read file stream int len = 0;byte[] buffer = new byte[1024 * 10];while ((len = ips.read(buffer)) != -1){out.write(buffer,0,len);}out.flush();}catch (Exception e){e.printStackTrace();} finally {out.close();ips.close();}return null;}Front Desk Code - Method 1:
<span style="white-space:pre;"> </span><div style="float: left;"> <#--${model.userDatil.photo} The file name stored for the database --> <img src="${ctx}/userInfo/IoReadImage/${model.userDatil.photo}" id="npcImg"/> <input type="hidden" id="photo" name="photo"/> </div>js code - Method 2:
var npcName = $('#npcImg').data('val'); var img = document.getElementById("npcImg"); img.src = '/userInfo/IoReadImage/'+npcName;jQuery code - Method 3:
$('#npcImg').attr('src','/userInfo/IoReadImage/'+npcName);OK, it's that simple, the front desk can display the pictures. There are only a few sentences of code in total, so there are no additional comments to explain it.
Summarize
This article is full of the content about reading pictures for the front-end display code sharing in Java IO streams. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!