SERVLET3.0 Study Summary (iii)--file upload based on Servlet3.0

Source: Internet
Author: User

SERVLET3.0 Study Summary (iii)--file upload based on Servlet3.0

In Servlet2.5, when we want to implement the file upload function, we generally need to use third party open source components, such as Apache commons-fileupload components, in Servlet3.0 to provide the native support for file upload, we do not need any third party upload components, directly using Ser The API provided by vlet3.0 enables file upload functionality.

One, using the API provided by Servlet3.0 to implement file upload 1.1, write upload page
 1 <%@ page language= "java" pageencoding= "UTF-8"%> 2 <!    DOCTYPE html> 3 1.2. Develop servlet to process file upload

  1. Use annotations @multipartconfig to identify a servlet as a support file upload.

2, Servlet3.0 the Multipart/form-data POST request into part, through the part of the uploaded files to operate .

The Uploadservlet code is as follows:

 1 package Me.gacl.web.controller; 2 3 Import Java.io.File; 4 Import java.io.IOException; 5 Import Java.io.PrintWriter; 6 Import java.util.Collection; 7 8 Import Javax.servlet.ServletException; 9 Import javax.servlet.annotation.multipartconfig;10 Import javax.servlet.annotation.webservlet;11 Import JAVAX.SERVLET.HTTP.HTTPSERVLET;12 Import javax.servlet.http.httpservletrequest;13 Import JAVAX.SERVLET.HTTP.HTTPSERVLETRESPONSE;14 Import javax.servlet.http.part;15 16// Use @webservlet to configure the access path for Uploadservlet @WebServlet (name= "Uploadservlet", urlpatterns= "/uploadservlet") 18// Using annotations @multipartconfig to identify a servlet as a support file upload @MultipartConfig//identity servlet support File upload the public class Uploadservlet extends HttpServlet {doget (httpservletrequest request, httpservletresponse response) throws Se Rvletexception, IOException {request.setcharacterencoding ("Utf-8"); Response.setcharacterenc Oding ("Utf-8"); Response.setcontenttype ("Text/html;chaRset=utf-8 "); 27//Storage path: String Savepath = Request.getservletcontext (). Getrealpath ("/web-inf/uploa Dfile "); 29//Get uploaded collection of files collection<part> parts = Request.getparts (); 31//Upload a single file if (Parts.size () ==1) {//servlet3.0 encapsulates the Multipart/form-data POST request into part, the uploaded file is manipulated by part. //part part = parts[0];//from the uploaded collection of files to the Section object ("file").//Through form                 The name of the file control (<input type= "file" name= "file" >) directly gets the part object, and//servlet3 does not provide a way to get the file name directly, which needs to be parsed from the request header 37 Gets the request header, the format of the request header: Form-data; Name= "File"; Filename= "Snmp4j--api.zip" the String Header = Part.getheader ("Content-disposition"); 39//Get FileName: String filename = GetFileName (header); 41//write the file to the specified path Part.write (SA            Vepath+file.separator+filename)}else {44//upload multiple files at once 45     For the (part Part:parts) {///loop processing of uploaded files 46//Get request header, format of request header: Form-data; name= "file"; filename= "snmp4j--a                     Pi.zip "String Header = Part.getheader (" content-disposition "); 48//Get file name 49 String fileName = GetFileName (header); 50//write the file to the specified path Part.write (s Avepath+file.separator+filename);}53}54 PrintWriter out = Response.getwriter () ; Out.println ("Upload success"); Out.flush (); Out.close (); 58}59 60/**61 * Parse out the file name according to the request header 62 * The format of the request header: Firefox and Google browser: form-data; Name= "File"; Filename= "Snmp4j--api.zip" under IE browser: form-data; Name= "File"; Filename= "E:\snmp4j--api.zip" * @param header Request Header * @return filename */67 public String getfilename (St Ring header) {/**69 * string[] tempArr1 = Header.split (";"); After the code has been executed, the contents of the TEMPARR1 array are slightly different under various browsers 70         * Under Firefox or Google Browser: temparr1={form-data,name= "file", filename= "snmp4j--api.zip"}71 * IE Browser: temparr1={form-d Ata,name= "File", filename= "E:\snmp4j--api.zip"}72 */73 string[] tempArr1 = Header.split (";"); 74/**75 * Firefox or Google browser: temparr2={filename, "Snmp4j--api.zip"}76 *ie Browser: temparr2={filename, "e:\         Snmp4j--api.zip "}77 */78 string[] tempArr2 = temparr1[2].split (" = "); 79//Get file name, compatible with various browser notation 80 String fileName = temparr2[1].substring (temparr2[1].lastindexof ("\ \") +1). ReplaceAll ("\" "," "); Bayi return Filenam e;82}83-public void DoPost (HttpServletRequest request, httpservletresponse response)-Throws Servletexception, IOException {this.doget (request, response); 87}88}

The results of the operation are as follows:

  

As you can see, it is very convenient to use the API provided by Servlet3.0 to implement the file upload function.

SERVLET3.0 Study Summary (iii)--file upload based on Servlet3.0

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.