我们需要动态获取后台的图片,现举例如下:
后台的代码:
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>下载img</title>
</head>
<body>
<img src="http://localhost:8400/mlams/img1.jsp?NoDecorate=1" widht="300" height="250"/>
</body>
</html>
后台的代码:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@page import="java.io.*" %>
<%
try{
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
String img = "D:/jxgl1/mlams/WebRoot/img.jpg";
BufferedInputStream bis = null;
OutputStream os = null;
FileInputStream fileInputStream = new FileInputStream(new File(img));
bis = new BufferedInputStream(fileInputStream);
byte[] buffer = new byte[512];
response.reset();
response.setCharacterEncoding("UTF-8");
//不同类型的文件对应不同的MIME类型
response.setContentType("image/*");
//文件以流的方式发送到客户端浏览器
//response.setHeader("Content-Disposition","attachment; filename=img.jpg");
//response.setHeader("Content-Disposition", "inline; filename=img.jpg");
response.setContentLength(bis.available());
os = response.getOutputStream();
int n;
while ((n = bis.read(buffer)) != -1) {
os.write(buffer, 0, n);
}
bis.close();
os.flush();
os.close();
}catch(Exception e){
e.printStackTrace();
}
%>