I won’t say much nonsense, I go straight to the point.
Two jar packages are required:
commons-fileupload.jar
Commons IO jar package (using commons-io-2.4.jar in this article)
Use Servlet to implement file upload.
package web.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.File;import java.util.Iterator;import java.util.List;import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;/*** Servlet implementation class UploadServlet*/@WebServlet("/UploadServlet")public class UploadServlet extends HttpServlet {private static final long serialVersionUID = 1L;private String uploadPath = "D://temp"; // directory for uploading file private String tempPath = "d://temp//buffer//"; // temporary file directory File tempPathFile;public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {try {// Create a factory for disk-based file itemsDiskFileItemFactory factory = new DiskFileItemFactory();// Set factory constraintsfactory.setSizeThreshold(4096); // Set the buffer size, here is 4kbfactory.setRepository(tempPathFile);// Set the buffer directory// Create a new file upload handlerServletFileUpload upload = new ServletFileUpload(factory);// Set overall request size constraintupload.setSizeMax(4194304); // Set maximum file size, here is 4MBList<FileItem> items = upload.parseRequest(request);// Get all the files Iterator<FileItem> i = items.iterator();while (i.hasNext()) {FileItem fi = (FileItem) i.next();String fileName = fi.getName();if (fileName != null) {File fullFile = new File(fi.getName());File savedFile = new File(uploadPath, fullFile.getName());fi.write(savedFile);}}System.out.print("upload successe");} catch (Exception e) {// The error page can be jumped e.printStackTrace();}}public void init() throws ServletException {File uploadFile = new File(uploadPath);if (!uploadFile.exists()) {uploadFile.mkdirs();}File tempPathFile = new File(tempPath);if (!tempPathFile.exists()) {tempPathFile.mkdirs();}}} jsp
<%@ page language="java" contentType="text/html; charset=ISO--"pageEncoding="utf-"%><!DOCTYPE html PUBLIC "-//WC//DTD HTML . Transitional//EN" "http://www.w.org/TR/html/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=GB"><title>File upload</title></head><body><!-- // action="fileupload" corresponds to the setting of <url-pattern> in <servlet-mapping> in web.xml. --><form name="myform" action="UploadServlet" method="post"enctype="multipart/form-data">File:<br><input type="file" name="myfile"><br><br><input type="submit" name="submit" value="Commit"></form></body></html>
This will simply implement a file upload function. Of course, this is the most basic one, and continue to study.