SpringMVC + servlet3.0 file upload configuration and implementation code

  • 2020-06-19 10:25:24
  • OfStack

Simple steps, SpringMVC+servlet3.0 file upload function:

Step 1: Configure servlet in the ES5en. xml file and add ES8en-ES9en:


<!-- SpringMVC --> 
<servlet> 
 <servlet-name>myWeb</servlet-name> 
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
 <init-param> 
  <param-name>contextConfigLocation</param-name> 
  <param-value>classpath:springmvc/servlet.xml</param-value> 
 </init-param> 
 <load-on-startup>1</load-on-startup> 
 <multipart-config> 
  <!-- <location>/</location> --> 
  <max-file-size>5242880</max-file-size> <!-- Maximum size of single file: 5MB--> 
  <max-request-size>20971520</max-request-size> <!-- Maximum size of all files: 20MB--> 
  <file-size-threshold>0</file-size-threshold> <!--  Beyond this size, directly store the hard disk instead of the memory  --> 
 </multipart-config> 
</servlet> 
 
<servlet-mapping> 
 <servlet-name>myWeb</servlet-name> 
 <url-pattern>/</url-pattern> 
</servlet-mapping> 

Step 2: Configure MultipartResolver in ES13en.xml:


<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"/> 

Step 3: Create Controller to receive the form form:


package com.xjj.web.controller; 
 
import java.io.File; 
import java.io.IOException; 
import java.util.Map; 
 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.Part; 
 
import org.springframework.stereotype.Controller; 
import org.springframework.util.StringUtils; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.ResponseBody; 
 
import com.xjj.json.JsonResult; 
 
/** 
 *  File upload  
 * @author XuJijun 
 * 
 */ 
@Controller 
@RequestMapping("/servlet/file") 
public class FileUploadController { 
  
 /** 
  *  Save the file in the directory placed web Directory, or 1 Under the specified absolute directory  
  */ 
  private static final String SAVE_DIR = "uploadFiles"; 
  
 @RequestMapping("/upload") 
 public @ResponseBody JsonResult upload(HttpServletRequest request, HttpServletResponse response, @RequestParam Map<String, Object> p) 
   throws ServletException, IOException { 
 
  //  To obtain  web application Absolute path of  
  String appPath = request.getServletContext().getRealPath(""); 
   
  //  Construct the path where the file is stored  
  String savePath = appPath + File.separator + SAVE_DIR; 
 
  //  If the file storage path does not exist mkdir1 a  
  File fileSaveDir = new File(savePath); 
  if (!fileSaveDir.exists()) { 
   fileSaveDir.mkdir(); 
  } 
 
  for (Part part : request.getParts()) { 
   String fileName = extractFileName(part); 
   if(!StringUtils.isEmpty(fileName)){ 
    part.write(savePath + File.separator + fileName); 
   } 
  } 
 
  return new JsonResult("200", " File upload successful! ", savePath); 
 } 
  
 /** 
  *  from content-disposition Gets the source file name in the header  
  * 
  * content-disposition The format of the header is as follows:  
  * form-data; name="dataFile"; filename="PHOTO.JPG" 
  * 
  * @param part 
  * @return 
  */ 
 private String extractFileName(Part part) { 
  String contentDisp = part.getHeader("content-disposition"); 
  String[] items = contentDisp.split(";"); 
  for (String s : items) { 
   if (s.trim().startsWith("filename")) { 
    return s.substring(s.indexOf("=") + 2, s.length()-1); 
   } 
  } 
  return ""; 
 } 
 
} 

request.getParts () is used to get multipart, which includes files. other < input type="text" > The parameters are @RequestParam Map < String, Object > p receives.

Step 4: form form submission file and other data:


<html> 
<head> 
<meta charset="UTF-8"> 
 <link href="../resources/css/common.css" rel="external nofollow" rel="stylesheet" /> 
 <script src="../resources/js/jquery-2.1.4.js"></script> 
  
</head> 
 
<body> 
<h2>File Upload</h2> 
 <form method="post" enctype="multipart/form-data"> 
  <input type="text" name="aaa"/><br/> 
   Select the file to upload: <input type="file" name="file" size="60" /><br/> 
  <input type="file" name="file" size="60" /><br/> 
  <br/> <!-- <input type="submit" value=" To upload " /> --> 
 </form> 
  
 <input type="button" value=" Upload! " onclick="upload()"/> 
</body> 
 
<script> 
 function upload(){ 
  $("form").attr('action', "http://localhost:8080/MyJavaStudio/servlet/file/upload"); 
  $("form").submit(); 
 } 
</script> 
 
</html> 

Note: submit form via jQuery, free assembly of action url, free definition submit button.

github: https: / / github com/xujijun/MyJavaStudio

Es61EN_jb51.rar


Related articles: