棧括號匹配問題
阿新 • • 發佈:2022-04-03
package com.xxx.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.IOException; /** * 使用註解將Servlet標識為檔案上傳 * Servlet對錶單post請求封裝為part物件 */ @WebServlet("/Ser1") @MultipartConfig public class Ser1 extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); //獲取普通引數 String uname=req.getParameter("uname"); System.out.println(uname); //獲取part物件 Part part=req.getPart("upFile"); //獲取上傳的檔名 String fileName=part.getName(); System.out.println(fileName); //檔案路徑 String filePath=req.getServletContext().getRealPath("/"); System.out.println(filePath); //下載檔案 存放在工程目錄下out資料夾裡 part.write(filePath+"/"+fileName); } }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <!-- 檔案上傳 1.表單提交型別為post,表單型別為enctype=“multipart/form-data 2.設定提交地址 action 3.設定表單name --> <form method="post" enctype="multipart/form-data" action="Ser1"> name:<input type="text" name="uname"><br> 檔案:<input type="file" name="upFile"><br> <button type="submit">submit</button> </form> </body> </html>