Commons fileupload Upload Component (complete Image Upload)

Source: Internet
Author: User

The Code comes from the network. Make some changes...

Imported jar package

Commons-io-1.2.jar

Commons-fileupload-1.1.1.jar

Servlet type

Import Java. io. file; import Java. io. ioexception; import Java. io. printwriter; import Java. util. iterator; import Java. util. list; import Java. util. regEx. matcher; import Java. util. regEx. pattern; 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; impo RT Org. apache. commons. fileupload. fileuploadexception; import Org. apache. commons. fileupload. disk. diskfileitemfactory; import Org. apache. commons. fileupload. servlet. servletfileupload;/***** @ author administrator **/public class fileuploadservlet extends httpservlet {private string uploadpath = "d :\\ addnetfile \\"; // Private file temppath = new file ("d :\addnetfile \ TMP \") used to store uploaded files; // directory P used to store temporary files Ublic void dopost (httpservletrequest req, httpservletresponse res) throws servletexception, ioexception {res. setcontenttype ("text/html; charset = gb2312"); printwriter out = res. getwriter (); system. out. println (req. getcontentlength (); system. out. println (req. getcontenttype (); diskfileitemfactory factory = new diskfileitemfactory (); factory. setsizethreshold (4096); factory. setrepository (new file ("D: \ file \ Ddnetfile \ Temp "); servletfileupload upload = new servletfileupload (factory); upload. setsizemax (1000000); try {list fileitems = upload. parserequest (req); iterator iter = fileitems. iterator (); // regular match, filtering path to get file name string Regexp = ". + \\\\(. +) {1} quot; // The filtered file type string [] errortype = {". EXE ",". com ",". cgi ",". ASP "}; pattern P = pattern. compile (Regexp); While (ITER. hasnext () {fileitem item = (fileitem) ITER. n EXT (); // ignore all other form information not in the file field if (! Item. isformfield () {string name = item. getname (); long size = item. getsize (); If (name = NULL | Name. equals ("") & size = 0) continue; matcher M = P. matcher (name); boolean result = m. find (); If (result) {for (INT temp = 0; temp <errortype. length; temp ++) {If (M. group (1 ). endswith (errortype [temp]) {Throw new ioexception (name + ": Wrong type") ;}try {// Save the uploaded file to the specified directory item. write (new file ("D: \" + M. group (1); out. print (name + "" + size + "<br>");} catch (exception e) {out. println (e) ;}} else {Throw new ioexception ("fail to upload") ;}}} catch (ioexception e) {out. println (E);} catch (fileuploadexception e) {out. println (e) ;}} public void Init () throws servletexception {}}

Web Front-end HTML page

<HTML> 

You can configure web. xml by yourself. You can also select a wizard, which is very simple...

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.