The blogger is recently working on an intranet project where external data can be accessed internally, but externally cannot access internal data, which also causes the file to be unable to be uploaded. Therefore, the blogger takes a different approach and creates a folder on the local server specifically for storing uploaded data.
Environment: jdk, tomcat
1. Upload files in the front desk (upload ajax)
<input type="file" name="annexUrl" id="annexUrl" multiple="multiple"/>
Where multiple="multiple" is set to upload multiple files
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 unique to jquery and is not serialized, because we are not a string in json format, but we need to pass the file processData: false, //Note that we must set contentType:false here, otherwise the string will be passed by default, so that the file cannot be passed through contentType: false, success: function (data) { save(data.data); } }); } else { save(); } } } Here we need to use formData object to encapsulate the file object. The save() method is used to save the file path returned after uploading, and save it to the database for easy download.
2. Receive files in the background and upload them to the server
// Upload multiple files, return a 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 stored real file name (unique) // I don't know why, file upload must contain the obtained file name, otherwise the syntax of the file name, directory name or volume tag is incorrect. There is no such restriction on image upload // File name, save the database, and use it to display the 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 (!file.isDirectory()) { //Create the file upload directory file.mkdirs(); } // The real path to the file storage String filePath = realPath + File.separator + realName; try { part.write(filePath); } catch (IOException e) { e.printStackTrace(); } } jsonObject.put("data", list); return jsonObject; } The list collection adds a string spliced by the file name and the real file name, which is needed later. realName is to prevent conflicts between multiple uploaded file names. realPath is the file saving path. Different operating systems have different paths. RealPath is best placed under tomcat to facilitate project porting.
Where File.separator is the path separator, which can automatically identify which operating system it is and use different path separators (windows is '/', linux is '/'). Finally return the list to the front desk.
This article has been compiled into "Summary of Java Upload Operation Techniques", and everyone is welcome to learn and read.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.