<%@ 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. Vim的学习心得

    现在的工作是在unix平台,平时是用UE的ftp功能来写代码的,有时候文件大了,传输就很慢,而且经常不是很稳定.下定决心要学学Vim(现在应该没有人用原始的vi了吧),在经过二周的使用后,发现Vim实 ...

  2. vm拷贝cloudera-scm-agent造成问题

    ------------网络问题---------- ifconfig...没有看到eth0..然后重启网卡又报下面错误. 故障现象: service network restartShutting  ...

  3. 转:.NET 环境中使用RabbitMQ

    原文来自于:http://blog.jobbole.com/83819/ 原文出处: 寒江独钓   欢迎分享原创到伯乐头条 在企业应用系统领域,会面对不同系统之间的通信.集成与整合,尤其当面临异构系统 ...

  4. Unity3d 跑酷游戏 之Character Controller篇

    unity3d  Character Controller @by  广州小龙 做3D跑酷游戏,也慢慢的学习了一些东西,从开发过程中积累了一些小的知识点跟大家分享一下! 1. 这个Revert按钮的意 ...

  5. 网络流(最大流) POJ 1637 Sightseeing tour

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8628   Accepted: 3636 ...

  6. HDU1251 统计难题(Trie)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Subm ...

  7. [Audio processing] 数据集生成 & 性别年龄分类训练 Python

    1.重命名,Python中文路径各种错误,所以需要先将所有文件的路径名全都改成中文.用的是MAC系统,所以WIN下的命令行批处理没法解决,所以用C来完成 // Created by Carl on 1 ...

  8. ACM编程网站

    ACM:ACM国际大学生程序设计竞赛(英文全称:ACM International Collegiate ProgrammingContest(ACM-ICPC或ICPC)是由美国计算机协会(ACM) ...

  9. poj3122

    题目大意:馅饼(看起来像是一个简单点的题目啊,嘎嘎,希望是的吧) 我的生日即将来临按照习惯我将准备馅饼,不是一个馅饼,我有N块馅饼,有各种各样的味道和尺寸,当我的朋友来参加我的聚会平且他们都能得到一块 ...

  10. 两种动态载入修改后的python模块的方法

    方案一:循环导入/删除模块 a.py import sys, time while True: from b import test test() del sys.modules(b) time.sl ...