- @MultipartConfig是Java Servlet API的一部分,主要用于处理HTTP的multipart/form-data类型的请求,这种请求通常用于文件上传。当你在Servlet或JSP页面中使用@MultipartConfig时,你告诉容器这个Servlet或JSP页面将处理文件上传。
- @MultipartConfig(location ="C:/Users/Administrator/Desktop" )这个只会将上传的图片文件临时存到Desktop文件夹中,名称为随机upload_e8a306b4_1327_483e_afe5_86092f39b380_00000000.tmp,这里不采用.
| 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; |
| import java.io.PrintWriter; |
| import java.nio.file.Files; |
| import java.nio.file.Path; |
| import java.nio.file.Paths; |
| import java.nio.file.StandardCopyOption; |
| |
| |
| import org.apache.catalina.core.ApplicationPart; |
| @WebServlet("/UploadServlet") |
| @MultipartConfig |
| public class UploadServlet extends HttpServlet { |
| @Override |
| protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| resp.setContentType("text/html;charset=UTF-8"); |
| PrintWriter out = resp.getWriter(); |
| |
| String path = this.getServletContext().getRealPath("/upload"); |
| |
| Part p = req.getPart("file1"); |
| |
| |
| String originalFileName = Paths.get(p.getSubmittedFileName()).getFileName().toString(); |
| |
| if (p.getContentType().contains("image")) { |
| |
| |
| Path saveFilePath = Paths.get(path, originalFileName); Files.copy(p.getInputStream(),saveFilePath,StandardCopyOption.REPLACE_EXISTING); |
| out.write("文件上传成功"); |
| }else{ |
| out.write("请选择图片文件"); |
| out.flush(); |
| out.close(); |
| } |
| |
| } |
| } |
编辑upload.jsp页面
| <%-- |
| Created by IntelliJ IDEA. |
| User: Administrator |
| Date: 2024/4/22 |
| Time: 21:27 |
| To change this template use File | Settings | File Templates. |
| --%> |
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> |
| <html> |
| <head> |
| <title>Insert title here</title> |
| </head> |
| <body> |
| <form action="UploadServlet" enctype="multipart/form-data" method="post"> |
| 选择文件<input type="file" name="file1"/> |
| <input type="submit" name="upload" value="上传"/> |
| </form> |
| </body> |
| </html> |
- 第二种方法,使用InputStream和OutStream上传文件
| 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.*; |
| import java.nio.file.Path; |
| import java.nio.file.Paths; |
| @WebServlet("/UploadServlet1") |
| @MultipartConfig |
| public class UploadServlet2 extends HttpServlet { |
| @Override |
| protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| resp.setContentType("text/html;charset=UTF-8"); |
| PrintWriter out = resp.getWriter(); |
| |
| String path = this.getServletContext().getRealPath("/"); |
| |
| Part filePart = req.getPart("file1"); |
| |
| String originalFileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); |
| |
| if (filePart.getContentType().startsWith("image/")){ |
| |
| String savePath = path + "/upload/" + originalFileName; |
| |
| try (InputStream input = filePart.getInputStream(); |
| OutputStream output = new FileOutputStream(savePath)){ |
| byte[] buffer = new byte[1024]; |
| int bytesRead; |
| while((bytesRead = input.read(buffer))!= -1){ |
| output.write(buffer,0,bytesRead); |
| } |
| |
| out.write("文件上传成功,文件名:" + originalFileName); |
| }catch(Exception e){ |
| |
| out.write("文件上传失败:" + e.getMessage()); |
| } |
| }else{ |
| |
| out.write("请选择图片文件"); |
| } |
| |
| } |
| } |
修改upload.jsp页面,修改action="UploadServlet1"即可.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)