知识点:

  1. SpringMvc单文件上传
  2. SpringMvc多文件上传

 

这里我直接演示多文件上传,单文件的上传就不说了,不过代码都是现成的。

效果预览:

 

DEMO图:

 

 

添加文件上传jar包:

Web.xml配置文件:添加spring Servlet

    <servlet>

        <servlet-name>springmvc</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>

            <param-name>contextConfigLocation</param-name>

            <param-value>classpath:spring-mvc.xml</param-value>

        </init-param>

    </servlet>

    <servlet-mapping>

        <servlet-name>springmvc</servlet-name>

        <url-pattern>*.do</url-pattern>

    </servlet-mapping>

Spring-mvc.xml代码:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd">

 

    <!-- 使用注解的包,包括子集 -->

<context:component-scan base-package="com"/>

 

<!-- 视图解析器 -->

    <bean id="viewResolver"

        class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="/WEB-INF/jsp/" />

        <property name="suffix" value=".jsp"></property>

    </bean>

    

    <bean id="multipartResolver"

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">        

        <property name="defaultEncoding" value="UTF-8"/>

     <property name="maxUploadSize" value="10000000"/>

    </bean>

</beans>

FileUploadController

@Controller

public class FileUploadController {

    //单文件上传

    @RequestMapping("/upload")

    public String uploadFile(@RequestParam("file1") MultipartFile file1,HttpServletRequest request)throws Exception{

        String filePath=request.getServletContext().getRealPath("/");

        System.out.println(filePath);

        file1.transferTo(new File(filePath+"upload/"+file1.getOriginalFilename()));//上传到目录下的upload文件夹下。

        return "redirect:success.jsp";

    }

    //多文件上传

    @RequestMapping("/upload2")

    public String uploadFiles(@RequestParam("file") MultipartFile[] files,HttpServletRequest request)throws Exception{

        String filePath=request.getServletContext().getRealPath("/");

        System.out.println(filePath);

        for(MultipartFile file:files){

            file.transferTo(new File(filePath+"upload/"+file.getOriginalFilename()));            

        }

        return "redirect:success.jsp";

    }

}

 

index.jsp:

<body>

<form action="upload2.do" method="post" enctype="multipart/form-data">

    <table>

        <tr>

            <th colspan="2">上传文件</th>

        </tr>

        <tr>

            <td>文件一</td>

            <td>

                <input type="file" name="file"/>

            </td>

        </tr>

        <tr>

            <td>文件二</td>

            <td>

                <input type="file" name="file"/>

            </td>

        </tr>

        <tr>

            <td colspan="2">

                <input type="submit" value="上传文件"/>

            </td>

        </tr>

    </table>

</form>

</body>

 

success.jsp:

<body>

上传成功

</body>

 

测试地址:http://localhost:8080/SpringMvc04/

查看你上传的文件的时候不是在项目下查看upload文件夹里面的是否有文件,而是在部署服务器下查看项目下的upload文件下,如我是查看的tomcat的地址:\Tomcat\webapps\SpringMvc04\upload。

 

好记性不如烂笔头,菜鸟边学边把学到的东西记录下来。

