java 球衣 servlet 命中过滤器但随后停止

java jersey servlet hits filter but then stops

我有一个未连接的球衣 servlet。当我使用调试器进行跟踪时,我可以看到 servlet 的过滤器(处理请求 header)被调用,但之后我失去了跟踪并且 servlet 本身(uploadFolder)永远不会被命中。

HTML

<form id="uploadForm" method="post" enctype="multipart/form-data"> 
<p>Upload project folder: 
<input type="file" id="files" name="files" multiple webkitdirectory /></p>

JS

$('#uploadForm').submit(function(e) {
    var fd = new FormData($('#uploadForm')[0]);
    var url = [my local machine]/GProject/uploadFolder;
    var cookieVal = RemoteJSvar.userId; 
    var auth = ['Super_Complex_Auth', cookieVal];

    var request = new XMLHttpRequest();
    request.open("POST", url);
    request.setRequestHeader('Authorization',auth);
    request.onreadystatechange = function() {
        if(request.readyState == 4) {
            alert(request.responseText);
        }
    }       
request.send(fd);
});

Java

@POST @Path("uploadFolder")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String uploadFolder(FormDataMultiPart multiPart)          
{
    boolean retVal = projects.uploadFolder(multiPart);  //**This never gets hit**
    return "{\"retVal\" : " + String.valueOf(retVal) + "}";
}   

我可以跟踪过滤器,它似乎工作正常。不会过早退出或中止请求。那么过滤后去哪里呢?警报 (request.responseText) 框显示 "This page says: " 里面什么也没有。

我也尝试使用 jquery.ajax 发送请求,如下所示:

return jQuery.ajax({
    type: 'POST',
    url: url,                           
    data: fd,
    enctype: 'multipart/form-data',
    processData: false,
    contentType: 'multipart/form-data',
    headers : {
        Authorization: auth
    },  
    error: function(xhr){
        alert("An error occured: status=" +xhr.status + ", statusText=" +xhr.statusText);   
    },
    success: function(data) {
        alert("success!: "+data);   
    }
});

有趣的是,当调试器仍在过滤器中时弹出错误警报。我没想到。输出:status=0, statusText="".

有什么建议吗?

修复的线索是 status=0。谷歌搜索我发现 onSubmit 有问题。因此,我将其更改为单击按钮。这是新的

HTML

             <form id="uploadForm" method="post" enctype="multipart/form-data">
                 <label>Choose folder to upload: </label>
                 <input type="file" id="files" name="files" multiple webkitdirectory class="k-button"/>
                 <p style="margin:0;margin-top:3px"></p>
                <input type="button" id="uploadButton" value="Upload Folder" class="k-button" disabled/>
            </form>

这修复了状态错误,但之后还有另一个错误(我忘记了)。解决方法是同时更改 Javascript.

JS

$('#uploadButton').click(function() {
    var file = document.getElementById('files');
    for (var i = 0, f; f = file.files[i]; ++i) {
        console.log(f.webkitRelativePath);
    }
    var fd = new FormData($('#uploadForm')[0]);
    var url = [my local machine]/GProject/uploadFolder;
    var cookieVal = RemoteJSvar.userId; 
    var auth = ['Super_Complex_Auth', cookieVal];

    var request = new XMLHttpRequest();
    request.open("POST", url);
    request.setRequestHeader('Authorization',auth);
    request.onreadystatechange = function() {//Call a function when the state changes.
        if(request.readyState == 4) {
            console.log("responseText=" +request.responseText +", status="+request.status +", statusText="+request.statusText);
        }
    }       
    request.send(fd);
});