首页 > 其他分享 >SpringMVC文件上传

SpringMVC文件上传

时间:2022-11-26 21:22:54浏览次数:42  
标签:文件 fileupload SpringMVC commons io 上传

1  文件上传客户端三要素

  *  表单项type = "file"

  *  表单的提交方式时post

  *  表单的enctype属性时多部分表单形式,即enctype = "multipart/form-data"

2  单文件上传步骤

  1  导入fileupload和io坐标

    

        <dependency>
          <groupId>commons-fileupload</groupId>
          <artifactId>commons-fileupload</artifactId>
          <version>1.3.1</version>
      </dependency>
      
      <dependency>
          <groupId>commons-io</groupId>
          <artifactId>commons-io</artifactId>
          <version>2.4</version>
      </dependency>

 

  2  配置文件上传解析器

    

<!-- 配置文件上传解析器 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"></property>        
        <property name="maxUploadSize" value="500000"></property>
    
    </bean>

 

  3  编写文件上传代码

    

@RequestMapping("/quick22")
    @ResponseBody  //告知SpringMVC框架,不进行视图跳转,直接进行数据响应
    //文件上传
    public void save22(String username ,MultipartFile uploadFile) throws Exception {
        System.out.println(username);
        //获得上传文件的名称
        String originalFilename = uploadFile.getOriginalFilename();
        uploadFile.transferTo(new File("D:\\upload\\"+originalFilename));
    }

 

标签:文件,fileupload,SpringMVC,commons,io,上传
From: https://www.cnblogs.com/zlyyds/p/16928034.html

相关文章