<%@ 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. HDU 1069 Monkey and Banana(LIS最长上升子序列)

    B - LIS Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Descripti ...

  2. centos es2.x安装

    #把下面这个放到es的server路径下,这个是rpm安装改了下. # # init.d / servicectl compatibility (openSUSE) # if [ -f /etc/rc ...

  3. windows下实现uboot的tftp下载功能

    一.原理分析 带有uboot的开发板实际上充当的就是tftp客户端,而PC机扮演的角色就是tftp服务器端,而tftp下载功能实际上就是文件传输.tftp服务器可以建立在虚拟机linux下,也可以建立 ...

  4. Python 更改cmd中的字色

    没有gui的python程序是在cmd窗口中运行的,黑色背景,灰色的字,确实很复古,不符合现代人的使用习惯-同事在用我写的小工具时,清一色的字色,看起来会没有重点性,因此我就想通过更改cmd中的字色来 ...

  5. Unity问答——NGUI怎么使用按键模拟鼠标点击?

    这篇博客源自我在泰课在线的回答.链接:http://www.taikr.com/group/1/thread/248 问:NGUI怎么模拟用代码模拟控制点击 答: 1. 这个问题问得好.因为在使用按键 ...

  6. 谈谈Parser --王垠

    一直很了解人们对于parser的误解,可是一直都提不起兴趣来阐述对它的观点.然而我觉得是有必要解释一下这个问题的时候了.我感觉得到大部分人对于parser的误解之深,再不澄清一下,恐怕这些谬误就要写进 ...

  7. net programming guid

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

  8. Computational Methods in Bayesian Analysis

    Computational Methods in Bayesian Analysis Computational Methods in Bayesian Analysis  [Markov chain ...

  9. 大用处--PowerShell Management Library for Hyper-V.

    http://pshyperv.codeplex.com/releases 用脚本来收集及集成HYPER-V管理. 越来越似KVM,LIBVIRT啦.

  10. 关于如何在BCB中使用CodeGuard

    作者:深圳虫 来自:深圳虫网本文来自http://www.szbug.com/disparticle.aspID=4 一. 为什么写这篇东西自己在使用BCB5写一些程序时需要检查很多东西,例如内存泄漏 ...