通过spring mvc 来获取html页面既有传统的表单参数,又有文件上传的组件的功能
html这段通过html5的 form_data 方法进行封装
- html 代码
使用form_data的方法,把form表单中要传给服务器的变量封装成一个json对象,并且命名为 properties 。服务器端的spring mvc 可以使用<!DOCTYPE html> <script src="../js/lib/jquery-3.1.1/jquery-3.1.1.min.js"></script> <body> <form> <h1>File API Demo</h1> <label for="img_input"></label> <input id="file1" type="file" /> <input id="file2" type="file" /> <input type="button" onClick="javascript:fun_upload();" value="upload"> </input> </form> <script> function fun_upload() { console.debug("file upload"); // 创建 var form_data = new FormData(); // 表单信息 form_data.append('properties', new Blob([JSON.stringify({ "name": "root用户名", "password": "root密码" })], { type: "application/json" })); console.debug($("#file1")); // 获取文件 form_data.append("files", $("#file1")[0].files[0]); form_data.append("files", $("#file2")[0].files[0]); console.debug($("#file1")[0].files[0]); $.ajax({ type: "POST", url: "/whale/ajax/upload", dataType: "json", processData: false, contentType: false, data: form_data, success: function(data) { console.debug(data); }, error: function(error) { console.debug("出错!"); console.debug(error); }, complete: function() { console.debug("完成"); } }); } </script> </body>
@RequestPart("properties") @Valid PropertiesBean
- java bean代码
<p class="p1"><span class="s1">package</span> com.ninelephas.whale.controller.test;</p><p class="p2"> </p><p class="p1"><span class="s1">import</span> org.hibernate.validator.constraints.NotEmpty;</p><p class="p2"> </p><p class="p3">/**</p><p class="p3"> * <span class="s2">@ClassName</span>: PropertiesBean</p><p class="p3"> * <span class="s2">@Description</span>: 测试用的Bean</p><p class="p3"> * <span class="s2">@author</span> 徐泽宇</p><p class="p3"> * <span class="s2">@date</span> 2016年10月31日 下午7:05:13</p><p class="p3"> *</p><p class="p3"> */</p><p class="p2"> </p><p class="p4"><span class="s1">public</span><span class="s3"> </span><span class="s1">class</span><span class="s3"> </span>PropertiesBean<span class="s3"> </span><span class="s4">{</span></p><p class="p5"><span class="s3"> </span>@NotEmpty<span class="s4">(</span><span class="s5">message</span><span class="s3"> = </span><span class="s6">"用户名不能为空"</span><span class="s4">)</span></p><p class="p1"> <span class="s1">private</span> <span class="s7">String</span> <span class="s8">name</span> ;</p><p class="p5"><span class="s3"> </span>@NotEmpty<span class="s4">(</span><span class="s5">message</span><span class="s3"> = </span><span class="s6">"密码不能为空"</span><span class="s4">)</span></p><p class="p6"><span class="s3"> </span><span class="s1">private</span><span class="s3"> </span><span class="s7">String</span><span class="s3"> </span>password<span class="s3">;</span></p><p class="p7"> </p><p class="p7"> </p><p class="p1"> <span class="s1">public</span> <span class="s7">String</span> getName<span class="s4">()</span> <span class="s4">{</span></p><p class="p1"> <span class="s1">return</span> <span class="s8">name</span>;</p><p class="p1"> <span class="s4">}</span></p><p class="p1"> <span class="s1">public</span> <span class="s1">void</span> setName<span class="s4">(</span><span class="s7">String</span> <span class="s9">name</span><span class="s4">)</span> <span class="s4">{</span></p><p class="p1"> <span class="s1">this</span>.<span class="s8">name</span> = <span class="s9">name</span>;</p><p class="p1"> <span class="s4">}</span></p><p class="p1"> <span class="s1">public</span> <span class="s7">String</span> getPassword<span class="s4">()</span> <span class="s4">{</span></p><p class="p1"> <span class="s1">return</span> <span class="s8">password</span>;</p><p class="p1"> <span class="s4">}</span></p><p class="p1"> <span class="s1">public</span> <span class="s1">void</span> setPassword<span class="s4">(</span><span class="s7">String</span> <span class="s9">password</span><span class="s4">)</span> <span class="s4">{</span></p><p class="p1"> <span class="s1">this</span>.<span class="s8">password</span> = <span class="s9">password</span>;</p><p class="p1"> <span class="s4">}</span></p><p class="p2"> </p><p class="p8">}</p>
- controller代码
/** * @Title: AjaxUplaodFileController.java * @Package com.ninelephas.whale.controller.test * @Description: TODO * Copyright: Copyright (c) 2016 * Company:九象网络科技(上海)有限公司 * * @author 徐泽宇 * @date 2016年10月31日 下午5:32:02 * @version V1.0.0 */ package com.ninelephas.whale.controller.test; import javax.validation.Valid; import javax.validation.constraints.NotNull; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.hibernate.validator.constraints.NotBlank; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; /** * @ClassName: AjaxUplaodFileController * @Description: 测试ajax上传文件 * @author 徐泽宇 * @date 2016年10月31日 下午5:32:02 * */ @Controller("com.ninelephas.whale.controller.test.AjaxUplaodFileController") @RequestMapping(value = "/ajax") public class AjaxUplaodFileController { /** * Logger for this class */ private static final Logger logger = LogManager.getLogger(AjaxUplaodFileController.class.getName()); /** * uploadFile * * @Auther 徐泽宇 * @Date 2016年10月31日 下午5:35:37 * @Title: uploadFile * @Description: 获取上传文件 * @return */ @RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = {"multipart/form-data"}) @ResponseBody public String uploadFile(@RequestPart("properties") @Valid PropertiesBean properties, @RequestPart("files") @Valid @NotNull @NotBlank MultipartFile[] files) { if (logger.isDebugEnabled()) { logger.debug("uploadFile() - start"); //$NON-NLS-1$ } logger.debug("获取的表单参数是:"); logger.debug(properties.getName()); logger.debug(properties.getPassword()); logger.debug("获取的上传文件是:"); for (int i = 0; i < files.length; i++) { logger.debug(files[i].getOriginalFilename()); } if (logger.isDebugEnabled()) { logger.debug("uploadFile() - end - return value={}", "success"); //$NON-NLS-1$ //$NON-NLS-2$ } return "success成功上传"; } }