Discussing how to implement the absence of componentization for 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 implements the construction of a form, and the submission is used

 <% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" ISO-8859-1 "%> <br/> <% <br/> string Path = request. getcontextpath (); <br/> string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; <br/>%> </P> <p> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <br/> <HTML> <br/> <pead> <br/> <base href = "<% = basepath %>"> </P> <p> <title> my JSP 'index. JSP 'starting page </title> <br/> <meta http-equiv = "Pragma" content = "no-Cache"> <br/> <meta http-equiv =" cache-control "content =" no-Cache "> <br/> <meta http-equiv =" expires "content =" 0 "> <br/> <meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3 "> <Br/> <meta http-equiv = "Description" content = "this is my page"> <br/> <! -- <Br/> <LINK rel = "stylesheet" type = "text/CSS" href = "styles.css" mce_href = "styles.css"> <br/> --> <br/> </pead> </P> <p> <body> <br/> This Is My JSP page. <br> <br/> <form action = "myjsp. JSP "method =" Post "> <br/>: <input type =" text "name =" name "> <br/>: <input type = "file" name = "image"> <br/> <input type = "Submit" value = "T"> <br/> </Form> <br/> </body> <br/> </ptml> <br/>

 

Image rename parameters and image files.

<% @ Page contenttype = "text/html; charset = gb2312" %> <br/> <% @ page import = "Java. util. * "%> <br/> <% @ page import =" Java. text. * "%> <br/> <% @ page import =" Java. io. * "%> <br/> <% @ page import =" com.sun.image.codec.jpeg. * "%> <br/> <% @ page import =" Java. AWT. image. * "%> <br/> <% @ page import =" Java. AWT. * "%> </P> <p> <% <br/> string name = request. getparameter ("name"); <br/> name = new string (name. getbytes ("ISO-8859-1"); <br/> String IMA = request. getparameter ("image"); <br/> out. print (name + "/N" + IMA); <br/> try {<br/> string Path = request. getrealpath ("/"); <br/> fileoutputstream ot = new fileoutputstream (path + name + ". jpg "); <br/> // servletoutputstream ot = response. getoutputstream (); // You can also directly output the display <br/> fileinputstream in = new fileinputstream (IMA); <br/> export imagedecoder jpgcodec = export codec. createjpegdecoder (in); <br/> bufferedimage Image = jpgcodec. decodeasbufferedimage (); <br/> required imageencoder encoder = required codec. createjpegencoder (OT); <br/> encoder. encode (image); <br/> in. close (); <br/> OT. close (); <br/> out. print ("image uploaded by JSP! <Br> "); <br/> // load the uploaded image <br/> out. print ("}< br/> catch (exception e) <br/>{< br/> system. out. print (E. tostring (); <br/>}< br/>%> <br/>

 

 

 

Source Address: http://www.soft6.com/tech/16/169469.html

 

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.