简体   繁体   English

Java jersey servlet到达过滤器,但随后停止

[英]java jersey servlet hits filter but then stops

I have a jersey servlet that is not connecting. 我有一个未连接的jersey servlet。 As I trace with debugger, I can see that the servlet's filter (which processes the request header) gets called but after that I lose the trace and the servlet itself (uploadFolder) never gets hit. 当我使用调试器进行跟踪时,可以看到调用了Servlet的过滤器(处理请求标头),但是此后我丢失了跟踪,并且Servlet本身(uploadFolder)从未受到攻击。

HTML 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 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 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) + "}";
}   

I can trace through the filter and it seems to be working correctly. 我可以跟踪筛选器,它似乎可以正常工作。 Does not exit prematurely or abort the request. 不会过早退出或中止请求。 So where does it go after the filter? 那么它在过滤器之后又去了哪里呢? The alert(request.responseText) box shows "This page says: " with nothing in it. alert(request.responseText)框显示“此页面显示:”,其中没有任何内容。

I also tried sending the request using jquery.ajax like this: 我还尝试过使用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);   
    }
});

Interestingly, the error alert popped up while the debugger was still in the filter. 有趣的是,当调试器仍在过滤器中时,错误警报会弹出。 I wasn't expecting that. 我没想到这一点。 Output: status=0, statusText="". 输出:status = 0,statusText =“”。

Any the Any suggestions? 有任何建议吗?

The clue to the fix was status=0. 修复的线索是状态= 0。 Googling on that I found there is an issue with the onSubmit. 谷歌搜索我发现onSubmit存在问题。 Consequently I changed it to a button click instead. 因此,我改为将其更改为按钮单击。 Here is the new 这是新的

HTML 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>

That fixed the status error but there was another error after that (I forget). 修复了状态错误,但是之后又出现了另一个错误(我忘了)。 The fix was to also change the Javascript. 解决方法是还更改Javascript。

JS 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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM