深入学习了文件上传功能。通过使用 Servlet 实现文件的上传.
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/uploadServlet")
@MultipartConfig
public class FileUploadServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Part filePart = request.getPart("file");
String filePath = "path/to/save";
File file = new File(filePath);
filePart.write(filePath);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("
File uploaded successfully!
");}
} 标签:http,io,File,import,servlet,javax,10.17 From: https://www.cnblogs.com/bawangji666/p/18472898