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

    1.创建上传页面

    <form action="first.do" method="post"
            enctype="multipart/form-data">
            <input type="file" name="uploadFile" value="选择文件" /> <input type="submit"
                value="提交" />
        </form>

    2.配置控制器

    @Controller
    public class MyController {
    
        // 处理器方法
        @RequestMapping(value = "/first.do")
        public String doFirst(HttpSession session, MultipartFile uploadFile)
                throws Exception {
    
            // 1.获取文件名称
            String originalFilename = uploadFile.getOriginalFilename();
            // 6.必须选择上传文件
            if (uploadFile.getSize() > 0) {
                // 5.限制文件类型
                if (originalFilename.endsWith("jpg")
                        || originalFilename.endsWith("png")
                        || originalFilename.endsWith("txt")) {
                    // 2.获取保存的前路径
                    String servletContext = session.getServletContext()
                            .getRealPath("upload");
                    System.out.println(servletContext);
                    // 3.拼接路径
                    File realPath = new File(servletContext, originalFilename);
                    // 4.保存
                    uploadFile.transferTo(realPath);
                    return "/two.jsp";
                }
            }
            return "/error.jsp";
        }
    }

    3.配置applicationContext.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:tx="http://www.springframework.org/schema/tx"
        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/beans 
            http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/mvc 
           http://www.springframework.org/schema/mvc/spring-mvc.xsd
            ">
        <!-- 扫描包中注解标注的类 -->
        <context:component-scan base-package="cn.cnsdhzzl.controller" />
    
        <bean id="multipartResolver"
            class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!-- 设置编码 -->
            <property name="defaultEncoding" value="UTF-8" />
            <!-- 设置上传文件总大小 -->
            <property name="maxUploadSize" value="5000" />
            <!-- 设置单个上传文件大小 -->
            <property name="maxUploadSizePerFile" value="1000" />
        </bean>
    
        <mvc:annotation-driven />
    
    </beans> 
  • 相关阅读:
    ACM: SCU 4440 Rectangle
    ACM: NBUT 1646 Internet of Lights and Switches
    ACM: Long Live the Queen
    ACM: Racing Gems
    C++ 11 笔记 (一) : lambda
    cocos2d-x笔记2: 编译到安卓的步骤与注意事项
    C++笔记1: 单例模式。(一个简单的设计模式在C++中复杂出翔。。)
    Java笔记2 : 泛型的体现,及其上限、下限、通配符
    我终于忍不住喷一下某些书了,关于Java传引用的XX言论
    Java笔记1 : 在生产者消费者模式中,线程通信与共享数据,死锁问题与解决办法
  • 原文地址:https://www.cnblogs.com/cnsdhzzl/p/6104398.html
Copyright © 2011-2022 走看看