zoukankan      html  css  js  c++  java
  • springMVC上传文件简单案例

    html

    <form name="Form2" action="/SpringMVC006/fileUpload2" method="post"  enctype="multipart/form-data">
    <h1>采用multipart提供的file.transfer方法上传文件</h1>
    <input type="file" name="file">
    <input type="submit" value="upload"/>
    </form>

    配置springmvc.xml:

    1
    2
    3
    4
    5
    6
    <!-- 多部分文件上传 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
         <property name="maxUploadSize" value="104857600" />
         <property name="maxInMemorySize" value="4096" />
         <property name="defaultEncoding" value="UTF-8"></property>
    </bean>

    使用方法:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    /*
         * 采用file.Transto 来保存上传的文件
         */
        @RequestMapping("fileUpload2")
        public String  fileUpload2(@RequestParam("file") CommonsMultipartFile file) throws IOException {
             long  startTime=System.currentTimeMillis();
           
            String path="E:/"+new Date().getTime()+file.getOriginalFilename();
             
            File newFile=new File(path);
            //通过CommonsMultipartFile的方法直接写文件(注意这个时候)
            file.transferTo(newFile);
           
            return "/success"
        }
  • 相关阅读:
    深拷贝与浅拷贝
    图片旋转插件
    promise 小抄
    github fork项目更改后与原作者同步更新
    eslint 的配置
    css规范
    Object类
    BigIntager
    System类
    Math类和Random类
  • 原文地址:https://www.cnblogs.com/lemon-flm/p/8031807.html
Copyright © 2011-2022 走看看