SpringMVC11文件上传详解编程语言
2023-06-13 09:20:36 时间
1 %@ page language="java" import="java.util.*" pageEncoding="UTF-8"% 2 % 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 % 6 !DOCTYPE HTML 7 html 8 head 9 base href=" %=basePath% " 11 title My JSP index.jsp starting page /title 12 meta http-equiv="pragma" content="no-cache" 13 meta http-equiv="cache-control" content="no-cache" 14 meta http-equiv="expires" content="0" 15 meta http-equiv="keywords" content="keyword1,keyword2,keyword3" 16 meta http-equiv="description" content="This is my page" 18 !-- 19 link rel="stylesheet" type="text/css" href="styles.css" 20 -- 22 /head 23 body 24 form action="user/login" method="post" enctype="multipart/form-data" 25 input type="file" name="imgs"/ 26 input type="file" name="imgs"/ 27 input type="file" name="imgs"/ 28 button 上传文件 /button 29 /form 31 /body 32 /html
index.jsp文件上传的页面
%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; !DOCTYPE HTML html head base href=" %=basePath% " title My JSP index.jsp starting page /title meta http-equiv="pragma" content="no-cache" meta http-equiv="cache-control" content="no-cache" meta http-equiv="expires" content="0" meta http-equiv="keywords" content="keyword1,keyword2,keyword3" meta http-equiv="description" content="This is my page" !-- link rel="stylesheet" type="text/css" href="styles.css" /head body form action="user/login" method="post" enctype="multipart/form-data" input type="file" name="imgs"/ input type="file" name="imgs"/ input type="file" name="imgs"/ button 上传文件 /button /form /body /html
%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" html head base href=" %=basePath% " title My JSP success.jsp starting page /title meta http-equiv="pragma" content="no-cache" meta http-equiv="cache-control" content="no-cache" meta http-equiv="expires" content="0" meta http-equiv="keywords" content="keyword1,keyword2,keyword3" meta http-equiv="description" content="This is my page" !-- link rel="stylesheet" type="text/css" href="styles.css" /head body h1 webroot success页面 /h1 /body /html
success.jsp成功界面
@Controller @RequestMapping("/user") public class MyController { /** * 单个文件上传 * MultipartFile img :必须和表单中的name属性值一致 * @throws Exception * @throws IllegalStateException @RequestMapping(value = "/login") public String login(MultipartFile img, HttpSession session) throws IllegalStateException, Exception { System.out.println("进入了login......"); // 获取上传的位置 String path = session.getServletContext().getRealPath("/images"); // 获取文件的名称 String filename = img.getOriginalFilename(); System.out.println(img.getOriginalFilename()); System.out.println(img.getName()); // 表单中name的属性值 System.out.println(img.getContentType()); // 上传文件的类型 // 上传文件只能是图片 通过MIME类型 来判断 if (img.getContentType().equals("image/jpeg")) { img.transferTo(new File(path, filename)); // 上传 return "/success.jsp"; }*/ /** * 多个文件上传 * @RequestParam 必须使用! [email protected] * 默认前台的每一个imgs都是一个数组! * 我们想要的是前台的所有上传文件 在一个数组里面! @RequestMapping(value = "/login") public String login(@RequestParam MultipartFile[] imgs, HttpSession session) throws IllegalStateException, Exception { System.out.println("进入了login......"); // 获取上传的位置 String path = session.getServletContext().getRealPath("/images"); // 循环获取文件的名称 for (MultipartFile img : imgs) { // 判断用户是否选择文件 if (img.getSize() 0) { String filename = img.getOriginalFilename(); img.transferTo(new File(path, filename)); // 上传 return "/success.jsp"; }
对应的controller代码
?xml version="1.0" encoding="UTF-8"? beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
mvc:annotation-driven/ !-- 配置文件上传 id必须是multipartResolver 因为已经在中央调度器 中定义了 Well-known name for the MultipartResolver object in the bean factory for this namespace. */ public static final String MULTIPART_RESOLVER_BEAN_NAME = "multipartResolver"; bean id="multipartResolver" property name="defaultEncoding" value="utf-8"/ !--乱码问题 -- property name="maxUploadSize" value="1048576"/ !-- 文件总大小不能超过 1M-- !-- property name="maxUploadSizePerFile" value="1048576"/ 单个文件不能超过1m -- /bean
相关文章
- 实现大文件上传和断点续传实践经验总结
- Vue上传文件操作(没有CV,认真看)
- Web文件上传靶场 - 通关笔记
- linux实现对上传文件的定时备份、增量备份的方法
- Spring Cloud Feign的文件上传实现详解编程语言
- python通过pysftp加密上传、下载ftp服务器文件详解编程语言
- JavaWeb学习总结(五十)——文件上传和下载详解编程语言
- spring mvc(注解)上传文件的简单例子详解编程语言
- 判断文件上传的类型详解编程语言
- springMVC3学习(十二)–文件上传优化CommonsMultipartResolver详解编程语言
- 使用commons-fileupload实现文件上传详解编程语言
- 实现上传文件但隐藏选择文件的文字详解编程语言
- Springboot文件上传出现找不到指定系统路径详解编程语言
- Springboot后端接口开发-文件上传接口详解编程语言
- ABAP-TXT文件上传详解编程语言
- ABAP_DEBUG时通过文件上传数据到内表详解编程语言
- Linux FTP上传文件报错解决方法(linuxftp无法上传)
- 器Linux环境下的文件上传服务指南(linux上传服务)
- 如何提高MySQL上传文件速度(mysql上传文件速度)
- 将文件上传、下载(以二进制流保存到数据库)实现代码
- 解析Jquery的LigerUI如何实现文件上传
- php文件上传的简单实例
- PHP上传文件时文件过大$_FILES为空的解决方法
- 使用AjaxFileUpload.js实现异步文件上传示例
- Asp.Net的FileUpload类实现上传文件实例