<%@ 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>

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 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 必须使用! 如果不加@RequestParam
* 默认前台的每一个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"> <!-- 配置需要扫描的包 -->
<context:component-scan base-package="cn.bdqn"/>
<!-- 开启注解 -->
<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" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/><!--乱码问题 -->
<property name="maxUploadSize" value="1048576"/><!-- 文件总大小不能超过 1M-->
<!-- <property name="maxUploadSizePerFile" value="1048576"/>单个文件不能超过1m -->
</bean> </beans>

springmvc-servlet.xml文件

SpringMVC11文件上传的更多相关文章

  1. jquery.uploadify文件上传组件

    1.jquery.uploadify简介 在ASP.NET中上传的控件有很多,比如.NET自带的FileUpload,以及SWFUpload,Uploadify等等,尤其后面两个控件的用户体验比较好, ...

  2. 11、Struts2 的文件上传和下载

    文件上传 表单准备 要想使用 HTML 表单上传一个或多个文件 须把 HTML 表单的 enctype 属性设置为 multipart/form-data 须把 HTML 表单的method 属性设置 ...

  3. Java FtpClient 实现文件上传服务

    一.Ubuntu 安装 Vsftpd 服务 1.安装 sudo apt-get install vsftpd 2.添加用户(uftp) sudo useradd -d /home/uftp -s /b ...

  4. 小兔Java教程 - 三分钟学会Java文件上传

    今天群里正好有人问起了Java文件上传的事情,本来这是Java里面的知识点,而我目前最主要的精力还是放在了JS的部分.不过反正也不麻烦,我就专门开一贴来聊聊Java文件上传的基本实现方法吧. 话不多说 ...

  5. ,net core mvc 文件上传

    工作用到文件上传的功能,在这个分享下 ~~ Controller: public class PictureController : Controller { private IHostingEnvi ...

  6. Web开发安全之文件上传安全

    很长一段时间像我这种菜鸡搞一个网站第一时间反应就是找上传,找上传.借此机会把文件上传的安全问题总结一下. 首先看一下DVWA给出的Impossible级别的完整代码: <?php if( iss ...

  7. AutoIt实现Webdriver自动化测试文件上传

    在运用WebDriver进行自动化测试时,由于WebDriver自身的限制,对于上传文件时Windows弹出的文件选择窗口无法控制,通过在网上查找资料锁定使用AutoIt来控制文件上传窗口. Auto ...

  8. Struts的文件上传下载

    Struts的文件上传下载 1.文件上传 Struts2的文件上传也是使用fileUpload的组件,这个组默认是集合在框架里面的.且是使用拦截器:<interceptor name=" ...

  9. .JavaWeb文件上传和FileUpload组件使用

    .JavaWeb文件上传 1.自定义上传 文件上传时的表单设计要符合文件提交的方式: 1.提交方式:post 2.表单中有文件上传的表单项:<input type="file" ...

随机推荐

  1. [原创]用python写了一个简单的markdown编辑器

    以前我常用openoffice记录东西,最喜欢它的当然是在linux上能用了,还有里面的公式输入,前几天才了解markdown这个东东,初步了解发现它正是我需要的东西,可以用它随心所欲地记录些东西,而 ...

  2. java分页查询

    很多数据库自身都提供了分页机制,如SQL Server中提供的top关键字,MySQL数据库中提供的limit关键字,它们都可以设置数据返回的记录数. 通过各种数据库的分页机制实现分页查询,其优点是减 ...

  3. net programming guid

    Beej's Guide to Network Programming Using Internet Sockets Brian "Beej Jorgensen" Hallbeej ...

  4. mysql oracle 删除外键约束

    mysql alter table xxx drop foreign key xxx cascade; oracle alter table drop constraint xxxxx cascade ...

  5. 【java】利用异常机制,往前台写错误信息

    有时候,程序可能会报异常,而这些异常,通常需要提示前台操作人员怎么去处理,才能完成业务. 此时,我们只需要在业务层,自己抛出一个异常,自己捕捉之后,调用下类,即可输出到前台. 1.servlet里面可 ...

  6. web 缓存

    http://robbinfan.com/blog/38/orm-cache-sumup http://my.oschina.net/ITBoy/blog/23683 http://www.kuqin ...

  7. Emag eht htiw Em Pleh

    Emag eht htiw Em Pleh This problem is a reverse case of the problem 2996. You are given the output o ...

  8. AV 地址错误 map 文件 根据地址报错,查 Delphi 代码

    1. 首先需要设置程序生成 map 文件.Project -> Options -> Linker -> Map file , Detailed 2. 计算公式Edit2.Text ...

  9. Yii widget使用

    关于widgets,他们在yii中的关系如下 system.web.widgets 系统自带最基本的widget zii.widgets 是基本扩展 zii.widgets.grid 是基本扩展的重要 ...

  10. Light OJ 1027 - A Dangerous Maze(概率)

    题目大意: 你在一个迷宫里,你面前有n个门,你选择门的概率是一样的,每扇门有一个数字k, 加入这个数字是负数,那么这个门会花费你abs(k)分钟后把你带回原点, 假如这个数字是正数,他可以把你带出迷宫 ...