zoukankan      html  css  js  c++  java
  • SpringMvc 文件上传

    commons-fileupload.jar、commons-io.jar

    springmvc.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:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
            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-4.3.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
    
        <!-- 扫描 有注解的包 -->
        <context:component-scan base-package="handler"></context:component-scan>
    
        <!--配置视图解析器(InternalResourceViewResolver)  -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/views/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    
        <!--view-name会被视图解析器 加上前缀、后缀  -->
        <mvc:view-controller path="handler/testMvcViewController" view-name="success"/>
    
        <!-- 该注解 会让 springmvc: 接收一个请求,并且该请求 没有对应的@requestmapping时,将该请求 交给服务器默认的servlet去处理(直接访问)  -->
        <mvc:default-servlet-handler></mvc:default-servlet-handler>
    
    
        <!-- 1将 自定义转换器 纳入SpringIOC容器 -->
        <bean id="myConverter" class="converter.MyConverter"></bean>
    
        <!-- 2将myConverter再纳入 SpringMVC提供的转换器Bean
        <bean id="conversionService"  class="org.springframework.context.support.ConversionServiceFactoryBean">
            <property name="converters">
                <set>
                    <ref bean="myConverter"/>
                </set>
            </property>
        </bean>
         -->
    
        <!-- 3将conversionService注册到annotation-driven中 -->
        <!--此配置是SpringMVC的基础配置,很功能都需要通过该注解来协调  -->
        <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
    
        <!-- 配置 数据格式化 注解 所依赖的bean FormattingConversionServiceFactoryBean:既可以实现格式化、又可以实现类型转换 -->
        <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
            <property name="converters">
                <set>
                    <ref bean="myConverter"/>
                </set>
            </property>
        </bean>
    
        <!-- 配置CommonsMultipartResolver,用于实现文件上传 将其加入SpringIOC容器.
            springIoc容器在初始化时,会自动寻找一个Id="multipartResolver"的bean,并将其加入Ioc容器 -->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="defaultEncoding" value="UTF-8"></property>
            <!-- 上传单个文件的最大值,单位Byte;如果-1,表示无限制 -->
            <property name="maxUploadSize"  value="104857600"></property>
        </bean>
    
    </beans>
    testUpload
        @RequestMapping("testUpload")
        public String testUpload(MultipartFile file, String desc) throws IOException {
            System.out.println("文件描述信息:" + desc);
            InputStream inputStream = file.getInputStream();
            String fileName = file.getOriginalFilename();
            String path = "E:\0JAVA\JavaFile\" + fileName;
            OutputStream outputStream = new FileOutputStream(path);
            int len;
            byte[] bytes = new byte[1024];
            while ((len = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
            inputStream.close();
            outputStream.close();
            System.out.println(555555);
            return "success";
        }

     index.jsp

    <form action="handler/testUpload" method="post" enctype="multipart/form-data">
        <input type="file" name="file"> <br>
        <input type="text" name="desc" value="描述"> <br>
        <input type="submit" value="上传">    <br>
    </form>
  • 相关阅读:
    SpringCloud的Archaius
    一些技术博文,有时间整理一下!
    spring-oauth-server实践:授权方式四:client_credentials 模式的refresh_token?
    spring-oauth-server实践:授权方式四:client_credentials 模式下有效期内重复申请 access_token ?
    spring-oauth-server实践:使用授权方式四:client_credentials 模式的客户端和服务端交互
    api-gateway实践(03)新服务网关
    spring-oauth-server实践:使用授权方式四:client_credentials 模式下access_token做业务!!!
    spring-oauth-server实践:授权方式四:client_credentials 模式下access_token的产生
    spring-oauth-server实践:授权方式三:PASSWORD模式下 authorities:ROLE_{user.privillege}, ROLE_USER
    spring-oauth-server实践:授权方式1、2、3和授权方式4的token对象.authorities产生方式比较
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/11878832.html
Copyright © 2011-2022 走看看