无法通过Ajax JAVA [复制]传MultipartFile(Can't Pas

2019-10-29 14:33发布

这个问题已经在这里有一个答案:

  • 什么是一个NullPointerException,以及如何解决这个问题? 12个回答

我想从我的jsp发送文件通过AJAX,即时得到例外控制器:空的错误,下面是我的代码:

控制器:

@RequestMapping(value = "/schedulebatch",method = 
RequestMethod.POST,params="insertData")
public @ResponseBody String addBatch(@RequestParam(
@RequestParam(value="upfile",required=false) MultipartFile upfile) throws 
Exception {  if(!upfile.isEmpty()){
                    System.out.println("test1");}

视图:

  $("#submit-btn").click(function(){ var upfile = document.getElementById('upfile').enctypeVIEW; alert('test'); if(batchname==null || fromdate == null || partners == null || interval == null){ $('#insertmodalalert').empty(); $('#insertmodalalert').append('<div class="alert alert-info"><strong >NOTICE |</strong> Please fill out the required form. </div>'); $('#alertMod').modal(); $('#okbtn').click(function(){ window.location.reload(true); }); } else{ $.ajax({ type: "POST", url: "schedulebatch?insertData", data: {"upfile" : upfile}, success: function(response){ // alert('test'); $('#insertmodalalert').empty(); $('#insertmodalalert').append('<div class="alert alert- info"><strong >NOTICE |</strong> '+response+' </div>'); $('#alertMod').modal(); $('#okbtn').click(function(){ $('#alertMod').modal('hide'); window.location.reload(true); }); }, error: function(xhr, status, error) { var err = eval("(" + xhr.responseText + ")"); // alert('test'); // Display the specific error raised by the server (eg not a // valid value for Int32, or attempted to divide by zero). $('#insertmodalalert').append('<div class="alert alert-danger"><strong >NOTICE |</strong> '+err.Message+'</div>'); $('#activateMod').modal(); $('#okbtn').click(function(){ $('#alertMod').modal('hide'); window.location.reload(true); }); } }); //alert("Test"); } 

HTML:

  File to upload: <input class="form-control" type="file" name="upfile" accept=".csv" id="upfile"> <button type="submit" class="btn btn-success" id="submit- btn">Submit</button> 

我缩小了代码,让我的错误,在此先感谢的唯一的事情。 它成功地得到多部分文件,但林不知道为什么它给出了一个空的异常错误

Answer 1:

当我上载春引导我RestController文件我用像下面,一切都很好:

@PostMapping
public ResponseEntity<User> post(UserCreateRequest request, @RequestPart("file") MultipartFile file) throws IOException {
    ftpService.uploadPhoto(file.getOriginalFilename(), file.getInputStream());

     return ResponseEntity.ok(userService.create(request));
 }

所以,可能是你可以尝试改变你的代码如下:

@RequestMapping(value = "/schedulebatch",method = 
RequestMethod.POST,params="insertData")
public @ResponseBody String addBatch(@RequestPart("upfile") MultipartFile upfile) throws Exception {  
    if(!upfile.isEmpty()){
        System.out.println("test1");
    }
}

和你的内容类型应该是多部分/ form-data的,所以我说一个例子HTML和AJAX形式要求:

<html>

    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

        <script>
            $(document).ready(function () {

                $('#btnSubmit').click( function(e) {
                    e.preventDefault();

                    var form = $('#my-form')[0];
                    var data = new FormData(form);

                    $.ajax({
                        type: "POST",
                        enctype: 'multipart/form-data',
                        url: "http://localhost:8080/schedulebatch",
                        data: data,
                        processData: false,
                        contentType: false,
                        cache: false,
                        timeout: 600000,
                        success: function (data) {
                            alert("success")
                        },
                        error: function (e) {
                            alert("ERROR : ", e);
                        }
                    });
                });
            });

        </script>

    </head>

    <body>

        <form method="POST" enctype="multipart/form-data" id="my-form">
            <input type="file" name="upfile"/><br/><br/>
            <input type="submit" value="Submit" id="btnSubmit"/>

        </form>

    </body>

</html>


文章来源: Can't Pass MultipartFile through Ajax JAVA [duplicate]