知识点:

  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. C++基础之预处理命令

    目录 概述——对预处理命令的理解 预处理命令的处理时间 C++中主要的预处理命令 宏定义 “文件包含”处理 选择编译 概述 使用C++的预处理命令可以提高程序的可维护性,有利于程序员以更加方便和快捷的 ...

  2. oc-25-id类型,

    /** id:万能指针 能够指向任何OC对象. id = NSObject *, id cat = [Cat new]; [cat jump]; NSObject:是所有类的父类,基类.可以指向任何O ...

  3. GDB---Oracle Function Call List

    http://yong321.freeshell.org/Oracle Function Call List 1. Oracle function call list If you want to s ...

  4. ext2文件系统

    2.1. 总体存储布局 我们知道,一个磁盘可以划分成多个分区,每个分区必须先用格式化工具(例如某种mkfs命令)格式化成某种格式的文件系统,然后才能存储文件,格式化的过程会在磁盘上写一些管理存储布局的 ...

  5. 精品教程--IOS零基础开发环境搭建

    下载源码 技术要点: 1. 启动XCODE开始开发 2. IOS项目文件结构分析 3. 添加视图label组件 4. 程序的入口以及启动流程 5. 源码详细的中文注释 ...... 详细介绍: 1. ...

  6. No saved view state could be found for the view identifier

    解决方法: javax.faces.application.ViewExpiredException:No saved view state could be found for the view i ...

  7. iOS制作gif动态图

    NSMutableArray * array = [NSMutableArray array]; for (int i = 1; i < 6; i++) { NSString * string ...

  8. linux_jvm_jmap_dump内存分析

    jmap命令   jmap命令 jmap命令可以获得运行中的jvm的堆的快照,从而可以离线分析堆,以检查内存泄漏,检查一些严重影响性能的大对象的创建,检查系统中什么对象最多,各种对象所占内存的大小等等 ...

  9. 剑指Offer01 杨氏数组寻值

    /************************************************************************* > File Name: 001.c > ...

  10. poj 1191 矩形块的划分

    思路:黑书的例题 #include<iostream> #include<cstring> #include<algorithm> #include<cmat ...