知识点:

  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. 进程控制之exec函数

    用fork函数创建子进程后,子进程往往要调用一种exec函数以执行另一个程序.当进程调用一种exec函数时,该进程执行的程序完全替换为新程序,而新程序则从其main函数开始执行.因为调用exec并不创 ...

  2. GAC(Global Assembly Cache)注册/卸载 dll

    当发现有多个解决方案引用一个dll时,为了不重复引用所以将.net的一个dll注册到GAC中去. gacutil.exe. 记得使用管理员权限打开 开始菜单-Microsoft Visual Stud ...

  3. LeetCode45 Jump Game II

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  4. java中的链式编程

    听到链式编程听陌生的,但是写出来就感觉其实很熟悉 package test; public class Test { String name; String phone; String mail; S ...

  5. HTML与Servlet

    1.什么是servlet Servlet 是在服务器上运行的小程序.一个 Servlet 就是 Java 编程语言中的一个类,它被用来扩展服务器的性能,服务器上驻留着可以通过“请求-响应”编程模型来访 ...

  6. Triangular Sums

    描述 The nth Triangular number, T(n) = 1 + … + n, is the sum of the first n integers. It is the number ...

  7. 对C++/CLR的一些评价

    我以后要是再用这东西,我自砍双手

  8. JavaScript实现搜索联想功能

    -.虽然Jquery已经有了一个完整的包 实现前端搜索联想功能,但是出于学习还是想了解一下实现此功能的原理性 回想起来 实现此功能很简单,1.前端输入字符串 文本改变 异步请求服务器 将返回的资料显示 ...

  9. 使用block来解决实现switch解决字符串

    NSString *lookup=@"Hearts"; typedef void  (^CaseBlock)(); NSDictionary *diction=@{@"D ...

  10. uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟

    挺水的模拟题,刚开始题目看错了,poj竟然过了...无奈.uva果断wa了 搞清题目意思后改了一下,过了uva. 题目要求模拟木块移动: 有n(0<n<25)快block,有5种操作: m ...