java 通过流的方式读取本地图片并显示在jsp 页面上(类型以jpg、png等结尾的图片)

Java代码:

File filePic = new File(path+"1-ab1.png");
if(filePic.exists()){
   FileInputStream is = new FileInputStream(filePic);
   int i = is.available(); // 得到文件大小  
   byte data[] = new byte[i];  
   is.read(data); // 读数据  
   is.close();  
   response.setContentType("image/*"); // 设置返回的文件类型  
   OutputStream toClient = response.getOutputStream(); // 得到向客户端输出二进制数据的对象  
   toClient.write(data); // 输出数据  
   toClient.close();  
}

path:图片所在的文件夹目录

jsp 页面代码:

<img id="mutationImage" style="height:150px;750px;" src="${pageContext.request.contextPath}/two-check/validate-order-mutation/seeChrom.action?chromId=${request.chromId}&t=<s:property value='verifyResultNote'/>"/>

t=<s:property value='verifyResultNote'/>表示时间戳

原文地址:https://www.cnblogs.com/henuyuxiang/p/6567812.html