springmvc 上传文件到后台

spring版本:5.0.2
spring-mvc.xml配置:

   <mvc:annotation-driven/>

后加上

 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

web.xml配置

<servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>   
    <!--  这里开始是文件上传的配置-->
    <load-on-startup>1</load-on-startup>
    <multipart-config>    
      <location>d:/share/</location>
      <max-file-size>4097152</max-file-size>     
      <max-request-size>8194304</max-request-size>
    </multipart-config>
    <!--  到这里结束-->
  </servlet>

HTML:

  <input type="file"  id="file" />

jq:

 var formData = new FormData();
 var files = $("#file")[0].files[0];
 formData.append("file", files);
  $.ajax({
                    url: "你的url",
                    type: "post",
                    data: formData,
                    cache: false,
                    processData: false,
                    contentType: false,
                    success: function (data) {
                        成功后执行的方法
                    }
                }
            );

formData内还可以放其他变量如

formData.append("user_id", 1);
formData.append("names", "张三");
formData.append("telephone", "12345678901");
formData.append("email", "123@123.com");

后台接收

@RequestMapping(value = "/client/updateUser", method = RequestMethod.POST)
@ResponseBody
public ServiceResponse updateUser(@RequestParam(value = "file",required = false)MultipartFile file/*如果加了其他参数可以在这后面加一个对象接收*/) throws IOException {

}
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值