SpringMVC异步上传多文件

来源:互联网 发布:新东方托福知乎 编辑:程序博客网 时间:2024/06/05 20:01

搭建springMVC的环境

1. 使用maven添加jar包

这里添加的spring的jar可能有些是多余的

<!-- spring 依赖 -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>4.3.5.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-web</artifactId>            <version>4.3.5.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>4.3.5.RELEASE</version>        </dependency>        <!--文件上传-->        <dependency>            <groupId>commons-fileupload</groupId>            <artifactId>commons-fileupload</artifactId>            <version>1.3.2</version>        </dependency>        <!--返回json所需jar包-->        <dependency>            <groupId>com.fasterxml.jackson.core</groupId>            <artifactId>jackson-core</artifactId>            <version>2.7.5</version>        </dependency>        <dependency>            <groupId>com.fasterxml.jackson.core</groupId>            <artifactId>jackson-annotations</artifactId>            <version>2.7.5</version>        </dependency>        <dependency>            <groupId>com.fasterxml.jackson.core</groupId>            <artifactId>jackson-databind</artifactId>            <version>2.7.5</version>        </dependency>

也可以在官网下载spring的jar包添加到WEB-INF的lib目录下。这里博主就不详细说明了。

2.添加spring的配置文件

applicationContext.xml

 <beans xmlns="http://www.springframework.org/schema/beans"       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-4.3.xsd">        <!-- 支持文件上传,文件上传必须添加该bean -->        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>    </beans>

springmvc.xml

    <beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"       xmlns:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd         http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd         http://www.springframework.org/schema/context             http://www.springframework.org/schema/context/spring-context-4.3.xsd">            <!-- 组件扫描 -->            <context:component-scan base-package="com.wqh.action"/>            <!-- 使用注解开发 -->            <mvc:annotation-driven/>            <!-- 视图解析器 -->            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">                <property name="prefix" value="/"/>                <property name="suffix" value=".jsp"/>            </bean>    </beans>

在web.xml中添加springmvc的配置

    <!-- 使用通配符*加载spring的配置文件 -->        <context-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:spring/applicationContext.xml,classpath:spring/applicationContext-*.xml</param-value>        </context-param>        <listener>            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>        </listener>        <!-- 定义拦截器,解决post乱码 -->        <filter>            <filter-name>CharacterEncodingFilter</filter-name>            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>            <init-param>                <param-name>encoding</param-name>                <param-value>utf-8</param-value>            </init-param>        </filter>        <filter-mapping>            <filter-name>CharacterEncodingFilter</filter-name>            <url-pattern>/*</url-pattern>        </filter-mapping>        <!-- 前端控制器 -->        <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/springmvc.xml</param-value>            </init-param>        </servlet>        <servlet-mapping>            <servlet-name>springmvc</servlet-name>            <url-pattern>*.action</url-pattern>        </servlet-mapping>

实现文件上传

该方法将返回上传文件的文件名,返回的格式为json。

@Controller    @RequestMapping("/fileController")    public class FileController {        @RequestMapping(value = "/fileUpload", method = RequestMethod.POST)        public @ResponseBody Map<String, String> fileUpload(@RequestParam(name = "file") MultipartFile[] multipartFiles) {            //这里使用数组是实现多文件的上传            Map<String,String> fileNames = new HashMap<String, String>();            if(multipartFiles!=null){                for(MultipartFile multipartFile : multipartFiles){                    String filename = multipartFile.getOriginalFilename();                    fileNames.put("file",filename);                    //文件保存的路径                    String path = "D:/file";                    File file = new File(path);                    if(!file.exists()){                        file.mkdirs();                    }                    try {                        //保存文件                        multipartFile.transferTo(new File(file,filename));                    } catch (IOException e) {                        e.printStackTrace();                    }                }            }            return fileNames;        }    }

上传页面

这里页面也上一篇:使用ajax、servlet实现多文件的上传文章使用的页面一样。使用jQuery的异步上传文件。博主这里就不添加也面的代码了。

原创粉丝点击