JSP file upload example study notes

Source: Internet
Author: User
Tags file upload

Example of uploading files to the server using JSP

1. An html file is required.

Upload.html (content as follows)

The code is as follows: Copy code

<Html>
<Head>
<Title> Upload </title>
</Head>
<Body>
<Form action = "HandleUpload" method = "post" enctype = "multipart/form-data">
<Input id = "file" name = "file" type = "file">
<Input type = "submit">
</Form>
</Body>
</Html>

 

2. Create a servlet to process the file uploaded from the front-end. The file is put under org. Rudiment. lab and named upload.

The code is as follows: Copy code


Package org. Rudiment. lab;

Import java. io. IOException;
Import java. io. PrintWriter;
Import java. util. Collection;
Import javax. servlet. ServletException;
Import javax. servlet. annotation. MultipartConfig;
Import javax. servlet. annotation. WebServlet;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Import javax. servlet. http. Part;

/* Configure the Servlet. This is a new feature of servlet 3.0. For other features, you can query javax. servlet. annotation through the api */
@ WebServlet (name = "upload", urlPatterns = {"/Handleupload "})
/* Configure the servlet to process binary data, which corresponds to the frontend enctype = "multipart/form-data */
@ MultipartConfig
Public class upload extends HttpServlet
{
@ Override
Public void service (HttpServletRequest request, HttpServletResponse response) throws IOException
    {
/* Set the content encoding generated by the response */
Response. setContentType ("text/html; charset = GBK ");
/* Get the output stream */
PrintWriter out = response. getWriter ();
Try {
/* This is the upload core class */
Part part = request. getPart ("file ");

/* Obtain the type of the uploaded file through the part */
Out. println ("Upload type:" + part. getContentType () + "<br/> ");
/* Get the size of the uploaded file */
Out. println ("size of the uploaded File:" + part. getSize () + "<br/> ");
/* Get the header information */
Collection <String> headerNames = part. getHeaderNames ();
       
/* Print header information traversal */
For (String headerName: headerNames)
        {
Out. println (headerName + "-------->" + part. getHeader (headerName) + "<br/> ");
        }
       
/* Upload the uploaded file to the application/uploadFiles. If you do not have this directory, create a file first */
Part. write (getServletContext (). getRealPath ("/uploadFiles") + "/" + System. currentTimeMillis ()/1000 );
       
} Catch (ServletException e ){
E. printStackTrace ();
        }
    }
}

The above format is Annotation for configuring servlet

If you want to use web. xml

@ WebServlet (name = "upload", urlPatterns = {"/Handleupload "})

@ MultipartConfig

Remove and configure the servlet in web. xml.

In the preceding example, the content of the configuration file in web. xml is as follows:

The code is as follows: Copy code

<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app version = "3.0"
Xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd>
<Display-name> </display-name>

<Servlet>
<Servlet-name> upload </servlet-name>
<Servlet-class> org. Rudiment. lab. upload </servlet-class>
<Multipart-config>
</Multipart-config>
</Servlet>
   
<Servlet-mapping>
<Servlet-name> upload </servlet-name>
<Url-pattern>/Handleupload </url-pattern>
</Servlet-mapping>
   
<Welcome-file-list>
<Welcome-file> upload.html </welcome-file>
</Welcome-file-list>
</Web-app>

Related Article

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.