[Uploadify3.1 use II] batch file and Image Upload

Source: Internet
Author: User

1. The use of uploadify3.1 is different from that of 2.x. Version 3.1 has embedded swfobject. js into jquery. uploadify-3.1.min.js,

2. Introduce resource files

<link rel="stylesheet" href="uploadify/uploadify.css" type="text/css"></link><script type="text/javascript" src="uploadify/jquery-1.7.2.min.js"></script><script type="text/javascript"src="uploadify/jquery.uploadify-3.1.min.js"></script>

3. Write Data in the header

<SCRIPT type = "text/JavaScript"> $ (function () {$ ("# multiple_file_upload "). uploadify ({'height': 27, 'width': 80, 'buttontext': 'browsed ', 'swf ': '<% = PATH %>/uploadify/uploadify.swf', 'upload': '<% = PATH %>/servlet/uploadifyserlet', 'auto': false, 'filetypeexts ': '*. JPG ;*. PNG ;*. GIF ;*. BMP ', 'formdata': {'username': '', 'qq':''}, 'onuploadstart': function (file) {// $ ("# file_upload "). uploadify ("Settings", "formdata", {'username': name, 'qq': QQ}); // $ ("# file_upload "). uploadify ("Settings", "QQ",) ;}}) ;}); </SCRIPT>

4. Write Data to the body

<Body> <input type = "file" name = "uploadify" id = "multiple_file_upload"/> <HR> <a href = "javascript: $ ('# multiple_file_upload '). uploadify ('upload', '*') "> Start upload </a> <a href =" javascript: $ ('# multiple_file_upload '). uploadify ('cancel', '*') "> cancel all uploads </a> </body>

5. Background code

Package COM. yangpan. uploadify; import Java. io. file; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. io. outputstream; import Java. io. printwriter; import Java. text. simpledateformat; import Java. util. arrays; import Java. util. date; import Java. util. iterator; import Java. util. list; import Java. util. random; import javax. servlet. servletexception; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import Org. apache. commons. fileupload. fileitem; import Org. apache. commons. fileupload. fileuploadexception; import Org. apache. commons. fileupload. disk. diskfileitemfactory; import Org. apache. commons. fileupload. servlet. servletfileupload; public class uploadifyserlet extends httpservlet {/*****/priv Ate static final long serialversionuid = 1l; // save the file upload path protected string configpath = "attached/"; protected string dirtemp = "attached/temp /"; protected string dirname = "file"; Public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {This. dopost (request, response);} public void dopost (httpservletrequest request, httpservletresponse res Ponse) throws servletexception, ioexception {request. setcharacterencoding ("UTF-8"); response. setcontenttype ("text/html; charset = UTF-8"); printwriter out = response. getwriter (); // path of the file storage directory string savepath = This. getservletcontext (). getrealpath ("/") + configpath; // temporary file directory string temppath = This. getservletcontext (). getrealpath ("/") + dirtemp; simpledateformat SDF = new simpledateformat ("yyyymm "); String ymd = SDF. format (new date (); savepath + = "/" + ymd + "/"; // create a folder file dirfile = new file (savepath); If (! Dirfile. exists () {dirfile. mkdirs ();} temppath + = "/" + ymd + "/"; // create a Temporary Folder file dirtempfile = new file (temppath); If (! Dirtempfile. exists () {dirtempfile. mkdirs ();} diskfileitemfactory factory = new diskfileitemfactory (); factory. setsizethreshold (20*1024*1024); // when the memory usage exceeds 5 MB, temporary files are generated and stored in the temporary directory. Factory. setrepository (new file (temppath); // you can specify the directory for storing temporary files. Servletfileupload upload = new servletfileupload (factory); upload. setheaderencoding ("UTF-8"); try {list items = upload. parserequest (request); iterator itr = items. iterator (); While (itr. hasnext () {fileitem item = (fileitem) itr. next (); string filename = item. getname (); long filesize = item. getsize (); If (! Item. isformfield () {string fileext = filename. substring (filename. lastindexof (". ") + 1 ). tolowercase (); simpledateformat df = new simpledateformat ("yyyymmddhhmmss"); string newfilename = DF. format (new date () + "_" + new random (). nextint (1000) + ". "+ fileext; try {file uploadedfile = new file (savepath, newfilename); outputstream OS = new fileoutputstream (uploadedfile); inputstream is = item. getin Putstream (); byte Buf [] = new byte [1024]; // you can modify 1024 to increase the read speed. Int length = 0; while (length = is. read (BUF)> 0) {OS. write (BUF, 0, length);} // close the stream OS. flush (); OS. close (); is. close (); system. out. println ("Upload successful! Path: "+ savepath +"/"+ newfilename); out. print ("1");} catch (exception e) {e. printstacktrace () ;}} else {string filedname = item. getfieldname (); // If (filedname. equals ("username") // {// name = item. getstring (); // else // {// QQ = item. getstring (); //} system. out. println ("fieldname:" + filedname); system. out. println ("string:" + item. getstring (); // system. out. println ("string ():" + item. getstring (item. getname (); system. out. println ("==================") ;}} catch (fileuploadexception e) {// todo auto-generated Catch Block E. printstacktrace ();} Out. flush (); out. close ();}}

6. Effect

After finishing the job, I prefer step by step. Oh, it's too procedural and I don't have the thinking. Haha

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.