The process of uploading JSP files is very simple. You can post the data through the form and then accept the data through getPart and save the file. Next I will introduce you to an example of uploading JSP files, I hope to help you.
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> |