The image is encoded in JPEG format on the JSP page and stored in the specified folder. Compared with component upload, this JSP upload method seems clumsy and cannot perform operations on file attributes. However, it is indeed easy to implement the JSP upload function.
Uploadimage1.jsp creates a form and submits parameters used to rename images and image files.
- <%... @ PageContentType="Text/html; charset = gbk"%>
- <%... @ PageLanguage="Java"%>
- <HTML>
- <HEAD>
- <TITLE>DATABASE TITLE>
- <Meta Http-equiv="Content-Type" Content="Text/html; charset = gb2312">
- HEAD>
- <FORM METHOD=POST ACTION="Uploadimage2.jsp">
- Characters:<INPUT TYPE="Text" NAME="Name"><BR>
- Image:<INPUT TYPE="File" NAME="Image"><BR>
- <INPUT TYPE="Submit" Value="Submit">
- FORM>
- BODY>
- HTML>
Uploadimage1.jsp is used to obtain the name of the image to be stored by the user submission parameter. The uploaded image is obtained in the form of characters and further converted into an input stream. This completes the JSP image upload function.
- <%... @ PageContentType="Text/html; charset = gb2312"%>
- <%... @ PageImport="Java. util .*"%>
- <%... @ PageImport="Java. text .*"%>
- <%... @ PageImport="Java. io .*"%>
- <%... @ PageImport="Com.sun.image.codec.jpeg .*"%>
- <%... @ PageImport="Java. awt. image .*"%>
- <%... @ PageImport="Java. awt .*"%>
- <%...
- StringName=Request. GetParameter ("name ");
- Name=NewString (name. getBytes ("ISO-8859-1 "));
- StringIma=Request. GetParameter ("image ");
-
- Try {
- StringPath=Request. GetRealPath ("/");
- FileOutputStreamOt=NewFileOutputStream (path + name + ". jpg ");
- // ServletOutputStreamOt=Response. GetOutputStream (); // You can also directly display the output.
- FileInputStreamIn=NewFileInputStream (ima );
- IncluimagedecoderJpgCodec=JPEGCodec. CreateJPEGDecoder (in );
- BufferedImageImage=JpgCodec. DecodeAsBufferedImage ();
- JPEGImageEncoderEncoder=JPEGCodec. CreateJPEGEncoder (ot );
- Encoder. encode (image );
- In. close ();
- Ot. close ();
- Out. print ("image uploaded by JSP!<BR>");
- // Load the uploaded Image
- Out. print ("<IMG Width=200 Height=200 Src='"+ Name +". jpg' />");
- }
- Catch (Exception e)
- {
- System. out. print (e. toString ());
- }
- %>
We will introduce the implementation of JSP Image Upload without componentization.
- Introduction to using jspsmart for file upload and download
- Use JSP pages to generate PDF reports
- Step for customizing JSP labels
- Detailed test of JSP containers
- Describes the following features of jsp http Server