JSP saves the image to the Oracle database and reads it directly to the JSP page

Source: Internet
Author: User
Tags http post

The JSP tutorial saves the image to the Oracle database tutorial and reads it directly to the JSP page
Combined image data, and on the calling page display a combination of graphic images (note: The servlet only implements JPG image file processing, does not support 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;

/**
  *   Inserts text in the specified font, color, and size, in the specified position of the specified picture, and invokes the parameter:
  *   text:   &    nbsp The text to be embedded
  *   imagefile:   jpg image virtual path
  *   x:   text output start x sit Superscript position
  *   y:   text output starting y-coordinate
  *   fontcolor:   font Color (example fon TCOLOR=FFFFFF)
  *   fontsize:   font size
  *   fontstyle:   font Wind Lattice (italic, bold, etc.)
  *   fontname:   font name (such as Song, Arial, etc.)
 /
public   Class&nbs p;  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 = ""; The text you want to embed
String imagefile = ""; Virtual path of the embedded picture
int x = 0; Coordinate
int y = 0;
String fontcolor = ""; Font Color
int fontsize = 0; Font size
String fontstyle = ""; Font style (italic, bold, etc.)
String fontname = ""; Font Name

try {
Get the parameters (see the Paramutil Class code appended to the Paramutil class)
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 ();

Set color
G.setcolor (New Color (Integer.parseint (fontcolor,16));

Set 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 flow
                         jpegimageencoder   encoder   =   jpegcodec.createjpegencoder (output);
                         Encoder.encode (image);

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


The code that gets the argument above uses a tool class, which is a class that extends the Request.getparameter () function: Package net.xdevelop.util;
Import javax.servlet.*;

public class Paramutil {
/**
* To obtain the parameter value of the specified name in request, please increase the transcoding part if there is a Chinese garbled problem
* @param request ServletRequest Object
* @param paramname parameter name
* @return Returns the value if the value of the variable exists, otherwise returns "
*/
public static string GetParameter (ServletRequest request, String paramname) {
String temp = Request.getparameter (paramname);
if (temp!= null &&!temp.equals ("")) {
If there is a Chinese problem, add transcoding code here, for example: temp = new String (temp.getbytes ("8859_1"), "GB2312");
return temp;
}
else {
Return "";
}
}

/**
* Get the int parameter value in Request
* @param request ServletRequest Object
* @param paramname parameter name
* @param defaultnum Default value if it is not returned
* @return returns the value converted to int if the parameter value exists, otherwise returns 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);
}
}
%>

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.