java enables multiple file uploads to local servers

  • 2020-12-21 18:03:02
  • OfStack

Recently, the blogger has been working on an Intranet project, where internal data can be accessed internally, but external data cannot be accessed externally, which may lead to the possibility that files cannot be uploaded. Therefore, the blogger takes a different approach and sets up a folder on the local server to store the uploaded data.

Environment: jdk, tomcat

1. Front Desk upload file (ajax upload)


<input type="file" name="annexUrl" id="annexUrl" multiple="multiple"/> 

Where multiple= "multiple", set the file to upload more


function uploadFile() { 
  var files = document.getElementById("annexUrl").files; 
  if (files.length != 0) { 
   var formData = new FormData(); 
   for (var i = 0; i < files.length; i++) { 
    var file = files[i]; 
    formData.append(file.name, file); 
   } 
   $.ajax({ 
    url: 'cdc/public/saveFiles', 
    type: 'POST', 
    cache: false, 
    data: formData, 
    // This parameter is jquery Specifically, we don't serialize because we're not json Format the string instead of the file  
    processData: false, 
    // Note that there 1 Need to set up contentType:false Otherwise, the default is to pass a string, so the file will not pass  
    contentType: false, 
    success: function (data) { 
     save(data.data); 
    } 
   }); 
  } else { 
   save(); 
  } 
 } 

Here we need to use the formData object to encapsulate the file file object. The save () method is used to save the file path returned after uploading to the database for easy download.

2. Receive files in the background and upload them to the server


//  Multiple file uploads, return 1 Collection of objects (attachment address, name)  
 @RequestMapping(value = "saveFiles", method = RequestMethod.POST) 
 @ResponseBody 
 public JSONObject saveFiles(HttpServletRequest request, HttpServletResponse response) { 
  JSONObject jsonObject = new JSONObject(); 
  try { 
   request.setCharacterEncoding("UTF-8"); 
  } catch (UnsupportedEncodingException e) { 
   e.printStackTrace(); 
  } 
  Collection<Part> parts = null; 
  try { 
   parts = request.getParts(); 
  } catch (IOException e) { 
   e.printStackTrace(); 
  } catch (ServletException e) { 
   e.printStackTrace(); 
  } 
 
  Iterator<Part> iterator = parts.iterator(); 
//   Collection of names, returned to the foreground  
  List<String> list = new ArrayList<>(); 
  while (iterator.hasNext()) { 
   Part part = iterator.next(); 
   //  Generate the actual file name of the actual store ( only 1) 
//    I don't know why file uploads must include the filename they get, otherwise the file name, directory name, or volume tag are incorrect. There is no such restriction for image uploads  
//    File name, save guide database, used for interface display  
   String name = part.getName(); 
   String realName = UUID.randomUUID().toString() + name; 
 
   list.add(name + "&&" + realName); 
   ///home/tomcat/apache-tomcat-9.0.1/files 
   String realPath = "D:" + File.separator + "apache-tomcat-8.5.15" + File.separator + "files"; 
   // String realPath = "C:" + File.separator + "XHJ224" + File.separator + "software" + File.separator + "apache-tomcat-9.0.1" + File.separator + "files"; 
//    String realPath = File.separator + "home" + File.separator + "tomcat" + File.separator + "apache-tomcat-9.0.1" + File.separator + "files"; 
   File file = new File(realPath); 
   // If the directory does not exist  
   if (!file.isDirectory()) { 
    // Create a file upload directory  
    file.mkdirs(); 
   } 
   //  The actual path to the file  
   String filePath = realPath + File.separator + realName; 
   try { 
    part.write(filePath); 
   } catch (IOException e) { 
    e.printStackTrace(); 
   } 
  } 
  jsonObject.put("data", list); 
  return jsonObject; 
 } 

Among them, list collection adds a string concatenated by file name and real file name, which is required by save later. realName is to prevent multiple upload filename conflicts,realPath is the file saving path, different operating systems have different paths, realPath is best placed under tomcat, easy for project migration.

File. separator is the path separator, which can automatically identify which operating system is using different path separators (windows is' \',linux is' /'). Finally, the list is returned to the front desk.

This article has been collected in Java Upload operation Skills Summary, welcome to learn to read.


Related articles: