img 显示response图片数据

[url]http://blog.csdn.net/ykf69177/article/details/8655881[/url]

<script type="text/javascript">  
loadImg=function(){
document.getElementById("img").src="servlet/IdentityServlet?ts="+new Date().getTime();
}
</script>

<body>
<img src="servlet/IdentityServlet" id="img" onclick="loadImg()"/>
</body>



//想要返回图片的路径  
FileInputStream fis = new FileInputStream("/Users/kun/Desktop/ServerImage/lxj12345.jpeg") ;
//得到文件大小
int size = fis.available();
byte data[] = new byte[size] ;
fis.read(data) ;
fis.close();

//设置返回的文件类型
response.setContentType("image/jpeg");
OutputStream os = response.getOutputStream() ;
os.write(data);
os.flush();
os.close();



public class IdentityServlet extends HttpServlet {  

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("image/jpeg"); //设置输出类型

String randomString = getRandomStr(); //获取随机字符串
request.getSession(true).setAttribute("randomString", randomString);

int width =100,height=30; //设置图片宽高

Color color = getRandomColor(); //背景色
Color reverse = getReverseColor(color); //反色,用于前景色

BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); //创建一个彩色图片

Graphics2D graph = bi.createGraphics(); //获取绘图对象
graph.setFont(new Font(Font.SANS_SERIF,Font.BOLD,16)); //设置字体
graph.setColor(color); //绘制颜色
graph.fillRect(0, 0, width, height); //绘制背景
graph.setColor(reverse); //绘制颜色
graph.drawString(randomString, 18, 20); //绘制随机字符串

/**
* 绘制噪音点
*/
for(int i = 0; i<100; i++){
graph.drawRect(random.nextInt(width),random.nextInt(height), 1,1);
}

ServletOutputStream out = response.getOutputStream(); //转换成JPEG格式
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out); //编码器
encoder.encode(bi);

out.flush();
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request,response);
}

public static char[] CHARS ={
//'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'0','1','2','3','4','5','6','7','8','9'}; //随机字符字典

private Random random = new Random(); //随机数对象

/**
* 获取6位随机数
* @return
*/
private String getRandomStr(){
StringBuffer buffer = new StringBuffer();
for(int i=0;i<6;i++){
buffer.append(CHARS[random.nextInt(CHARS.length)]);
}
return buffer.toString();
}

/**
* 获取随机的颜色
* @return
*/
private Color getRandomColor(){
return new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255));
}

/**
* 返回某颜色的反色
* @param color
* @return
*/
private Color getReverseColor(Color color){
return new Color(255-color.getRed(),255-color.getGreen(),255-color.getBlue());
}
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jie310600

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值