为什么Apache Commons File Upload卡在getItemIterator上?

时间:2019-03-11 02:11:21

标签: angular tomcat8 apache-commons-fileupload

我正在尝试接收对Tomcat 8.5.38和Java 11.0.2服务器的multipart / form-data POST请求。我可以很好地收到请求,并且用println调试了显示标题和内容 正确的方式:

host: localhost:8443
connection: keep-alive
content-length: 305
accept: application/json, text/plain, */*
origin: https://localhost:8443
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36
dnt: 1
content-type: multipart/form-data; boundary=----WebKitFormBoundaryC8B7mlMCS5oRcK7O
referer: https://localhost:8443/myrestfulthing
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9
cookie: OPUSSESSIONID=850E4969DA073026ABCFB954C1EBB33C; cfid=dc2d9c2d-9cc0-4fc7-bd12-c79800278c66; cftoken=0
------WebKitFormBoundaryC8B7mlMCS5oRcK7O
Content-Disposition: form-data; name="name"

foobar
------WebKitFormBoundaryC8B7mlMCS5oRcK7O
Content-Disposition: form-data; name="data"; filename="file.pdf"
Content-Type: application/octet-stream

<<binary data...>>
------WebKitFormBoundaryC8B7mlMCS5oRcK7O--

(运行下面的问题代码时,我禁用了上面的调试日志记录)。
我已将面包屑放入Apache的示例代码中,以查找失败之处:

try {            
    ServletFileUpload upload = new ServletFileUpload();
    System.out.println("A");
    FileItemIterator iter = upload.getItemIterator(request);
    System.out.println("B");

    while (iter.hasNext()) {
        iter.next();
    }
}
catch (Exception e) {
    System.out.println(e);
}

这会将A发送到控制台,并且永远不会到达B,但是它也不会引发任何异常。它仅在upload.getItemIterator(request)之后挂起,并且在浏览器中返回500错误。最终的问题是:为什么它会在这里阻塞?

客户端代码是一个简单的Angular调用:

let file = this.fileInput.nativeElement.files[0];
let formData = new FormData();
formData.set("name", this.uploadForm.name);
formData.set("data", file);
this.http.post("/foo/secure/pad", formData).subscribe(...

1 个答案:

答案 0 :(得分:0)

基本上是How to solve this java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream?的副本。

我的servlet没有抛出异常,而是抛出了错误。由于我没有明确捕获这些内容,因此没有看到NoClassDefFoundError。将Apache Commons IO的jar放入我的类路径中即可解决。