jsp把图象保存到Oracle数据库中并读出直接显示到Jsp页面
jsp教程把图象保存到Oracle数据库教程中并读出直接显示到Jsp页面
结合后的图像数据,并在调用的网页上显示出图文结合后的图像(注:该servlet仅实现了JPG格式图像文件的处理,不支持GIF):
package net.xdevelop.merge;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
import net.xdevelop.util.ParamUtil;
/**
* 将文字用指定的字体,颜色和大小,嵌入指定图片的指定位置,调用参数:
* text: 要嵌的文字
* imageFile: JPG图片的虚拟路径
* x: 文字输出的起始X坐标位置
* y: 文字输出的起始Y坐标位置
* fontColor: 字体颜色(例fontColor=FFFFFF)
* fontSize: 字体大小
* fontStyle: 字体风格(斜体,粗体等)
* fontName: 字体名称(如仿宋体,宋体等)
*/
public class TextIntoImage extends HttpServlet {
private static final String CONTENT_TYPE = "image/jpeg;charset=GB2312 ";
public void init() throws ServletException {
}
/** Process the HTTP Get request */
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
//---------------------------------------------------------------------------------------------
/** Process the HTTP Post request */
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
String text = " "; //要嵌的文字
String imageFile = " "; //被嵌的图片的虚拟路径
int x = 0; //坐标
int y = 0;
String fontColor = " "; //字体颜色
int fontSize = 0; //字体大小
String fontStyle = " "; //字体风格(斜体,粗体等)
String fontName = " "; //字体名称
try {
//取得参数(ParamUtil类请参看后面附的ParamUtil类代码)
text = ParamUtil.getParameter(request, "text ");
imageFile = ParamUtil.getParameter(request, "imageFile ");
x = ParamUtil.getIntParameter(request, "x ",0);
y = ParamUtil.getIntParameter(request, "y ",0);
fontColor = ParamUtil.getParameter(request, "fontColor ");
fontSize = ParamUtil.getIntParameter(request, "fontSize ",16);
fontStyle = ParamUtil.getParameter(request, "fontStyle ");
fontName = ParamUtil.getParameter(request, "fontName ");
}
catch(Exception e) {
e.printStackTrace();
}
ServletOutputStream output=response.getOutputStream();
if(imageFile.toLowerCase().endsWith( ".jpeg ")||imageFile.toLowerCase().endsWith( ".jpg ")) {
imageFile = getServletContext().getRealPath(imageFile);
InputStream imageIn = new FileInputStream(new File(imageFile));
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);
BufferedImage image = decoder.decodeAsBufferedImage();
Graphics g=image.getGraphics();
//设置颜色
g.setColor(new Color(Integer.parseInt(fontColor,16)));
//设置字体
Font mFont = new Font(fontName,Font.PLAIN,fontSize);//默认字体
if(fontStyle.equalsIgnoreCase( "italic ")) mFont=new Font(fontName,Font.ITALIC,fontSize);
if(fontStyle.equalsIgnoreCase( "bold ")) mFont=new Font(fontName,Font.BOLD,fontSize);
if(fontStyle.equalsIgnoreCase( "plain ")) mFont=new Font(fontName,Font.PLAIN,fontSize);
g.setFont(mFont);
//输出文字
g.drawString(text,x,y);
//输出数据流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
encoder.encode(image);
imageIn.close();
}
output.close();
}
}//////////
上面获取参数的代码使用了一个工具类,它是扩展了request.getParameter()功能的一个类:package net.xdevelop.util;
import javax.servlet.*;
public class ParamUtil {
/**
* 获得request中指定名称的参数值,若有中文乱码问题请增加转码部分
* @param request ServletRequest对象
* @param paramName 参数名称
* @return 如果该变量值存在则返回该值,否则返回 " "
*/
public static String getParameter( ServletRequest request, String paramName ) {
String temp = request.getParameter(paramName);
if( temp != null && !temp.equals( " ") ) {
//若有中文问题,在此添加转码代码,例:temp = new String(temp.getBytes( "8859_1 "), "GB2312 ");
return temp;
}
else {
return " ";
}
}
/**
* 获得request中的int型参数值
* @param request ServletRequest对象
* @param paramName 参数名称
* @param defaultNum 默认值,如果没有返回该值
* @return 如果该参数值存在则返回其转换为int型的值,否则返回defaultNum
*/
public static int getIntParameter( ServletRequest request, String paramName, int defaultNum ) {
String temp = request.getParameter(paramName);
if( temp != null &response.setContentType( "iamge/jpg ");
ServletOutputStream sos = response.getOutputStream();
ResultSet rs = stmt.executeQuery( "select imagefield from t ");
while(rs.next()){
InputStream is = rs.getBinaryStream( "imagefield ");
byte[] b = new byte[128];
int l;
while((l = is.read(b))> -1){
sos.write(b,0,l);
}
}
%>
补充:Jsp教程,J2ME开发