I won’t say much nonsense, I will just post the code to you. The specific code is as follows:
The html code is as follows:
<body><input id="fileid" type="file" accept="video/*;capture=camera"onchange="onfile(this)"><input id="btn" type="button" value="submit"><script type="text/javascript">var xhr;function onfile(file) {var fd = new FormData();fd.append("fileToUpload", document.getElementById('fileid').files[0]);xhr = new XMLHttpRequest();xhr.open("POST", "http://localhost:8889/TestUp/upservlet");//xhr.setRequestHeader("Content-Type","charset=UTF-8");xhr.send(fd);xhr.onreadystatechange = processResponse;}function processResponse(){if(xhr.readyState == 4){alert("Upload end data stream end");if(xhr.status == 200){var infor = xhr.responseText;alert("Server side response = "+infor);}}}</script></body>The java code looks like this:
package com.yjm.up;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;public class UpServlet extends HttpServlet {public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// Get the saved directory of the uploaded file, store the uploaded file in the WEB-INF directory, and does not allow direct access from the outside world to ensure the security of the uploaded file String savePath = this.getServletContext().getRealPath("/WEB-INF/upload");File file = new File(savePath);System.out.println("test...");// Determine whether the saved directory of the uploaded file exists if (!file.exists() && !file.isDirectory()) {System.out.println(savePath + "The directory does not exist, needs to be created");// Create the directory file.mkdir();}// The message prompts String message = "";try {// Use the Apache file upload component to process file upload steps: // 1. Create a DiskFileItemFactory factory DiskFileItemFactory factory = new DiskFileItemFactory();// 2. Create a file upload parser ServletFileUpload upload = new ServletFileUpload(factory);// Solve the Chinese garbled upload.setHeaderEncoding("UTF-8");// 3. Determine whether the submitted data is the data of the upload form if (!ServletFileUpload.isMultipartContent(request)) {// Get data in the traditional way;}// 4. Use the ServletFileUpload parser to parse the uploaded data. The parsing result returns a List<FileItem> collection. Each FileItem corresponds to the input item of a Form form List<FileItem> list = upload.parseRequest(request); for (FileItem item : list) {// If the data of a normal input item is encapsulated in the fileitem if (item.isFormField()) {String name = item.getFieldName();// Solve the Chinese garbled problem of data of ordinary input items String value = item.getString("UTF-8");// value = new String(value.getBytes("iso8859-1"),"UTF-8");System.out.println(name + "=" + value);} else {// If the file item encapsulates the uploaded file // Get the uploaded file name, String filename = item.getName();System.out.println(filename);if (filename == null || filename.trim().equals("")) {continue;}// Note: The file names submitted by different browsers are different. Some browsers submit files with paths, such as: // c:/a/b/1.txt, and some are just simple file names, such as: 1.txt// Process the path part of the file name of the obtained uploaded file, only the file name part is retained filename = filename.substring(filename.lastIndexOf("//") + 1);// Get the input stream of the uploaded file in the item InputStream in = item.getInputStream();// Create a file output stream FileOutputStream out = new FileOutputStream(savePath + "//"+ filename);// Create a buffer byte buffer[] = new byte[1024 * 1024];// Create a buffer byte buffer[] = new byte[1024 * 1024];// Create a file output stream FileOutputStream out = new FileOutputStream(savePath + "//"+ filename);// Create a buffer byte buffer[] = new byte[1024 * 1024];// The identifier of determining whether the data in the input stream has been read is int len = 0;// Loop to read the input stream into the buffer. (len=in.read(buffer))>0 means that there is still data in in while ((len = in.read(buffer)) > 0) {// Use FileOutputStream output stream to write the data in the buffer to the specified directory (savePath + "//"// + filename) out.write(buffer, 0, len);}out.flush();// Close the input stream in.close();// Close the output stream out.close();// Delete the temporary file generated when processing file upload item.delete(); message = "File upload successfully!";}}} catch (Exception e) {message = "File upload failed!";e.printStackTrace();}request.setAttribute("message", message);request.getRequestDispatcher("/message.jsp").forward(request, response);}}The java package used is uploaded more than 1G
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
//xhr.setRequestHeader("Content-Type","application/octet-stream;charset=UTF-8"); This cannot be added