JSP code for writing images to the database.

Source: Internet
Author: User

I found an example for the upload task and kept it for reference.

 

Upload: The smartupload component is required.

 

Import COM. jspsmart. upload. *; <br/> Import javax. servlet. *; <br/> Import javax. servlet. HTTP. *; <br/> Import Java. io. *; <br/> Import Java. util. *; <br/>/** <br/> * upload public classes, smartupload is encapsulated <br/> * @ author askr <br/> * @ version 1.0 <br/> */<br/> public class myupload <br/> {< br/>/** <br/> * Save the uploaded file <br/> * @ Param request page request object <br/> * @ Param response page response object <br/> * @ Param config page configuration object <br/> * @ Param mainname: the name of the primary file to be uploaded. <br/> * @ Param savephypath: the folder where the file is saved. <br/> * @ Param maxsizepool: KB <br/> * @ Param maxsizeallowed the maximum size allowed when uploading a file. Unit: KB <br/> * @ Param formatlistallowed the file format is allowed, string sequence separated by commas <br/> * @ return if no upload is performed, an empty string is returned. Otherwise, is the saved file name <br/> */<br/> Public static string saveupload (httpservletrequest request, <br/> httpservletresponse response, servletconfig config, <br/> string Mai Nname, string savephypath, int maxsizepool, int maxsizeallowed, <br/> string formatlistallowed) <br/>{< br/> smartupload upl = new smartupload (); <br/> COM. jspsmart. upload. file F = NULL; <br/> try {<br/> upl. init (config); // config is an implicit built-in object <br/> upl. service (request, response); <br/> // upl. setallowedfileslist (formatlistallowed); <br/> upl. settotalmaxfilesize (maxsizepool * 1024); // byte <br/> upl. upload (); <Br/> F = upl. getfiles (). getFile (0); <br/> string name = f. getfilename (); <br/> If (F. getsize () = 0) {<br/> return ""; <br/>}< br/> If (F. getsize ()> maxsizeallowed * 1024) <br/> throw new runtimeexception ("maximum size:" + <br/> maxsizeallowed + "kb "); <br/> string ext = f. getfileext (); <br/> stringtokenizer ST = new stringtokenizer (formatlistallowed, ","); <br/> Boolean formatflag = false; <br/> while (St. Hasmoretokens () <br/>{< br/> string element = ST. nexttoken (); <br/> If (element. equalsignorecase (EXT) <br/>{< br/> formatflag = true; <br/> break; <br/>}< br/> If (formatflag = false) throw new runtimeexception ("the file format is incorrect, must be "+ <br/> formatlistallowed +"); <br/> string Path = savephypath + "//" + mainname + ". "+ <br/> F. getfileext (); <br/> F. saveas (path, Com. jspsmart. upload. file. saveas _ Physical); <br/>}< br/> catch (runtimeexception ex) {<br/> throw ex; <br/>}< br/> catch (ioexception ex) <br/> {<br/> ex. printstacktrace (); <br/>}< br/> catch (smartuploadexception ex) <br/>{< br/> ex. printstacktrace (); <br/>}< br/> catch (servletexception ex) {<br/> ex. printstacktrace (); <br/>}< br/> return mainname + ". "+ F. getfileext (); <br/>}</P> <p> Generate a thumbnail: <br/> Import COM. s Un.image.codec.jpeg. *; <br/> Import Java. AWT. *; <br/> Import Java. AWT. image. bufferedimage; <br/> Import Java. io. *; <br/> Import javax. imageIO. *; <br/> Import com.sun.image.codec.jpeg. using imageencoder; <br/> public class compressimage <br/> {<br/>/** <br/> * @ Param oldpath: the complete physical path of the file to be compressed, including the name <br/> * @ Param newdir: The storage path of the new file, name not included <br/> * @ Param newmainname main file name of the new file <br/> * @ Param maxborder new The length of the longest side in the file and the length of the longest side in the width are scaled down by another side. For example, the longest side of the graph to generate <br/> * is 64 pixels, if the length is greater than the width, the generated length is 64, and the width is the same as the original width. <Br/> */<br/> Public static void generate (string oldpath, string newdir, string newmainname, int maxborder) <br/>{< br/> file soufile = new file (oldpath); <br/> // string a = soufile. getabsolutepath (); <br/> // int a1 = 1; <br/> try {<br/> string newpath; <br/> If (newdir. equals ("") <br/> newpath = newmainname + ". jpg "; <br/> else <br/> newpath = newdir +" // "+ newmainname + ". jpg "; <br/> fileoutputstr EAM desfile = new fileoutputstream (New <br/> file (newpath); <br/> bufferedimage souimage = ImageIO. read (soufile); <br/> int oldwidth = souimage. getwidth (); <br/> int oldheight = souimage. getheight (); <br/> int newwidth, newheight; <br/> If (oldwidth> oldheight) {<br/> newwidth = maxborder; <br/> newheight = (INT) (newwidth * oldheight/<br/> oldwidth); <br/>}< br/> else {<br/> newheight = ma Xborder; <br/> newwidth = (INT) (oldwidth * newheight/<br/> oldheight ); <br/>}< br/> bufferedimage desimage = new bufferedimage (<br/> newwidth, newheight, 1); <br/> desimage. getgraphics (). drawimage (souimage, 0, 0, newwidth, newheight, <br/> null); <br/> export imageencoder encode = export codec. createjpegencoder (desfile); <br/> specify encode. encode (desimage); <br/> desfile. close (); <br/>}< br/> catch (Imageformatexception ex) {<br/> ex. printstacktrace (); <br/>}< br/> catch (ioexception ex) {<br/> ex. printstacktrace (); <br/>}< br/> catch (exception e) <br/>{< br/> E. printstacktrace (); <br/>}</P> <p> ////////////////// //////////////////////////////////////// ////// // The servlet method is as follows: </P> <p> JavaBean: <br/> mport Java. io. *; </P> <p> public class DBO {<br/> Public DBO () {<br/>} </P> <p> Public inputstream getbytes () {</P> <p> connection con = NULL; <br/> preparedstatement PST = NULL; <br/> resultset rs = NULL; <br/> inputstream stream = NULL; <br/> file F = NULL; <br/> byte context [] = new byte [100000]; <br/> try {<br/> class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver "); <br/> con = drivermanager. getconnection ("JDBC: ODBC: test"); <br/> F = new file ("C: // aa.jpg"); <br/> stream = New fileinputstream (f); <br/> // PST = con. preparestatement ("insert into mytable (col10) values (?) "); <Br/> // PST. setbinarystream (1, stream, (INT) F. length (); <br/> PST = con. preparestatement ("select col10 from mytable"); <br/> rs = pst.exe cutequery (); <br/> If (RS. next () {<br/> stream = Rs. getbinarystream (1); <br/>}< br/> return stream; <br/>}< br/> catch (exception e) {<br/> system. out. println (E); <br/> return stream; <br/>}< br/> finally {<br/> try {<br/>/* <br/> If (RS! = NULL) {<br/> Rs. Close (); <br/>}< br/> If (Pst! = NULL) {<br/> PST. Close (); <br/>}< br/> If (con! = NULL) {<br/> PST. close (); <br/>}< br/> */<br/>}< br/> catch (exception E) {</P> <p >}< br/>}</P> <p> Public static void main (string ARGs []) {</P> <p >}< br/>}</P> <p>/* <br/> write data to the database <br/> connection con = NULL; <br/> preparedstatement PST = NULL; <br/> resultset rs = NULL; <br/> inputstream stream = NULL; <br/> file F = NULL; <br/> try {<br/> class. forname ("Sun. JDBC. ODBC. jdbcodbcdr Iver "); <br/> con = drivermanager. getconnection ("JDBC: ODBC: test"); <br/> F = new file ("C: // aa.jpg "); <br/> stream = new fileinputstream (f); <br/> PST = con. preparestatement ("insert into mytable (col10) values (?) "); <Br/> PST. setbinarystream (1, stream, (INT) F. length (); <br/> pst.exe cute (); <br/>}< br/> catch (exception e) {<br/> system. out. println (E); <br/>}< br/> finally {<br/> try {<br/> If (RS! = NULL) {<br/> Rs. Close (); <br/>}< br/> If (Pst! = NULL) {<br/> PST. Close (); <br/>}< br/> If (con! = NULL) {<br/> PST. close (); <br/>}< br/> catch (exception E) {</P> <p >}< br/> */</P> <p> JSP and Servlet <br/> JSP: <br/> <% @ page contenttype = "text/html; charset = GBK "%> <br/> <HTML> <br/> <pead> <br/> <title> <br/> jsp1 <br/> </title> <br/> </pead> <br/> <body bgcolor = "# ffffff"> <br/> dfdfd <br/> <br/> dfdef <br/> </body> <br/> </ptml> </P> <p> servlet: </P> <p> package index; </P> <p> Import javax. servlet. *; <br/> Import javax. servlet. HTTP. *; <br/> Import Java. io. *; <br/> // import Java. util. *; <br/> Import index. DBO; </P> <p> public class servlet1 <br/> extends httpservlet {<br/> Private Static final string content_type = "text/html; charset = GBK "; </P> <p> // initialize global variables <Br/> Public void Init () throws servletexception {<br/>}</P> <p> // process the http get request <br/> Public void doget (httpservletrequest request, httpservletresponse response) throws <br/> servletexception, ioexception {<br/> // response. setcontenttype ("image/GIF;"); <br/> DBO d = new DBO (); <br/> JAVA. io. inputstream PIC = NULL; <br/> JAVA. io. outputstream PW = NULL; <br/> try {<br/> // request. Getparameter ("ID"); <br/> response. setcontenttype ("image/gif?name=doskt.jpg"); <br/> PIC = D. getbytes (); <br/> PW = response. getoutputstream (); <br/> byte [] B = new byte [4096]; <br/> int bt = pic. read (B); <br/> while (BT! =-1) {<br/> PW. write (B, 0, BT); <br/> bt = pic. read (B); <br/>}< br/> catch (exception e) {<br/> system. out. println ("doget:" + E); <br/>}< br/> finally {<br/> If (pic! = NULL) {<br/> pic. close (); <br/>}</P> <p>/* out. println ("<HTML>"); <br/> out. println ("<pead> <title> servlet1 </title> </pead>"); <br/> out. println ("<body bgcolor =/" # ffffff/">"); <br/> out. println ("<p> the servlet has received ed a get. this is the reply. </P> "); <br/> out. println ("</body> </ptml> "); */<br/>}</P> <p> // clean up resources <br/> Public void destroy () {<br/>}< br/> Source: DIY tribe (http://www.diybl.com/course/4_webprogram/jsp/jsp_js/2008430/112053_4.html) </P> <p>

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.