form 表单提交数据和文件(fromdata的使用方法)

<!--  数据和文件一次性提交  -->
<form class="form_meren" id="mainForm" name="mainForm" action="${ctx}/shahescenicinfo/insertShaheScenicinfo.do" method="post" enctype="multipart/form-data">

       <input class="form-control " id="aname" name="aname" type="text">
       <input type="file" id="otherfiles">
<input type="file" id="otherfiles2">
 
<button type="button" onclick="sbutripform()" >保存</button> </form>
//验证结束后执行保存
        function sbutripform(){
                       //form的id就是mainForm
            var mainstr=formToJsonObject(mainForm);     
                       //from表单对象 转json
             mainstr=JsonToStr(mainstr);
             mainstr=mainstr.replace("%", "%25");
             var manstr = mainstr.toString();
             var formData = new FormData();
              formData.append("file1",document.getElementById("otherfiles").files[0]);
              formData.append("file2",document.getElementById("otherfiles2").files[0]); 
              formData.append("jsonStr",manstr);
             $.ajax({                  
                    url:url,
                    type:'POST',
                    dataType:'json',
                    data:formData,
                    cache: false,//上传文件无需缓存
                   processData: false,//用于对data参数进行序列化处理 这里必须false
                   contentType: false, //必须
                    success:function(xclyid){
                         window.top.customAlertTip("", "保存成功", "warning");
                     },
                     error:function (error){
                           window.top.customAlertTip("", "保存失败", "warning");
                     }
            });
        
        }

 

@RequestMapping(value="/updateallareainfo.do", method={RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public String updateallareainfo(HttpServletRequest request,
        @RequestParam(value="file1",required=false)MultipartFile file1,
        @RequestParam(value="file2",required=false)MultipartFile file2,
        String jsonStr) {
        //把数据从json 转换为 实体类
        YbsjAreaInfo ybsjAreaInfo = JSON.parseObject(jsonStr,YbsjAreaInfo.class);
      try {
            log.info("当前访问的action方法:updateallareainfo") ;
             // uploads   文件夹位置
             String rootPath = request.getSession().getServletContext().getRealPath(PropertyManager.getProperty("voiceupload"));
              // uploads  文件夹2位置
              String imgPath = request.getSession().getServletContext().getRealPath(PropertyManager.getProperty("filesavepath

// 原始名称
            String newFileName1 = "";
            String newFileName2 = "";

       if (file1!=null) {
                String originalFileName1 = file1.getOriginalFilename();//旧的文件名(用户上传的文件名称)
                //新的文件名 
                newFileName1 = UUIDUtil.getuuid() + originalFileName1.substring(originalFileName1.lastIndexOf("."));
                File newFile = new File(rootPath  + File.separator + newFileName1);
                 // 判断目标文件所在目录是否存在
                 if( !newFile.getParentFile().exists()) {
                     // 如果目标文件所在的目录不存在,则创建父目录
                     newFile.getParentFile().mkdirs();
                 }
                 //存入
                 file1.transferTo(newFile);
                 ybsjAreaInfo.setMapUrl("/"+filesavepath+newFileName1);
            }
            if (file2!=null) {
                String originalFileName2 = file2.getOriginalFilename();
                newFileName2 = UUIDUtil.getuuid() + originalFileName2.substring(originalFileName2.lastIndexOf("."));
                 File newFile2 = new File(imgPath  + File.separator + newFileName2);
                 if( !newFile2.getParentFile().exists()) {
                     // 如果目标文件所在的目录不存在,则创建父目录
                     newFile2.getParentFile().mkdirs();
                 }
                 file2.transferTo(newFile2);
                 ybsjAreaInfo.setAlogo("/"+filesavepath+newFileName2);
            
            }
            
         
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }
        int updateallareainfo = ybsjAreaInfoService.updateallareainfo(ybsjAreaInfo);
        
        String shaheScenicinfoid="{\"id\":\""+updateallareainfo+"\"}";
        return shaheScenicinfoid;
    }

 

 

 

以上是 单个文件 接收的方法

 

 

 

 

 

如果用到一个Input 上传多个文件   

后台用

@RequestParam("files") MultipartFile[] files 

接收就好了


posted @ 2019-07-25 14:58  ExpectoPatronum—S  阅读(9137)  评论(2编辑  收藏  举报