单文件、多文件上传 – Tomcat 小半 • 2022年10月28日 下午7:13 • 其他分类 • 阅读 228 导读:本篇文章讲解 单文件、多文件上传 – Tomcat,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com 文章目录 1. 单文件上传 1.1 前端页面显示 1.2 Servlet后台处理请求 2. 多文件上传 2.1 前端页面显示 2.2 Servlet后台处理请求 表单 servlet前 注意事项 属性设置 → enctype:multipart/form-data 注解设置 → @MultipartConfig() 1. 单文件上传1.1 前端页面显示<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html><!-- 相对路径都以base路径为标准 --><% String scheme = request.getScheme() + "://"; String server = request.getServerName() + ":"; String port = request.getServerPort()+""; String projectName = request.getContextPath() + "/"; String basePath = scheme+server+port+projectName;%><html><head><meta charset="UTF-8"><base href="<%=basePath%>"><title>Insert title here</title></head><body> <form action="FileServlet" method="post" enctype="multipart/form-data"> <input type="file" name="newFile"/> <br><br> <input type="text" name="name"> <br><br> <input type="submit" name="上传文件" /> </form></body></html> 显示效果 1.2 Servlet后台处理请求 @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 获取有关上传文件的信息对象 Part filePart = req.getPart("newFile"); // 获取文件名 String fileName = filePart.getSubmittedFileName(); // 规定一个上传的文件存到服务器哪个位置 --- 必须绝对路径 String filePath = req.getServletContext().getRealPath("/File/") + fileName; // 获取前端上传的文件的字节流输入流对象 InputStream is = filePart.getInputStream(); // 将二进制字节流持久到硬盘 FileOutputStream fos = new FileOutputStream(filePath); byte[] buffer = new byte[1024]; int length = -1; while ((length = is.read(buffer)) != -1) { fos.write(buffer, 0, length); } is.close(); fos.close(); // 在前端提示上传成功的弹框 resp.setContentType("text/html;charset=utf-8"); resp.getWriter().print("<script>alert('上传成功')</script>"); } 2. 多文件上传 切记必须设置 不可缺少name的设置,否则servlet获取不了上传的文件流 2.1 前端页面显示 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String scheme = request.getScheme() + "://"; String serverName = request.getServerName() + ":"; String port = request.getServerPort()+""; String projectName = request.getContextPath() + "/"; String basePath = scheme+serverName+port+projectName; %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <base href="<%=basePath %>"> <title>Insert title here</title> <script src="js/jquery-3.4.1.min.js" type="text/javascript"></script> </head> <body> <button id="add_btn">添加</button><br><br> <form action="FileMulitiServlet" method="post" enctype="multipart/form-data"> <div class="container"></div> <input type="submit" name="上传文件"></input> </form> </body> <script> $(function() { // 页面必须始终保持一个<input>文件上传输入框 addFile(); var count = 1; // 文件上传添加按钮事件处理 $("#add_btn").click(function(){ addFile(); count++; }) // 文件上传按钮删除事件处理 $(".container").on("click", ".del_btn", function() { if(count == 1) { return; } $(this).parent().remove(); count--; }) // 增添文件上传输入框的方法 function addFile() { $(".container").append('<div><input name="file" type="file"><button type="button" class="del_btn">删除</button></div><br><br>'); } }) </script> </html> 页面显示 2.2 Servlet后台处理请求 @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 规定一个存文件的地方 String projectRealPath = req.getServletContext().getRealPath("/File/"); String filePath = null; // 定义初始化一个输出、输入流 OutputStream os = null; InputStream is = null; // 获取所有part参数对象 Collection<Part> parts = req.getParts(); // 查找全部的part参数对象 for(Part part : parts) { String fileName = part.getSubmittedFileName(); // 判断part这个对象是否存有文件名,有则说明是文件对象 if(fileName != null) { is = part.getInputStream(); filePath = projectRealPath + fileName; os = new FileOutputStream(filePath); byte[] buffer = new byte[2048]; int len = -1; // 将用户上传的文件持久化服务器硬盘上 while( (len=is.read(buffer)) != -1 ) { os.write(buffer, 0, len); } // 记得关闭 -- 否则文件的内容为空 is.close(); os.close(); } } // 上传文件成功,则显示信给客户 resp.setContentType("text/html;charset=utf-8"); resp.getWriter().write("上传成功"); } 运行效果 版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。 文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/46541.html 赞 (0) 小半 0 0 生成海报 微信扫码分享 相关推荐 其他分类 vue2 qrcodejs2链接生成二维码 000430 小半 2022年11月3日 微信精选 设计模式(二)-构建者模式 000318 小半 2023年1月11日 微信精选 上篇:docker基础玩转 000271 小半 2022年11月25日 其他分类 Vim学习1 000207 小半 2022年10月28日 微信精选 Ubuntu下使用Kubeadm搭建k8s集群 000417 小半 2024年2月20日 微信精选 CentOS7下docker环境安装RabbitMQ 000222 小半 2023年12月17日 微信精选 zookeeper基础命令 000422 小半 2024年2月2日 其他分类 django how to mock aws S3BotoStorage? 000290 小半 2022年11月3日 其他分类 windows10 时间显示秒数 010875 小半 2022年11月3日 其他分类 动态增加的元素无事件处理 — Jquery解决方案 000163 小半 2022年10月28日 其他分类 Nginx作为反向代理 000412 小半 2022年10月20日 微信精选 Gradle构建之生命周期 000245 小半 2023年1月15日 分享到:微博微信微信扫码分享QQ好友QQ空间Facebook豆瓣XTwitterWhatsApp 极客之家——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!
文章目录 1. 单文件上传 1.1 前端页面显示 1.2 Servlet后台处理请求 2. 多文件上传 2.1 前端页面显示 2.2 Servlet后台处理请求 表单 servlet前 注意事项 属性设置 → enctype:multipart/form-data 注解设置 → @MultipartConfig() 1. 单文件上传1.1 前端页面显示<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html><!-- 相对路径都以base路径为标准 --><% String scheme = request.getScheme() + "://"; String server = request.getServerName() + ":"; String port = request.getServerPort()+""; String projectName = request.getContextPath() + "/"; String basePath = scheme+server+port+projectName;%><html><head><meta charset="UTF-8"><base href="<%=basePath%>"><title>Insert title here</title></head><body> <form action="FileServlet" method="post" enctype="multipart/form-data"> <input type="file" name="newFile"/> <br><br> <input type="text" name="name"> <br><br> <input type="submit" name="上传文件" /> </form></body></html> 显示效果 1.2 Servlet后台处理请求 @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 获取有关上传文件的信息对象 Part filePart = req.getPart("newFile"); // 获取文件名 String fileName = filePart.getSubmittedFileName(); // 规定一个上传的文件存到服务器哪个位置 --- 必须绝对路径 String filePath = req.getServletContext().getRealPath("/File/") + fileName; // 获取前端上传的文件的字节流输入流对象 InputStream is = filePart.getInputStream(); // 将二进制字节流持久到硬盘 FileOutputStream fos = new FileOutputStream(filePath); byte[] buffer = new byte[1024]; int length = -1; while ((length = is.read(buffer)) != -1) { fos.write(buffer, 0, length); } is.close(); fos.close(); // 在前端提示上传成功的弹框 resp.setContentType("text/html;charset=utf-8"); resp.getWriter().print("<script>alert('上传成功')</script>"); } 2. 多文件上传 切记必须设置 不可缺少name的设置,否则servlet获取不了上传的文件流 2.1 前端页面显示 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String scheme = request.getScheme() + "://"; String serverName = request.getServerName() + ":"; String port = request.getServerPort()+""; String projectName = request.getContextPath() + "/"; String basePath = scheme+serverName+port+projectName; %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <base href="<%=basePath %>"> <title>Insert title here</title> <script src="js/jquery-3.4.1.min.js" type="text/javascript"></script> </head> <body> <button id="add_btn">添加</button><br><br> <form action="FileMulitiServlet" method="post" enctype="multipart/form-data"> <div class="container"></div> <input type="submit" name="上传文件"></input> </form> </body> <script> $(function() { // 页面必须始终保持一个<input>文件上传输入框 addFile(); var count = 1; // 文件上传添加按钮事件处理 $("#add_btn").click(function(){ addFile(); count++; }) // 文件上传按钮删除事件处理 $(".container").on("click", ".del_btn", function() { if(count == 1) { return; } $(this).parent().remove(); count--; }) // 增添文件上传输入框的方法 function addFile() { $(".container").append('<div><input name="file" type="file"><button type="button" class="del_btn">删除</button></div><br><br>'); } }) </script> </html> 页面显示 2.2 Servlet后台处理请求 @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 规定一个存文件的地方 String projectRealPath = req.getServletContext().getRealPath("/File/"); String filePath = null; // 定义初始化一个输出、输入流 OutputStream os = null; InputStream is = null; // 获取所有part参数对象 Collection<Part> parts = req.getParts(); // 查找全部的part参数对象 for(Part part : parts) { String fileName = part.getSubmittedFileName(); // 判断part这个对象是否存有文件名,有则说明是文件对象 if(fileName != null) { is = part.getInputStream(); filePath = projectRealPath + fileName; os = new FileOutputStream(filePath); byte[] buffer = new byte[2048]; int len = -1; // 将用户上传的文件持久化服务器硬盘上 while( (len=is.read(buffer)) != -1 ) { os.write(buffer, 0, len); } // 记得关闭 -- 否则文件的内容为空 is.close(); os.close(); } } // 上传文件成功,则显示信给客户 resp.setContentType("text/html;charset=utf-8"); resp.getWriter().write("上传成功"); } 运行效果 版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。 文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/46541.html 赞 (0) 小半 0 0 生成海报 微信扫码分享 相关推荐 其他分类 vue2 qrcodejs2链接生成二维码 000430 小半 2022年11月3日 微信精选 设计模式(二)-构建者模式 000318 小半 2023年1月11日 微信精选 上篇:docker基础玩转 000271 小半 2022年11月25日 其他分类 Vim学习1 000207 小半 2022年10月28日 微信精选 Ubuntu下使用Kubeadm搭建k8s集群 000417 小半 2024年2月20日 微信精选 CentOS7下docker环境安装RabbitMQ 000222 小半 2023年12月17日 微信精选 zookeeper基础命令 000422 小半 2024年2月2日 其他分类 django how to mock aws S3BotoStorage? 000290 小半 2022年11月3日 其他分类 windows10 时间显示秒数 010875 小半 2022年11月3日 其他分类 动态增加的元素无事件处理 — Jquery解决方案 000163 小半 2022年10月28日 其他分类 Nginx作为反向代理 000412 小半 2022年10月20日 微信精选 Gradle构建之生命周期 000245 小半 2023年1月15日 分享到:微博微信微信扫码分享QQ好友QQ空间Facebook豆瓣XTwitterWhatsApp 极客之家——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!