我的定位:中高级人才(框架熟练、Java语法精通、数据库会用、Linux会用、中间件会用、有大数据经验!)
开始,我以为自己什么都知道。后来发现,其实我什么都不知道。

SpringMVC11文件上传

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

 

posted @ 2017-06-30 09:43  想太多先森  阅读(103)  评论(0编辑  收藏  举报