springmvc+ajax file upload

Environment: JDK6 or above, here I use JDK8, mysql57, maven project

Framework environment: spring+springmvc+mybaits or spring+springmvc+mybatis plus 

The front-end code is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>ajax文件上传</title> 
<script src="../js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
functionsavePic(){
    alert( " come in " );
     var formData =  new FormData($( " #uploadPic " )[ 0 ]);  
     var ajaxUrl =  " /LMS/user/saveHeaderPic " ;
     // alert(ajaxUrl); 
    // $(' #uploadPic').serialize() cannot serialize binary files, here use formData to upload 
    // Requires browser support: Chrome 7+, Firefox 4+, IE 10+, Opera 12+, Safari 5+. 
    $.ajax({
        type: "POST",
        url: ajaxUrl,
        data: formData,
        async: false,  
        cache: false,  
        contentType: false,  
        processData: false,
        success: function (data) {
            alert( " success " );
        alert(data);
        },
        error: function(data) {
            alert("error:"+data.responseText);

         }
    });
    return false;
}
</script>
</head> 

<body> 
<form id="uploadPic" action="#" enctype="multipart/form-data">
    <input type="file" name="file">
    <a href="#" class="btn green" onclick="savePic();"> 提交 </a>
</form>
</body> 
</html> 

 

Backend Controller

/**
 * System user basic information table
 */
@RestController
@RequestMapping("/user")
public class UserController {
   
    
    
    /**
     * Avatar image upload
     * @throws IOException 
     */
    @RequestMapping(value = "/saveHeaderPic", method = RequestMethod.POST)
    public void saveHeaderPic(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException {

            String resMsg = "";
        try {

            long  startTime=System.currentTimeMillis();

            System.out.println("fileName:"+file.getOriginalFilename());
            String path="E:\\Demo\\workspace\\images\\"+new Date().getTime()+file.getOriginalFilename();
            System.out.println("path:" + path);

            File newFile = new File(path);
             // Write the file directly through the CommonsMultipartFile method 
            file.transferTo(newFile);
             long   endTime= System.currentTimeMillis();
            System.out.println("运行时间:"+String.valueOf(endTime-startTime)+"ms");
            resMsg = "1";
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            resMsg = "0";
        }
              response.getWriter().write(resMsg);

          }
    

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325069794&siteId=291194637