Java Realizes Multi file Upload Function

  • 2021-10-13 07:40:54
  • OfStack

File uploading is a common function in development. Before servlet 3.0, one plug-in technology was needed to realize file uploading, such as:

commons-fileupload smartupload

However, after 3.0, servlet integrates file uploading technology (multipart), and the implementation process of servlet 3.0 file uploading is as follows:

1. The form submission mode must be set to post
2. The enctype of the form must be set to multipart/form-data (submit data using binary flow)
3. Add the @ MultipartConfig annotation to the servlet class

Contains four settable parameters:

Maximum size of fileSizeThreshold memory cache (use temporary file cache when the number of bytes of uploaded files reaches this value) Storage Directory for location Temporary Files Maximum limit of single file allowed to upload by maxFileSize Total number of bytes allowed to submit an maxRequestSize form

Page side


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 <form action="upload3" method="post" enctype="multipart/form-data">
  <input type="text" name="fname" placeholder=" Please enter a file name " /> <br />
  <input type="file" name="myfile" multiple/>
  <button> Upload </button>
 </form>
</body>
</html>

Server side


package com.softeem.servlet;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.UUID;

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;

@WebServlet({ "/UploadServlet", "/upload" })
@MultipartConfig(
  // Set the maximum memory cache space (use temporary file cache when the number of bytes of uploaded files reaches this value) 
  fileSizeThreshold=1024*1024,
  // Set the storage directory of temporary files 
  location="d:/temp",
  // Set the maximum limit of a single file allowed to be uploaded 
  maxFileSize=1024*1024*200,
  // Set the maximum number of bytes allowed to submit for a form 
  maxRequestSize=1024*1024*500
  )
public class UploadServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
       
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
  String basePath = "d:/myfileserver";
  Collection<Part> parts = request.getParts();
  for(Part part:parts){
   if(part.getSize() > 0){
    String fname = part.getSubmittedFileName();
    // Random generation 1 A uuid As a file name 
    String uuid = UUID.randomUUID().toString();
    // Get the file suffix 
    String suffix = fname.substring(fname.lastIndexOf("."));
    // Combination uuid And file suffix to become the new file name 
    fname = uuid+suffix;
    
    part.write(basePath+File.separator+fname);
   }
  }
 }
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  doGet(request, response);
 }

}

Related articles: