Jersey客户端文件上传异常:NoClassDefFoundError:org / glassfish / jersey / message / internal / HeadersFactory

时间:2016-01-18 12:51:15

标签: java rest file-upload jersey

我试图创建一个球衣客户端来上传文件。我搜索了i-net,但我无法找到适合我的例外的解决方案。

这是我的客户代码:

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;

//...

private static void clientUploadTest() {
    ClientConfig config = new ClientConfig();
    Client client       = ClientBuilder.newClient(config);
    client.register(MultiPartFeature.class);
    URI baseUri         = UriBuilder.fromUri("http://localhost/rest/test").build();
    WebTarget target    = client.target(baseUri);

    MultiPart multiPart = new MultiPart();

}

如果我尝试添加multiPart,我会遇到以下异常。我尝试了不同的方法,但我解决了我的问题。我也试过这样的事情:

final FileDataBodyPart filepart     = new FileDataBodyPart("file", new File("C:/Users/admin/Downloads/testtext.txt"));
FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
final FormDataMultiPart multiPart   = (FormDataMultiPart) formDataMultiPart.field("foo", "bar").bodyPart(filepart);

但我得到了同样的错误。

Exception in thread "main" java.lang.NoClassDefFoundError: org/glassfish/jersey/message/internal/HeadersFactory at org.glassfish.jersey.media.multipart.BodyPart.<init>(BodyPart.java:70) at org.glassfish.jersey.media.multipart.MultiPart.<init>(MultiPart.java:66) at de.restclient.requests.ClientTest.clUploadtest(ClientTest.java:64) at de.restclient.requests.ClientTest.main(ClientTest.java:46) Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.message.internal.HeadersFactory at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 4 more

这是我导入的球衣文件。

Jersey files

我能够向服务器发送正常的GET和Post Request。但我也需要发送文件。 我希望,有人可以帮助我。

1 个答案:

答案 0 :(得分:1)

你是missing a dependency

org.glassfish.jersey.media:jersey-media-multipart