springMVC(十)——使用Ajax文件上传和参数上传

在springmvc中如何使用ajax即上传文件又上传参数呢?

本文参考于

https://blog.csdn.net/chengziaa123/article/details/81041371

这里在JS中使用到了FromData

springmvc.xml配置

 

<!-- 配置文件上传解析器 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--设置文件上传的大小  字节  -->
        <property name="maxUploadSize" value="20971520"></property>
        <property name="defaultEncoding" value="UTF-8"></property>
        
    </bean>

 

前台代码

<script type="text/javascript">
    function saveDraft(){
      //创建一个forData 
         var formData = new FormData($('#projectinfo')[0]);
        /* alert($("#province7").val()+$("#city7").val());
        var formData = new FormData($("#projectinfo"));
        formData.append("ps_file",$("#ps_file")[0].files[0]);
        formData.append("ps_name",$("#ps_name").val());
        
        formData.append("ps_goal",$("#ps_goal").val());
        formData.append("ps_money",$("#ps_money").val());
        formData.append("ps_days",$("#ps_days").val());
        fromData.append("province",$("#province7").val());        
        fromData.append("city",$("#city7").val()); */
        $.ajax({
            type : 'POST',
            url : '/Crowd-funding/projects/upload',
            data : formData,
            dataType:"json",
            async: false,  
            cache : false,
            processData : false,
            contentType : false,
        }).success(function(data) {            
            alert("上传成功");
        }).error(function() {
            alert("上传失败");

        });
    }
</script>

 

 

 

 

后台代码

@RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public boolean upload(@RequestParam("ps_file") MultipartFile ps_file, HttpServletRequest request,
            String ps_name ,
            String ps_goal ,
            String ps_money,
            String ps_days,
            String province,
            String city) {
        System.out.println(province+city);
        
        Map<String, String> modelMap = new HashMap<>();
        boolean flay=true;
        if (!ps_file.isEmpty()) {
            //获取文件上传真实保存路径
            String storePath = request.getServletContext().getRealPath("/images");
            Random r = new Random();
            String fileName = ps_file.getOriginalFilename();
            String[] split = fileName.split(".jpg");
            fileName = split[0] + r.nextInt(1000);
            fileName = fileName + ".jpg";
            System.out.println(fileName);
            File filePath = new File(storePath, fileName);
            System.out.println(filePath.getParentFile());
            if (!filePath.getParentFile().exists()) {
                filePath.getParentFile().mkdirs();// 如果目录不存在,则创建目录
            }
            try {
                ps_file.transferTo(new File(storePath + File.separator + fileName));// 把文件写入目标文件地址
            } catch (Exception e) {
                e.printStackTrace();
                modelMap.put("back", "error");
                return flay=false;                
            }
            modelMap.put("back", "success"); 
        } else {
            modelMap.put("back", "error");
        }
        return flay;
 
    }

 

posted @ 2019-09-21 09:31  小成~  阅读(2101)  评论(0编辑  收藏  举报