Discussion on how to implement the componentization of JSP image uploading

Source: Internet
Author: User

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.

 
 
  1. <%... @ PageContentType="Text/html; charset = gbk"%>
  2. <%... @ PageLanguage="Java"%>
  3. <HTML>
  4. <HEAD>
  5. <TITLE>DATABASE TITLE>
  6. <Meta Http-equiv="Content-Type" Content="Text/html; charset = gb2312">
  7. HEAD>
  8. <FORM METHOD=POST ACTION="Uploadimage2.jsp">
  9. Characters:<INPUT TYPE="Text" NAME="Name"><BR>
  10. Image:<INPUT TYPE="File" NAME="Image"><BR>
  11. <INPUT TYPE="Submit" Value="Submit">
  12. FORM>
  13. BODY>
  14. 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.

 
 
  1. <%... @ PageContentType="Text/html; charset = gb2312"%>
  2. <%... @ PageImport="Java. util .*"%>
  3. <%... @ PageImport="Java. text .*"%>
  4. <%... @ PageImport="Java. io .*"%>
  5. <%... @ PageImport="Com.sun.image.codec.jpeg .*"%>
  6. <%... @ PageImport="Java. awt. image .*"%>
  7. <%... @ PageImport="Java. awt .*"%>
  8. <%...
  9. StringName=Request. GetParameter ("name ");
  10. Name=NewString (name. getBytes ("ISO-8859-1 "));
  11. StringIma=Request. GetParameter ("image ");
  12.  
  13. Try {
  14. StringPath=Request. GetRealPath ("/");
  15. FileOutputStreamOt=NewFileOutputStream (path + name + ". jpg ");
  16. // ServletOutputStreamOt=Response. GetOutputStream (); // You can also directly display the output.
  17. FileInputStreamIn=NewFileInputStream (ima );
  18. IncluimagedecoderJpgCodec=JPEGCodec. CreateJPEGDecoder (in );
  19. BufferedImageImage=JpgCodec. DecodeAsBufferedImage ();
  20. JPEGImageEncoderEncoder=JPEGCodec. CreateJPEGEncoder (ot );
  21. Encoder. encode (image );
  22. In. close ();
  23. Ot. close ();
  24. Out. print ("image uploaded by JSP!<BR>");
  25. // Load the uploaded Image
  26. Out. print ("<IMG Width=200 Height=200 Src='"+ Name +". jpg' />");
  27. }
  28. Catch (Exception e)
  29. {
  30. System. out. print (e. toString ());
  31. }
  32. %>

We will introduce the implementation of JSP Image Upload without componentization.

  1. Introduction to using jspsmart for file upload and download
  2. Use JSP pages to generate PDF reports
  3. Step for customizing JSP labels
  4. Detailed test of JSP containers
  5. Describes the following features of jsp http Server

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.