jquery uses the Uploadify plugin for uploading multiple files (Java edition)

Source: Internet
Author: User

Source Address: Https://git.oschina.net/zhengweishan/UploadDemo_Java

1. jquery uploadify Download:http://www.uploadify.com/

2, installation, because the download down the example is the PHP version, so I only left a few main files.

3, the configuration item description, please look at the document yourself. Document Address: http://www.uploadify.com/documentation/

4. Use

Front page:

<%@ page language= "java"  contenttype= "Text/html; charset=utf-8"      pageencoding= "UTF-8"%><! doctype html public  "-//w3c//dtd html 4.01 transitional//en"   "HTTP// Www.w3.org/TR/html4/loose.dtd ">

Background:

Here need to use the Commons-fileupload components, self-download (provided in the source code has OH ~).

import java.io.bufferedinputstream;import java.io.bufferedoutputstream;import java.io.file; Import java.io.fileoutputstream;import java.io.ioexception;import java.io.printwriter;import  java.util.Date;import java.util.Iterator;import java.util.List;import  javax.servlet.servletexception;import javax.servlet.annotation.webservlet;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;import org.apache.commons.fileupload.util.streams ;/** * servlet implementation class uploadservlet */@WebServlet ("/UploadServlet" ) Public class uploadservlet extends httpservlet {private static final long serialversionuid = 1l;            /**     *  @see  httpservlet# HttpServlet ()      */    public uploadservlet ()  {         super ();         // todo  Auto-generated constructor stub    }/** *  @see  httpservlet# Doget (Httpservletrequest request, httpservletresponse response)  */protected void  doget (Httpservletrequest request, httpservletresponse response)  throws  Servletexception, ioexception {// todo auto-generated method stubdopost (Request, Response);} /** *  @see  httpservlet#dopost (httpservletrequest request, httpservletresponse  Response)  */protected void dopost (Httpservletrequest request, httpservletresponse response)  throws ServletException, IOException {// TODO Auto-generated method  stub//File storage Directory           File tempDirPath =new  File (Request.getsession (). Getservletcontext (). Getrealpath ("/") + "\\upload\\");           if (!tempdirpath.exists ()) {               tempdirpath.mkdirs ();           }                     //creating a disk File Factory           diskfileitemfactory  fac = new diskfileitemfactory ();               //create servlet File Upload component           servletfileupload upload  = new servletfileupload (FAC);               //file List           List<FileItem>  fileList = null;               //parse request to get the file from the front desk           try {                   filelist  = upload.parserequest (Request);               } catch  (Fileuploadexception ex)  {                   ex.printstacktrace ();                   return;               }            //file name after saving           String imageName =  null;          //convenient list of documents available from the front desk            iterator<fileitem> it = filelist.iterator ();              while (It.hasnext ()) {                   fileitem item =   it.next ();              // If it is not a normal form field, treat the               if as a file field (! Item.isformfield ()) {&NBSP;&NBSP;&NBsp;           imagename = new date (). GetTime () +math.random () *10000+item.getname ();                   BufferedInputStream in = new  Bufferedinputstream (Item.getinputstream ());                      bufferedoutputstream out = new  bufferedoutputstream (                                  new fileoutputstream (New file (tempdirpath+ "\" +imagename));                   streams.copy (in, out,  true);                                  }          }           //           printwriter out = null;          try  {              out =  Encodehead (request, response);           } catch   (ioexception e)  {               e.printstacktrace ();          }           //This place can not be less, otherwise the reception can not get the results of uploading            out.write ("1");         out.close ();   }/**       * ajax Auxiliary methods   get  PrintWriter      *  @return       *  @throws  ioexception       *   @throws  ioexception       * request.setcharacterencoding (" Utf-8 ");          response.setcontenttype (" Text/html; charset =utf-8 ");       */  private printwriter encodehead ( Httpservletrequest request,httpservletresponse response)  throws IOException{           request.setcharacterencoding ("Utf-8");           response.setcontenttype ("Text/html; charset=utf-8");            return response.getwriter ();       }  } 

5, finally a bit like Baidu upload file page effect did not Baidu do good-looking ha ~ do not spray


jquery uses the Uploadify plugin for uploading multiple files (Java edition)

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.