您现在的位置:首页 >> 建站经验 >> 内容

回一次帖变一次图的BBS常用代码(刷新变图)

时间:2009/9/14 11:38:34 点击:2984

  核心提示:回一次帖变一次图的BBS常用代码%@LANGUAGE=VBSCRIPT CODEPAGE=936% br/% br/---------------------------------------------------------------------------------------- br/...

回一次帖变一次图的BBS常用代码

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <br/><% <br/>---------------------------------------------------------------------------------------- <br/>使用实例:img.asp?jn=图片存放目录 <br/>使用注意:图片目录放于本文件同目录下!需要FSO支持! <br/>说明:只要将变化图片放于一个目录下,修改下面程序的(需要修改的地方)成你的目录名称即可。<br/>---------------------------------------------------------------------------------------- <br/>Function AllPath() <br/>   Dim Domain,GFilePath <br/>   Domain = Request.ServerVariables("SERVER_NAME") <br/>   GFilePath = Request.ServerVariables("PATH_INFO") <br/>   GFilePath = lcase(left(GFilePath,instrRev(GFilePath,"/"))) <br/>   AllPath = "http://"&Domain&GFilePath <br/>End Function <br/>   <br/>Function ShowFilejn(folderspec) <br/>   Dim Path,objFSO,objFolder,count,objFile,nume,S <br/>   Path = Server.MapPath(folderspec) <br/>   Set objFSO = Server.CreateObject("Scripting.FileSystemObject") <br/>   If objFSO.FolderExists(Path) Then <br/>   Set objFolder = objFSO.GetFolder(Path) <br/>   count = 0 <br/>   For Each objFile in objFolder.Files <br/>   count = count+1 <br/>   Next <br/>   randomize <br/>   nume = Int((count*rnd)+1) <br/>   S = 0 <br/>   ShowFilejn = "" <br/>   For Each objFile in objFolder.Files <br/>   S = S + 1 <br/>   If S = nume Then <br/>   ShowFilejn = objFile.Name <br/>   Exit For <br/>   End If <br/>   Next <br/>   Set objFolder = Nothing <br/>   Else <br/>   ShowFilejn = "NO" <br/>   End If <br/>   Set objFSO = Nothing <br/>End Function <br/>   <br/>Dim jn,filename,address,str <br/>   <br/>jn = trim(Request.QueryString("jn")) <br/>if jn = "" then <br/>   Response.write "本页需要正确参数引入,您缺少相关的参数!正确格式如下:"&AllPath&"img.asp?jn=(随便改,告诉别人路径就成)" <br/>   Response.End() <br/>end if <br/>   <br/>filename = ShowFilejn("./"&jn&"/") <br/>if filename = "NO" then <br/>   Response.write "您指定的目录<b>"&jn&"</b>不存在,请重新指定!" <br/>   Response.End() <br/>end if <br/>   <br/>if filename = "" then <br/>   Response.write "您指定的目录<b>"&jn&"</b>没有相关的图片文件存在,请重新指定!" <br/>   Response.End() <br/>end if <br/>   <br/>str = right(filename,3) <br/>if str<>"jpg" and str<>"gif" then <br/>filename = "erro.gif" <br/>end if <br/>   <br/>address = AllPath&jn&"/" <br/>address = address&filename <br/>%> <br/><%Response.redirect(address)%><br/>


 

回贴就变的图,现在在论坛里很吃香的。其实并不象你想象的那样,回贴就变。即使你不回复,每刷新一次页面那副图都会变,那为什么会这样呢?其实用Servlet来实现这种效果是非常简单的。看看下面的代码:

import java.awt.Color;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream; import javax.servlet.Servlet;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageDecoder;import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class CreateImage extends HttpServlet implements Servlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Cache-Control","no-store"); response.setContentType("image/jpeg"); ServletOutputStream out=response.getOutputStream(); String imagePath=request.getSession().getServletContext().getRealPath("images")+"\\"+getRandom()+".jpg"; InputStream imageIn = new FileInputStream(new File(imagePath)); JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn); BufferedImage image = decoder.decodeAsBufferedImage(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.close(); } public String getRandom(){ int i1 = (int)(java.lang.Math.random()*10); int i2 = (int)(java.lang.Math.random()*10); return String.valueOf(i1)+String.valueOf(i2); }}

在Images目录下放入100个从00-99命名的jpg文件,然后你在页面中使用<image src="CreateImage">来调用,每次刷新就会出现不同的图片,当然偶尔也会出现相同的:)。其实这个原理很简单,将Servlet的类型设为图片型,然后每次随机以流的形式取出一副jpg文件,然后再输出出去。一般论坛上面使用的也都是这种类型的,不信你把把这副图片的网络地址以插入图片的形式加到你的贴子里试试。
当然有时我们可能会想在图片上动态加入一些文字,那应该怎么,其实也很简单,就是利用Graphics这个类来对图片内容进行修改,如下:
import java.awt.Color;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;
import javax.servlet.Servlet;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageDecoder;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class CreateImage extends HttpServlet implements Servlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Cache-Control","no-store"); response.setContentType("image/jpeg"); ServletOutputStream out=response.getOutputStream(); String imagePath=request.getSession().getServletContext().getRealPath("images")+"\\01.jpg"; InputStream imageIn = new FileInputStream(new File(imagePath)); JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn); BufferedImage image = decoder.decodeAsBufferedImage(); Graphics g = image.getGraphics(); g.setColor(new Color(255,0,0)); //更改输出字体的颜色 g.setFont(new Font("Default",Font.BOLD,30)); //更改字体大小 g.drawString("Hello",30,100); //在图片上坐标0,13的位置上输出"Hello everyone!" JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.close(); }

作者:冷月湖 录入:冷月湖 来源:原创
相关评论
发表我的评论
  • 大名:
  • 内容:
  • 亿万站网址导航-站长之家(www.yiwanzhan.cn) © 2006-2024 版权所有 All Rights Reserved.
  • Email:xiegr71@163.com 站长QQ:329700870 京ICP备07500142号