Jersey ApacheConnector MultiPart文件上传 - 未找到多部分边界

时间:2013-10-02 10:20:33

标签: file-upload httpclient boundary jersey-client

我希望有人可以提供帮助,

我已经为我的spring webmvc app写了一个客户端,我正在使用jersey和ApacheConnector。我需要保持jsessionid,看不出我怎么能用jersey的HttpUrlConnector做到这一点。我还要提一下,在我需要跟踪会话之前,文件上传服务与jersey的HttpUrlConnector一起工作。

我知道spring webmvc文件上传服务有效,因为我的web前端能够上传文件。只是为了完整性,它看起来像这样。

@ResponseBody
private Image onSubmit(@ModelAttribute("uploadedFile") ImageUpload imageUpload) {   
        service.create(imageUpload);
        return new Image();
}

客户端初始化代码如下所示:

clientConfig = new ClientConfig();
connector = new ApacheConnector(clientConfig);
clientConfig.connector(connector);
client = ClientBuilder.newBuilder().withConfig(clientConfig)
                .register(MultiPartFeature.class)
                .register(JacksonJsonProvider.class).build();
target = client.target(url);

我注册了MultiPartFeature和JacksonJsonProvider,因为我想使用jackson 2.x

上传文件的客户端代码如下所示:

File file = new File(fileName);
FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
FormDataBodyPart bodyPart = new FormDataBodyPart("file", file,
                MediaType.APPLICATION_OCTET_STREAM_TYPE);
formDataMultiPart.bodyPart(bodyPart);
response = target
                .path(path)
                .request()
                .post(Entity.entity(formDataMultiPart,
                        formDataMultiPart.getMediaType()), Response.class);

我也尝试了类似的代码:

final FileDataBodyPart filePart = new FileDataBodyPart("file", new File(fileName));
         final MultiPart multipart = new FormDataMultiPart()
            .field("foo", "bar")
            .bodyPart(filePart);
response = target
                .path(path)
                .request()
                .post(Entity.entity(multipart,
                        multipart.getMediaType()), Response.class);

与文档更接近。

当文件上传时,服务器响应

org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:990)
at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:310)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:334)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:115)
at org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:156)
at org.springframework.web.multipart.commons.CommonsMultipartResolver.resolveMultipart(CommonsMultipartResolver.java:139)
at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1047)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:892)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)

这似乎是一个相当普遍的问题,我认为这意味着我没有边界集,所以我在客户端启用了调试。

DEBUG: org.apache.http.headers - >> POST /api/content/image/upload HTTP/1.1
DEBUG: org.apache.http.headers - >> Content-Type: multipart/form-data
DEBUG: org.apache.http.headers - >> User-Agent: Jersey/2.3.1 (Apache HttpClient 4.3)
DEBUG: org.apache.http.headers - >> Transfer-Encoding: chunked
DEBUG: org.apache.http.headers - >> Host: localhost:8010
DEBUG: org.apache.http.headers - >> Connection: Keep-Alive
DEBUG: org.apache.http.headers - >> Cookie: JSESSIONID=2xl9hgb4zq031jpbrn07ylp10
DEBUG: org.apache.http.headers - >> Cookie2: $Version=1
DEBUG: org.apache.http.wire -  >> "7d[\r][\n]"
DEBUG: org.apache.http.wire -  >> "--Boundary_1_674530318_1380707961372[\r][\n]"
DEBUG: org.apache.http.wire -  >> "Content-Type: application/octet-stream[\r][\n]"
DEBUG: org.apache.http.wire -  >> "Content-Disposition: form-data; name="file"[\r][\n]"
DEBUG: org.apache.http.wire -  >> "[\r][\n]"
DEBUG: org.apache.http.wire -  >> "[\r][\n]"
DEBUG: org.apache.http.wire -  >> "1061[\r][\n]"
DEBUG: org.apache.http.wire -  >> "[0x89]PNG[\r][\n]"
DEBUG: org.apache.http.wire -  >> "[0x1a][\n]"
DEBUG: org.apache.http.wire -  >> "[0x0][0x0][0x0][\r]IHDR[0x0]  -!!!! SNIP you don't want to see this !!!!
DEBUG: org.apache.http.wire -  >> "[\r][\n]"
DEBUG: org.apache.http.wire -  >> "2a[\r][\n]"
DEBUG: org.apache.http.wire -  >> "[\r][\n]"
DEBUG: org.apache.http.wire -  >> "--Boundary_1_674530318_1380707961372--[\r][\n]"
DEBUG: org.apache.http.wire -  >> "[\r][\n]"
DEBUG: org.apache.http.wire -  >> "0[\r][\n]"
DEBUG: org.apache.http.wire -  >> "[\r][\n]"
DEBUG: org.apache.http.wire -  << "HTTP/1.1 500 Server Error[\r][\n]"
DEBUG: org.apache.http.wire -  << "Content-Type: text/html; charset=ISO-8859-1[\r][\n]"
DEBUG: org.apache.http.wire -  << "Cache-Control: must-revalidate,no-cache,no-store[\r][\n]"
DEBUG: org.apache.http.wire -  << "Content-Length: 13979[\r][\n]"
DEBUG: org.apache.http.wire -  << "Server: Jetty(9.0.5.v20130815)[\r][\n]"
DEBUG: org.apache.http.wire -  << "[\r][\n]"
DEBUG: org.apache.http.impl.conn.DefaultClientConnection - Receiving response: HTTP/1.1 500 Server Error
DEBUG: org.apache.http.headers - << HTTP/1.1 500 Server Error
DEBUG: org.apache.http.headers - << Content-Type: text/html; charset=ISO-8859-1
DEBUG: org.apache.http.headers - << Cache-Control: must-revalidate,no-cache,no-store
DEBUG: org.apache.http.headers - << Content-Length: 13979
DEBUG: org.apache.http.headers - << Server: Jetty(9.0.5.v20130815)
DEBUG: org.apache.http.impl.client.DefaultHttpClient - Connection can be kept alive indefinitely
DEBUG: ws.webraw.jsonrest.JsonRestClient - reponse code : 500

我原本以为没有看到--Boundary条目。我只能假设使用ApacheConnector时实体编码出错了。

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:6)

尝试通过调用post来更改MultiPartMediaTypes.createFormData()来电的第二个参数。这应该创建一个具有已配置边界的新MediaType实例,该实例将由MultiPart编写器拾取。

target
    .path(path)
    .request()
    .post(Entity.entity(multipart,
                    MultiPartMediaTypes.createFormData()), Response.class);

答案 1 :(得分:2)

这是所有Jersey连接器实现中的已知问题;相关的错误是JERSEY-2123。正在JERSEY-2341中跟踪修复此问题的重构。

目前推荐的解决方法是简单地使用ClientBuilder的默认实现,该实现使用java.net.HttpUrlConnection并正确地将所需的boundary值添加到Content-Type标头。