Jsp saves the image to the Oracle database and reads it and displays it directly on the Jsp page.

Source: Internet
Author: User

Jsp saves the image to the Oracle database and reads it and displays it directly on the Jsp page.

The jsp tutorial saves the image to the Oracle database tutorial and reads it and displays it directly to the Jsp page.
The combined image data is displayed on the called webpage. Note: This servlet only processes JPG image files and does not support GIF files ):
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;

/**
* Embed the text in the specified position of the specified image with the specified font, color, and size. Call the parameters:
* Text: the text to be embedded
* ImageFile: the virtual path of the jpg image.
* X: the starting X coordinate position of the text output.
* Y: The starting Y coordinate position of the text output.
* FontColor: fontColor (for example, fontColor = FFFFFF)
* FontSize: font size
* FontStyle: font style (italic, bold, etc)
* FontName: font name (for example, or)
*/
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 );

}
// Configure //---------------------------------------------------------------------------------------------
/** Process the HTTP Post request */
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {

Response. setContentType (CONTENT_TYPE );

String text = ""; // The text to be embedded
String imageFile = ""; // virtual path of the embedded image
Int x = 0; // coordinates
Int y = 0;
String fontColor = ""; // font color
Int fontSize = 0; // font size
String fontStyle = ""; // font style (italic, bold, etc)
String fontName = ""; // font name

Try {
// Obtain the parameter. (For the ParamUtil class, see the ParamUtil class code in the Appendix)
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 ));
Required imagedecoder decoder = required codec. createJPEGDecoder (imageIn );
BufferedImage image = decoder. decodeAsBufferedImage ();
Graphics g = image. getGraphics ();

// Set the color
G. setColor (new Color (Integer. parseInt (fontColor, 16 )));

// Set the font
Font mFont = new Font (fontName, Font. PLAIN, fontSize); // default Font
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 );

// Output text
G. drawString (text, x, y );

// Output data stream
Required imageencoder encoder = required codec. createJPEGEncoder (output );
Encoder. encode (image );

ImageIn. close ();
}
Output. close ();
}
}//////////

 


The code for obtaining parameters above uses a tool class that extends the request. getParameter () function: package net. xdevelop. util;
Import javax. servlet .*;

Public class ParamUtil {
/**
* Get the parameter value of the specified name in the request. If Chinese characters are garbled, add the transcoding part.
* @ Param request ServletRequest object
* @ Param paramName: Parameter Name
* @ Return if the variable value exists, this value is returned; otherwise, "" is returned ""
*/
Public static String getParameter (ServletRequest request, String paramName ){
String temp = request. getParameter (paramName );
If (temp! = Null &&! Temp. equals ("")){
// If there is a problem with Chinese characters, add the transcoding code here, for example, temp = new String (temp. getBytes ("8859_1"), "GB2312 ");
Return temp;
}
Else {
Return "";
}
}

/**
* Obtain the int type parameter value in the request.
* @ Param request ServletRequest object
* @ Param paramName: Parameter Name
* @ Param defaultNum default value. If no value is returned
* @ Return if this parameter value exists, the system returns the value converted to the int type. Otherwise, the system returns the 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.exe cuteQuery ("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 );
}
}
%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.