美文网首页
SpringMvc+jquery.form.js实现上传

SpringMvc+jquery.form.js实现上传

作者: Aluha_f289 | 来源:发表于2018-07-17 16:08 被阅读0次
    <form action="upload" method="post" enctype="multipart/form-data">
        <td class="title" width="18%">选择文件:</td>
         <td width="32%"><input type="file" name="Myfile" class="l-text" /></td>
        <input type="submit" value="submit" style="display:none" />
    </form>
            
    <tr>
        <td class="b_button">
        <td><button id="button" style="margin-top:30px"  class="big_button" >上传</button></td>
        </td>
    </tr>
    
    $("#button").click(function(){
        //获取form 表单对象
        var hideForm = $('form');   
          var options = {  
              dataType : "json",  
              beforeSubmit : function() {  
                  alert("正在上传");  
              },  
              success : function(result) {  
                  alert('成功上传!');  
                  window.close();
              },  
              error : function(result) {  
                   
              }  
          };  
          //Submit 提交
          hideForm.ajaxSubmit(options); 
      });
    
    /** 
         * @Method: upload 
         * @Description: 数据上传
         * @return ModelAndView 
         * @throws 
         */ 
        @RequestMapping(value = "/upload",method = RequestMethod.POST)
        @ResponseBody
        public void upload(HttpServletRequest request,HttpServletResponse res,String json
               ) throws Exception {
            MultipartFile file = FileRWUtil.parse2MultFile(request, "Myfile");
            FileUploadBo fileUploadBo = JsonUtil.json2Java(json, FileUploadBo.class);
            //获取当前登入人
            Map<String,String> userMaps = (Map<String,String>) request.getSession().getAttribute("user");
            fileUploadBo.setUpUserNo(userMaps.get("name"));
            //如果文件不为空,写入上传路径
               if(!file.isEmpty()) {
                   //上传文件路径
                   String filename = file.getOriginalFilename();
                   String fileName = fileUploadBizc.getFileName(filename);
                   long fileSize = (file.getSize())/1024;
                   String fileSizeKb = String.valueOf(fileSize);
                  //String fileSizeKb = getTransSize(fileSize);
                   String fileType =fileUploadBizc.getExtensionName(filename);
                   File filepath = new File("/upload",filename);
                   String filePathName = String.valueOf(filepath);
                   //获取绝对路径
                   //String realPath =request.getSession().getServletContext().getRealPath(filePathName);
                   //判断路径是否存在,如果不存在就创建一个
                   if (!filepath.getParentFile().exists()) { 
                       filepath.getParentFile().mkdirs();
                   }
                   //将上传文件保存到一个目标文件当中
                   file.transferTo(new File("/upload" + File.separator + filename));
                   fileUploadBo.setName(fileName);
                   fileUploadBo.setFileType(fileType);
                   fileUploadBo.setFilePath(filePathName);
                   fileUploadBo.setFileSize(fileSizeKb);
                   fileUploadBizc.saveFileUplod(fileUploadBo);
               }
    

    3.配置部分

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!-- 上传文件大小限制,单位为字节-10Mb -->
            <property name="maxUploadSize">
                <value>10485760</value>
            </property>
            <!-- 请求的编码格式 -->
            <property name="defaultEncoding">
                <value>UTF-8</value>
            </property>
        </bean>
    

    4.注意事项
    @RequestParam("Myfile")
    和 <td width="32%"><input type="file" name="Myfile" class="l-text" /></td>

    相关文章

      网友评论

          本文标题:SpringMvc+jquery.form.js实现上传

          本文链接:https://www.haomeiwen.com/subject/chhgpftx.html