1. Create a simple Web project first upload (1-1)
2, plug-in preparation
(1), to uploadify the official website to download a uploadify plug-in, and then unzip the new JS folder to put in (this does not insist, as long as the path is right)
(2), prepare the required back-end processing jar package for uploading files: Commons-fileupload-1.2.1.jar
3, a new JSP is index.jsp +servlet that is Uploadservlet.java
Figure 1-1
4, spend a few minutes on these simple configuration completed can see INDEX.JSP and Uoloadservlet.java
(1) index.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >(2). Uoloadservlet.java
Packagecoms;ImportJava.io.File;Importjava.io.IOException;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.UUID;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.apache.commons.fileupload.FileItem;Importorg.apache.commons.fileupload.FileUploadException;Importorg.apache.commons.fileupload.disk.DiskFileItemFactory;ImportOrg.apache.commons.fileupload.servlet.ServletFileUpload; Public classUploadservletextendsHttpServlet {@Overrideprotected voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {String Savepath= This. Getservletconfig (). Getservletcontext (). Getrealpath (""); //the path to the saved fileSavepath = Savepath + "/upload/"; File F1=NewFile (Savepath); System.out.println (Savepath); //If the file does not exist, create a new if(!f1.exists ()) {F1.mkdirs (); } //This is the file upload required class, specific to Baidu to see, now just use the goodDiskfileitemfactory FAC =Newdiskfileitemfactory (); Servletfileupload Upload=NewServletfileupload (FAC); Upload.setheaderencoding ("Utf-8"); List fileList=NULL; Try{fileList=upload.parserequest (Request); } Catch(Fileuploadexception ex) {return; } //iterators, searching for files sent over the front endIterator<fileitem> it =Filelist.iterator (); String name= ""; String Extname= ""; while(It.hasnext ()) {Fileitem Item=It.next (); //determines whether the form item is a normal type if(!Item.isformfield ()) {Name=Item.getname (); LongSize =item.getsize (); String type=Item.getcontenttype (); System.out.println (Size+ " " +type); if(Name = =NULL|| Name.trim (). Equals ("")) { Continue; } //extension Format: extname is the suffix of the file, for example,. txt if(Name.lastindexof (".") >= 0) {Extname= Name.substring (Name.lastindexof ("."))); } File File=NULL; Do { //generate file Name:Name =Uuid.randomuuid (). toString (); File=NewFile (Savepath + name +extname); } while(File.exists ()); File SaveFile=NewFile (Savepath + name +extname); Try{item.write (saveFile); } Catch(Exception e) {e.printstacktrace (); }}} response.getwriter (). Print (Name+extname); }}For JSP and Java does not have a good explanation, if do not understand can private chat me, actually spent a lot of hours to use this uploadify, mainly because think upload as long as Uploadify plugin can, originally also need: Commons-fileupload-1.2.1.jar, I am a person who likes to share, I hope everyone has the ability to write blog, let more people study hard
Uploadify foreground upload file, Java background processing example