前端使用Ajax传值方式:
<body><input type="file" id="importFile"/><input type="button" id="import-submit" value="提交导入文件"/></body><script type="text/javascript">$('#import-submit').click(function () {var formData = new FormData();var name = $("#importFile").val();formData.append('file', $("#importFile")[0].files[0]);// 此处可传入多个参数formData.append('name', name);$.ajax({url: '/collar/import',type: 'post',async: false,data: formData,processData: false,// 告诉jQuery不要去处理发送的数据contentType: false,// 告诉jQuery不要去设置Content-Type请求头beforeSend: function () {//过程...console.log('正在进行,请稍候')},success: function (res) {if (res == '1') {console.log('导入成功')} else {console.log('导入失败')}},error:function(){console.log('导入失败')}})})</script>
后端:
@RequestMapping("/import")public void export(VivoIMEIUp zipRequest, @RequestParam("file") MultipartFile file,HttpServletRequest request, HttpServletResponse response) {//通过file.getInputStream();处理}