Create a File Upload form

Source: Internet
Author: User
Tags html form

The JSP can upload files to the server via HTML form forms. The file type can be any other document, such as a text file, a binary file, an image file, and so on.

Create a File Upload form

Next we use HTML tags to create the file upload form, here are the points to note:

    • The Form form method property must be set to POST and cannot be used with the GET method.
    • The form form Enctype property needs to be set to Multipart/form-data.
    • The form form Action property needs to be set to the JSP file address to submit to the background processing file upload. For example, uploadfile.jsp program files are used to process uploaded files.
    • The upload file element requires the <input .../> tag with the property set to Type= "file". If you need to upload multiple files, you can set a different name in the <input .../> tab.

The following is a form for uploading a file, with the following examples:

In your local browser to access the file, the display interface as shown below, when you click on "Upload file" will pop up a window to let you select the file to upload:

Background JSP processing script

First we define the location of the file to be stored on the service, you can write the path in your program, or we can set the file storage directory in the Web. XML configuration file by setting the Context-param element as follows:

<web-app>....<context-param>     <description> File upload address </description>     <param-name >file-upload</param-name>     <param-value>         c:\apache-tomcat-5.5.29\webapps\data     </ Param-value> </context-param>....</web-app>

The following script file uploadfile.jsp can process multiple uploaded files, before using this script, we need to pay attention to the following points:

    • The following instances depend on FileUpload, so you need to introduce the latest Commons-fileupload.x.x.jar package files in your classpath. As: http://commons.apache.org/fileupload/.
    • FileUpload relies on Commons IO, so you need to introduce the latest Commons-io-x.x.jar in your classpath. As: http://commons.apache.org/io/.
    • When you test the following instance, you need to upload a confirmation that the uploaded file size is less than the size of the maxfilesize variable, or the file cannot be uploaded successfully.
    • Make sure you have created the directories C:\Temp and C:\apache-tomcat-5.5.29\webapps\data.
<%@ page import= "java.io.*,java.util.*, javax.servlet.*"%><%@ page import= "javax.servlet.http.*"%><% @ Page import= "org.apache.commons.fileupload.*"%><%@ page import= "org.apache.commons.fileupload.disk.*"%> <%@ page import= "org.apache.commons.fileupload.servlet.*"%><%@ page import= "Org.apache.commons.io.output.   * "%><% file file;   int maxfilesize = 5000 * 1024;   int maxmemsize = 5000 * 1024;   ServletContext context = Pagecontext.getservletcontext ();   String FilePath = Context.getinitparameter ("File-upload");   Verify that the uploaded content is of type String ContentType = Request.getcontenttype ();      if ((Contenttype.indexof ("Multipart/form-data") >= 0)) {diskfileitemfactory factory = new Diskfileitemfactory ();      Sets the maximum value of the stored file in memory Factory.setsizethreshold (maxmemsize);      The data stored locally is larger than maxmemsize.      Factory.setrepository (New File ("C:\\Temp"));      Create a new file upload handler servletfileupload upload = new Servletfileupload (factory); Set the maximum uploaded file sizeSmall Upload.setsizemax (maxfilesize);         try{//Parse Gets the file List Fileitems = upload.parserequest (request);         Processing the uploaded file Iterator i = Fileitems.iterator ();         Out.println ("

Next let's access the http://localhost:8080/UploadFile.htmthrough the browser, the interface is as follows, and upload the file:

If your JSP script is working properly, the file will be uploaded to C:\apache-tomcat-5.5.29\webapps\data\, and you can open the folder to see if the upload was successful.

Create a File Upload form

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.