如何从java中的请求标头获取内容类型?

时间:2016-11-29 21:36:11

标签: javascript java

这个问题已经毁了我生命中的三天。这就是我正在做的事情:

boolean mimef = false, notvideo=false;
Enumeration headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
    String key = (String) headerNames.nextElement();
    String value = request.getHeader(key);

    System.out.println("K: "+key+" V: "+value);

    if(key.equals("content-type")){
        mimef=true;
        System.out.println("V: "+value);
        if(!value.contains("video/")){
            notvideo=true;
        }
    }
}

if(notvideo){
    System.out.println("file is not video!");
    response.setStatus(400);
    out.print("Selected file is not video, Please try again. ( '_' )");
    out.close();
    return;
}

if(!mimef){
    response.setStatus(400);
    out.print("Couldn't identify file type, Please try again ( '_' )");
    out.close();
    System.out.println("error here");
    return;
}

所以我基本上使用它来从帖子标题中获取上传文件的content-type。它适用于.exevideo types但不适用于pdf文件,老实说,我有两个.exe文件,其中一个工作,另一个不工作,并打印两个标题内容-type它都说application,到目前为止我能找到的是,它在我的服务器控制台中没有工作的文件打印file is not video!但在我的客户端我从未收到错误i&# 39;我想是。

前端:

var xhr=new XMLHttpRequest();
var started_at = new Date();
xhr.onreadystatechange = function() {
    if (xhr.readyState == XMLHttpRequest.DONE) {
        var sres=xhr.responseText;
        alert(sres);
    }
}
xhr.open('post','../uploadtostack',true);
xhr.setRequestHeader('X-File-Name',file.name);
xhr.setRequestHeader('x-File-Size',file.size);
xhr.setRequestHeader('X-File-Type',file.type);
xhr.send(file);

我也尝试过:

String contenttype = request.getContentType();
System.out.println("CT: "+contenttype);
if(!contenttype.contains("video/")){
    out.print("File is not a video, try again!");
    return;
}

我的控制台输出是:

CT: application/pdf
CT: application/pdf
CT: application/pdf
CT: application/pdf
CT: application/pdf
CT: application/pdf
CT: application/pdf

我认为该帖子已发送7次。

0 个答案:

没有答案