Although many examples on the network are of Version 3.1, the call methods are of the old version and cannot run. After viewing the doc, the following example can be run normally in Tomcat.
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"> <HTML>
Servlet: uploadify. Java
Package COM. RH. core. upload; import Java. io. bufferedinputstream; import Java. io. bufferedoutputstream; import Java. io. file; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. text. simpledateformat; import Java. util. date; import Java. util. iterator; import Java. util. list; import javax. servlet. servletexception; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; Import javax. servlet. HTTP. httpservletresponse; import Org. apache. commons. fileupload. disk. diskfileitem; import Org. apache. commons. fileupload. disk. diskfileitemfactory; import Org. apache. commons. fileupload. servlet. servletfileupload; import Org. apache. commons. fileupload. util. streams; public class uploadify extends httpservlet {Private Static final long serialversionuid = 1l;/*** Multifile upload simultaneously */Public void Doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {// set the received encoding format request. setcharacterencoding ("UTF-8"); Date = new date (); // get the current time simpledateformat sdffilename = new simpledateformat ("yyyymmddhhmmss "); simpledateformat sdffolder = new simpledateformat ("yymm"); string newfilename = sdffilename. format (date); // file name string filerealpath = ""; // file storage Put the real address string filerealresistpath = ""; // The actual relative path of the file. // The name page encoding must be consistent with the request .. otherwise, garbled string name = request. getparameter ("name"); string firstfilename = ""; // obtain the physical path of the upload folder in the container string savepath = This. getservletconfig (). getservletcontext (). getrealpath ("/") + "uploads \" + newfilename + "\"; system. out. println ("path" + savepath + "; Name:" + name); file = new file (savepath); If (! File. isdirectory () {file. mkdirs ();} Try {diskfileitemfactory FAC = new diskfileitemfactory (); servletfileupload upload = new servletfileupload (FAC); upload. setheaderencoding ("UTF-8"); // get multiple Upload File List filelist = upload. parserequest (request); // traverses the Upload File and writes it to the disk iterator it = filelist. iterator (); While (it. hasnext () {object obit = it. next (); If (obit instanceof diskfileitem) {system. out. PR Intln ("xxxxxxxxxxxxx"); diskfileitem item = (diskfileitem) obit; // If item is a file upload form field // obtain the file name and path string filename = item. getname (); If (filename! = NULL) {firstfilename = item. getname (). substring (item. getname (). lastindexof ("\") + 1); string formatname = firstfilename. substring (firstfilename. lastindexof (". "); // get the file suffix filerealpath = savepath + newfilename + formatname; // the actual file storage address bufferedinputstream in = new bufferedinputstream (item. getinputstream (); // obtain the file input stream bufferedoutputstream outstream = new bufferedoutputstream (New fileoutputstream (new file (filerealpath); // obtain the file output stream streams. copy (in, outstream, true); // start to write the file to the specified upload folder. // if the file is successfully uploaded, the database is inserted if (new file (filerealpath ). exists () {// virtual path value: filerealresistpath = sdffolder. format (date) + "/" + filerealpath. substring (filerealpath. lastindexof ("\") + 1); // save it to the database system. out. println ("Save to database:"); system. out. println ("name:" + name); system. out. println ("virtual path:" + filerealresistpath) ;}}} catch (Org. apache. commons. fileupload. fileuploadexception ex) {ex. printstacktrace (); system. out. println ("No File Uploaded"); return;} response. getwriter (). write ("1");} public void dopost (httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {doget (req, resp );}}
Web. xml
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" 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>Uploadify</servlet-name> <servlet-class>com.rh.core.upload.Uploadify</servlet-class> </servlet> <servlet-mapping> <servlet-name>Uploadify</servlet-name> <url-pattern>/scripts/uploadify</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
Project: (you may need to modify the image location in the CSS file)