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);
}
}
%>