File Upload Code

Source: Internet
Author: User

1.0 initialize on the page first

Code

<title> File Upload </title>      

Enctype= multi-part form data, and if Enctype= "Multipart/form-data" is more in the properties of the form form, you cannot use the Request.getparameter (Name property)

1.12: Create a test.jsp in the root directory of Web-root, implement the file upload function!

<% @page import= "Java.io.File"%><% @page import= "org.apache.commons.fileupload.disk.DiskFileItemFactory"% ><% @page import= "Org.apache.commons.fileupload.FileItem"%><% @page import= " Org.apache.commons.fileupload.servlet.ServletFileUpload "%><%@ page language=" java "import=" java.util.* "    pageencoding= "UTF-8"%><%//Solve garbled request.setcharacterencoding ("Utf-8"); The content of the request passed by the user is displayed, judging the type, if it is a regular normal form, then the execution mode one//otherwise is the file domain, the execution mode two//01. Judge whether it is a normal form or a file upload boolean flag=servletfileupload    . ismultipartcontent (Request);        if (flag) {//define a parser to parse each item in the request Diskfileitemfactory factory=new diskfileitemfactory ();        Parse upload created successfully servletfileupload upload=new servletfileupload (Factory);        List<fileitem> filelist=upload.parserequest (Request);        Each of the Fileitem represents a TABLE element iterator<fileitem> mytor=filelist.iterator ();            while (Mytor.hasnext ()) {Fileitem item=mytor.next (); if (item!=null{//For current validation, normal form or file upload if (Item.isformfield ())//proof is normal form field {                    Gets the form element content (value values) of String name=item.getfieldname (); The Name property of the form instead of the value if (Name.equals ("username")) {Out.print (                    Item.getstring ("Utf-8"));                    }}else{//is a file upload String filename=item.getname (); Throw to the server directory string path= "/web-inf/folder";//relative path string Paths=this.getservletcont                    Ext (). Getrealpath (path);                    Out.print (paths);                    File File=new file (filename);                    File Uploadfile=new file (Paths,file.getname ());                    Item.write (UploadFile);                Out.print ("Upload success"); }}}}%>

File Upload code

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.