首页 > 代码库 > java sprinvmvc 文件上传

java sprinvmvc 文件上传

1、配置文件 /wbh-adapter-h5/src/main/resources/dispatcher-servlet.xml

<!-- 文件上传需要配置该句 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

 

2、jquery.form 提交表单;

$(function(){      //异步提交表单      $("#ajaxSubmit").on("click",function(){          console.log($(this));          $("#uploadForm").ajaxSubmit({              type:‘post‘,              url:‘${context}/mobile/uploadimgs‘,              success:function(data){                  console.log(data);                  var data= http://www.mamicode.com/eval(data);"1"){    				 $.alert(data.errorMessage);    			}else{    				$.showPreloader(‘编辑成功...‘);    			      setTimeout(function () {    			        $.hidePreloader();    			        window.location.href=http://www.mamicode.com/‘${context}/weish/index‘;>

  

后台:

	/**	 * 上传用户图片,通用	 * 	 * @param res 传过来的资源对象	 * @return 目标对象	 * @throws Exception 	 */	@RequestMapping(value = "http://www.mamicode.com/mobile/uploadimgs", method = RequestMethod.POST)	public @ResponseBody JsonResult uploadimgs(			HttpServletRequest request,			HttpServletResponse response,			com.wbh.adapter.bean.out.user.UserImg userImg,			@RequestParam(value = "http://www.mamicode.com/fieldName", required = false) MultipartFile file) {		JsonResult result = new JsonResult();		try {			result = usersService.uploadImgs(userImg, file);		} catch (MalformedURLException e) {			e.printStackTrace();		}		return result;	}

  

 

java sprinvmvc 文件上传