In the case of jsp + servlet, the multipart / form data file uploads the introduction

1,enctype="multipart/form-data"

<form method="post" action="route" enctype="multipart/form-data">

Set the enctype attribute in the form tag: enctype = "multipart / form data".
enctype specifies the encoding type used by the browser when sending data back to the server. enctype is short for EncodeType.
Attribute values are:

  • application/x-www-form-urlencoded: form data is encoded as name / value pairs. This is the standard encoding format;
  • Multipart / form data: the form data is encoded into a message. Each control on the page corresponds to a part of the message, which is used to upload attachments;
  • text/plain: form data is encoded in plain text without any controls or formatting characters.

Enctype = "multipart / form data" is used to set the MIME code of the form. By default, this code format is application/x-www-form-urlencoded and cannot be used for file upload; only multipart / form data can be used to completely transfer file data.

Chestnuts:

<form method="post" action="vip/vip.do" enctype="multipart/form-data">
    user name:<input type="text" name="username" id="username"><span style="color: aqua" id="msg"></span><br>
    password:<input type="password" name="password"><br>
    Gender:
    male<input type="radio" name="gender" value="male">
    female<input type="radio" name="gender" value="female"><br>
    head portrait<input type="file" name="profile" multiple/><br>
    <input type="submit" value="determine">
</form>

2. The servlet used to receive data must have the @ MultipartConfig annotation

This annotation tells the Servlet that it will process multipart / form data type requests.

@MultipartConfig annotation properties:

attributetypeRequired
maxFileSizelongno
maxRequestSizelongno
fileSizeThresholdintno
locationStringno

In fact, in Servlet 3 Before 0, the Servlet itself did not provide direct support for uploading files. Uploading files usually used Commons fileUpload XXX Jar and Commons IO XXX Jar two jar packages, including many related APIs. And in Servlet 3 0, some APIs have been improved. Files can be uploaded conveniently through @ MultipartConfig annotation and related methods.

If there is no annotation, it will be like this. I can't receive data. I didn't add @ MultipartConfig for a long time today. I didn't get anything.

3. Api related to file upload in Servlet

Methods provided by HttpServletRequest:

  • Part getPart(String name): get the file upload field according to the name
  • Collection < part > getparts(): get all file upload fields

Common methods in Part:

  • String getContentType(): gets the file type of the uploaded file
  • long getSize(): the size of the uploaded file
  • String getSubmittedFileName(): the original file name of the uploaded file
  • String getName(): get < input name = "Upload"... > Name attribute value in tag
  • String getHeader(String name): get the request header
  • Collection < string > getheadernames(): get all request header names
  • InputStream getInputStream(): get the input stream of the uploaded file
  • void write(String path): save the file to the server

Chestnuts:

/**
 * @Author: zhuqu01
 * @Date: 2021/8/31 9:50
 */
@MultipartConfig
public class VipServlet extends HttpServlet {
    
    private VipService vipService = new VipServiceImpl();
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // Processing requests
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String gender = req.getParameter("gender");
        Vip vip = new Vip(username,password,gender);
        // Process the uploaded file and obtain the file upload field according to the name
        Part profile = req.getPart("profile");
        // Get the original file name of the uploaded file
        String filename = profile.getSubmittedFileName();
        InputStream inputStream = profile.getInputStream();
        OutputStream outputStream = new FileOutputStream("D:\\upload\\profile\\" + filename);
        byte[] bytes = new byte[1024];
        int len = 0;
        while( (len = inputStream.read(bytes)) != -1){
            outputStream.write(bytes,0,len);
            outputStream.flush();
        }
        outputStream.close();
        inputStream.close();
    }
}

Keywords: Java JSP servlet

Added by ksb24930 on Fri, 17 Dec 2021 02:35:26 +0200