SERVLET显示图片

1. 工具类

package com.chis.web.util;

import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

public class ImgUtil {

	public static final String JPG = "jpg";
	public static final String PNG = "png";
	public static final String BMP = "bmp";
	public static final String GIF = "gif";

	/**
	 * 在servlet中调用该方法, jsp页面中img标签的src指向该servlet, 则会显示图片
	 * 
	 * @param response
	 * @param path
	 * @param isResponseClose
	 */
	public static void showImage(HttpServletResponse response, String path, boolean isResponseClose) {
		try {
			ServletOutputStream outStream = response.getOutputStream();// 得到向客户端输出二进制数据的对象
			FileInputStream fis = new FileInputStream(path); // 以byte流的方式打开文件
			// 读数据
			byte data[] = new byte[1000];
			while (fis.read(data) > 0) {
				outStream.write(data);
			}
			fis.close();
			response.setContentType("image/*"); // 设置返回的文件类型
			outStream.write(data); // 输出数据
			if (isResponseClose) {
				outStream.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 在servlet中调用该方法, jsp页面中img标签的src指向该servlet, 则会显示图片
	 * 
	 * @param response
	 * @param data
	 * @param isResponseClose
	 */
	public static void showImage(HttpServletResponse response, byte[] data, boolean isResponseClose) {
		try {
			ServletOutputStream outStream = response.getOutputStream();// 得到向客户端输出二进制数据的对象
			// 读数据
			outStream.write(data);
			response.setContentType("image/*"); // 设置返回的文件类型
			outStream.write(data); // 输出数据
			if (isResponseClose) {
				outStream.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 在servlet中调用该方法, jsp页面中img标签的src指向该servlet, 则会显示图片
	 * 
	 * @param response
	 * @param image
	 * @param imgType
	 * @param isResponseClose
	 */
	public static void showImage(HttpServletResponse response, BufferedImage image, String imgType, boolean isResponseClose) {
		try {
			ImageIO.write(image, imgType, response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

2.servlet

package com.chis.web.util;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ImgServlet extends HttpServlet {

	private static final long serialVersionUID = 3728441236122484282L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String imgPath = request.getParameter("imgPath");// 输出图片的类型的标志
		if(null != imgPath && !"".equals(imgPath.trim())) {
			ImgUtil.showImage(response, imgPath, true);  
		}

	}
}

3. 页面调用 showImg.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>showPic.html</title>
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="this is my page">
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	</head>

	<body style="overflow-x: hidden;overflow-y: hidden;margin: 0;padding: 0;">
	</body>
</html>	
<script> 
function request(paras){ 
var url = location.href;  
var paraString = url.substring(url.indexOf("?")+1,url.length).split("&");  
var paraObj = {}  
for (i=0; j=paraString[i]; i++){  
paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=")+1,j.length);  
}  
var returnValue = paraObj[paras.toLowerCase()];  
if(typeof(returnValue)=="undefined"){  
return "";  
}else{  
return returnValue;  
}  
} 

window.οnlοad=function() {
var len=document.documentElement.clientWidth-40;
var hit=document.documentElement.clientHeight-10;
var addr=request('addr'); 
document.writeln("<img src='/ImgServlet?imgPath="+addr+"' border=0 width='"+len+"' height='"+hit+"'/>");
}

</script>	



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用 Servlet 下载图片并将其显示的示例代码: ```java @WebServlet("/image") public class ImageServlet extends HttpServlet { private static final int BUFFER_SIZE = 4096; private String imagePath; @Override public void init() throws ServletException { // 设置图片路径为 web.xml 中的参数 imagePath = getServletContext().getInitParameter("imagePath"); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取图片的名称 String imageName = request.getParameter("imageName"); // 设置响应的内容类型为图片类型 response.setContentType("image/jpeg"); // 获取图片的绝对路径 String filePath = imagePath + imageName; // 打开文件输入流 InputStream inputStream = new FileInputStream(filePath); // 打开响应的输出流 OutputStream outputStream = response.getOutputStream(); // 缓冲数组 byte[] buffer = new byte[BUFFER_SIZE]; // 读取输入流并写入输出流 int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } // 关闭输入流和输出流 inputStream.close(); outputStream.close(); } } ``` 在 web.xml 文件中设置图片路径: ```xml <context-param> <param-name>imagePath</param-name> <param-value>/path/to/images/</param-value> </context-param> ``` 在 JSP 页面中使用以下代码来显示图片: ```html <img src="${pageContext.request.contextPath}/image?imageName=image.jpg" /> ``` 其中,`imageName` 参数是图片的名称,`/image` 是 Servlet 的映射路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值