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