SpringMvc入门五----文件上传的更多相关文章

  1. 使用SpringMVC框架实现文件上传和下载功能

    使用SpringMVC框架实现文件上传和下载功能 (一)单个文件上传 ①配置文件上传解释器 <!—配置文件上传解释器 --> <mvc:annotation-driven>&l ...

  2. (转)SpringMVC学习(九)——SpringMVC中实现文件上传

    http://blog.csdn.net/yerenyuan_pku/article/details/72511975 这一篇博文主要来总结下SpringMVC中实现文件上传的步骤.但这里我只讲单个文 ...

  3. 使用Typescript重构axios(二十五)——文件上传下载进度监控

    0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...

  4. 深入springMVC源码------文件上传源码解析(下篇)

    在上篇<深入springMVC------文件上传源码解析(上篇) >中,介绍了springmvc文件上传相关.那么本篇呢,将进一步介绍springmvc 上传文件的效率问题. 相信大部分 ...

  5. springMVC实现多文件上传

    <h2>上传多个文件 实例</h2> <form action="/workreport/uploadMultiFile.html" method=& ...

  6. SpringMvc MultipartFile 图片文件上传

    spring-servlet.xml <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 --> <bean id="multipar ...

  7. SpringMVC+BUI实现文件上传(附详解,源码下载)

    中午有限时间写这博文,前言就不必多说了,直奔主题吧. BUI是一个前端框架,关于BUI的介绍请看博主的文章那些年用过的一些前端框架. 下面我们开始实例的讲解! 一.效果演示: 上传成功后,会发现本地相 ...

  8. SpringMVC国际化与文件上传

    点击阅读上一章 其实SpringMVC中的页面国际化与上一章的验证国际化基本一致. 1.对页面进行国际化 1)首先我们对Spring配置文件中添加国际化bean配置 <!-- 注册国际化信息,必 ...

  9. 2017/2/12:springMVC的简单文件上传跟拦截器

    1.写文件上传的界面jsp代码如下重点为文件上传标签的类型 2.写登录成功跟失败的界面:成功自己写 3.写springMVC的文件上传的controller的方法 4.最后一步配置spring-ser ...

随机推荐

  1. /bin/bash: line 0: fg: no job control一般解决方法

    測试版本号:CDH5.0,(Hadoop2.3) 在使用windows调用Hadoop yarn平台的时候,一般都会遇到例如以下的错误: 2014-05-28 17:32:19,761 WARN or ...

  2. centos 服务器配置(一) 之端口占用

    1.查找被占用的端口 netstat -tln netstat -tln | grep 8060 netstat -tln 查看端口使用情况,而netstat -tln | grep 8060则是只查 ...

  3. Linux下的线程

    一.线程的优点 与传统进程相比,用线程来实现相同的功能有如下优点: (1)系统资源消耗低. (2)速度快. (3)线程间的数据共享比进程间容易的多. 二.多线程编程简单实例 #include < ...

  4. CentOS安装某个命令的办法如locate

    在Linux使用命令时,有时候因为系统精简的原因,某些命令可能没有安装locate包,例如你要输入的命令是locate 发现没有该命令,修复办法如下: 一.使用如下命令查询缺失命令的包名 #yum l ...

  5. c++ 构造函数,拷贝构造函数,析构函数与赋值操作符

    题目: 为下面的Rectangle类实现构造函数,拷贝构造函数,赋值操作符,析构函数. class Shape { int no; }; class Point { int x; int y; }; ...

  6. 从零开始学JAVA(01)-JAVA开发环境安装

    写在前面: 本人没有JAVA基础(包括语法.开发环境),未使用开发工具开发过程序,如果有不对或误导的地方,欢迎指正. 本系列所有文章使用Eclipse,JDK是Version 7 Update 51, ...

  7. mysql中的longblob类型处理

    longblob 对应的 C#数据类型为 byte[] 1.byte[] 与 string 之间的转换 byte[] bb = Encoding.UTF8.GetBytes(ss); string s ...

  8. 1.5.4 什么是Filter--过滤器

    什么是Filter--过滤器 像分词器(tokenizer)一样,过滤器(filter)消耗输入,产生token流.过滤器同样从org.apache.lucene.analysis.TokenStre ...

  9. DIH中添加不同的数据源

    需求:从mysql数据库中读取一个知识记录,从记录表中的字段值中获取一个文件路径,读取xml文件,xml文件中可能包含多个文档内容.建立索引. xml文件样例: <?xml version=&q ...

  10. 4. Android框架和工具之 android-async-http

    1. android-async-http   简介 主要有以下功能: (1)发送异步http请求,在匿名callback对象中处理response信息: (2)http请求发生在UI(主)线程之外的 ...