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
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值