Using Common-fileupload for file uploads

Source: Internet
Author: User

Upload the components via Common-fileupload file, you can upload documents, pictures and other materials. Depending on the requirements of the program, it can be used in a variety of ways.

Here we introduce a simple example to implement the file upload function.

Get ready:

Introducing the associated build JAR package


First, we go to the upload page index.html.



<body><form action= "./servlet/item/fileuploadservlet" method= "post" enctype= "Multipart/form-data" Name= " Form1 "><input type=" file "name=" file "><input type=" Submit "name=" submit "value=" Upload "></form ><form action= "./servlet/helloword" method= "POST" ><input type= "Submit"/></form><form name= "Uploadform" method= "POST" action= "./servlet/item/fileuploadservlet" enctype= "Multipart/form-data" ><table Border= "1" width= "cellpadding=" 4 "cellspacing=" 2 "bordercolor=" #9BD7FF "><tr><td width=" 100% " colspan= "2" > File <input name= "x" size= "+" type= "file" ></td></tr></table><table> <TR><TD align= "center" ><input name= "upload" type= "submit" value= "Start uploading"/></td></tr> </table></form></body>

Select the file to upload, click Start Upload, jump to the servlet page, the fileuploadservlet mentioned below.

/** * Implement file Upload servlet * @author Administrator * */public class Fileuploadservlet extends HttpServlet {//upload path private file up loadpath;//when the file is too large, you need to set a temporary path private File temppath;public void DoPost (httpservletrequest req, httpservletresponse Res) Throws Servletexception, IOException {diskfileitemfactory factory = new Diskfileitemfactory ();// Memory storage Maximum value factory.setsizethreshold (4096); Factory.setrepository (TempPath); Servletfileupload upload = new Servletfileupload (factory);//Set File Upload size Upload.setsizemax (1000000 *); try {List Fileitems = Upload.parserequest (req); String ItemNo = ""; for (Iterator iter = Fileitems.iterator (); Iter.hasnext ();) {Fileitem item = (Fileitem) iter.next ();//is the normal form input domain if (Item.isformfield ()) {if ("ItemNo"). Equals (Item.getfieldname ())) {ItemNo = Item.getstring ();}} Is the input= "type" input field if (!item.isformfield ()) {String fileName = Item.getname (); Long size = Item.getsize (); if (fileName = = NULL | | Filename.equals ("")) && size = = 0) {continue;} Intercept strings such as: C:\WINDOWS\Debug\PASSWD.LOGfilename = filename.substring (filename.lastindexof ("\ \") + 1, filename.length ());//item.write (new File (Uploadpath + ItemNo + ". gif")); Item.write (New File (Uploadpath, ItemNo + ". gif"));}} redirect page Res.sendredirect (Req.getcontextpath () + "/servlet/item/searchitemservlet");} catch (Exception e) {e.printstacktrace ();}} /** * Initialize method, set directory */public void Init () throws servletexception {Uploadpath = new File (Getservletcontext (). Getrealpath ("UPL Oad ")//system.out.println (" uploadpath===== "+ uploadpath);//If the directory does not exist if (!uploadpath.exists ()) {// Create directory Uploadpath.mkdir ();} Temp directory TempPath = new File (Getservletcontext (). Getrealpath ("temp")), if (!temppath.exists ()) {Temppath.mkdir ();}} }
Summary:

FileUpload the process of uploading a file can be divided into three parts: first, the client establishes a connection with the server to generate the request data stream for the file to be uploaded. Second, the server receives the request stream and caches the stream to memory or disk. When the file is too large, the file will be cached to memory, or the temporary file will be cached to disk. Finally, the server-side memory or temporary files in the file output to the specified directory, this directory is the specified file upload directory.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Using Common-fileupload for file uploads

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.