Excerpt from: http://itindex.net/detail/47160-java-jquery-%E4%B8%8A%E4%BC%A0
Operating Effect:
Package structure diagram:
background Java logic:
Package Com.bijian.study;import Java.io.file;import Java.io.ioexception;import java.util.iterator;import Java.util.list;import Java.util.uuid;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, @SuppressWarnings ("Serial") public class Upload extends HttpServlet {@SuppressWarnings ("unchecked") public void DoPost (HttpServletRequest request, HttpServletResponse resp Onse) throws Servletexception, IOException {String Savepath = This.getservletconfig (). Getservletcontext (). Getrealp Ath (""); Savepath = Savepath + "/uploads/"; File F1 = new file (Savepath); System.out.println (Savepath); if (!f1.exists ()) {F1.mkdirs (); } diskfileitemfactory FAC = new Diskfileitemfactory (); Servletfileupload upload = new Servletfileupload (FAC); Upload.setheaderencoding ("Utf-8"); List fileList = null; try {fileList = upload.parserequest (request); } catch (Fileuploadexception ex) {return; } iterator<fileitem> it = Filelist.iterator (); String name = ""; String extname = ""; while (It.hasnext ()) {Fileitem item = It.next (); if (!item.isformfield ()) {name = Item.getname (); Long size = Item.getsize (); String type = Item.getcontenttype (); SYSTEM.OUT.PRINTLN (Size + "" + type); if (name = = NULL | | Name.trim (). Equals ("")) {continue; }//EXTENSION format: if (Name.lastindexof (".") >= 0) {extname = name.substring (Name.lastindexof (".")); } file file = null; do {//Generate file name: Name = Uuid.randomuuid (). toString (); File = new file (Savepath + name + extname); } while (File.exists ()); File SaveFile = new file (Savepath + name + extname); try {item.write (saveFile); } catch (Exception e) {e.printstacktrace (); }}} response.getwriter (). Print (name + extname); }}
Xml:
<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.4" xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "> <servlet> <servlet-name>upload </servlet-name> <servlet-class>com.bijian.study.Upload</servlet-class> </servlet > <servlet-mapping> <servlet-name>upload</servlet-name> <url-pattern>/ servlet/upload</url-pattern> </servlet-mapping> <welcome-file-list> < Welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
INDEX.JSP:
<%@ page language= "java" contenttype= "text/html; Charset=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" > Java version-jquery upload plugin uploadify use instance