代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import org.glassfish.jersey.client.ClientResponse;
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
import com.itz.passport.authentication.Int3DPassport;
import com.sun.jersey.api.client.*;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.multipart.impl.MultiPartWriter;
public class UploadFileRestClient {
private WebResource webResource;
private Client client;
private String url = "http://development/server/resources/FileService";
public UploadFileRestClient() {
try {
this.client = Client.create(new DefaultClientConfig());
this.webResource = client.resource(url).path("/file");
} catch (Exception e) {
e.printStackTrace();
}
}
public ClientResponse upload() {
try {
Int3DPassport passportAut = new InPassport();
String host = "http://development/server";
String userName = "admin";
String password = "xxxxx";
String ticket = IntPassport.getTicket(host, userName, password);
System.out.println("ticket:::::" + ticket);
WebResource resource = this.webResource;
MultiPart multiPart = new MultiPart();
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("photo", new File("D://Documents//Test.txt"));
multiPart.bodyPart(fileDataBodyPart);
// here Iam getting error, return statement.
return resource.path("upload"+ticket).type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, multiPart);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
我在return语句的upload()中获取异常。因此它将捕获并给出空指针异常。我已经用google搜索了此错误com.sun.jersey.api.client.ClientHandlerException,但没有任何解决方案可以解决我的错误。我添加了mimepull.jar和jeresey-multipart.jar。问题仍然没有解决。
com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class org.glassfish.jersey.media.multipart.MultiPart, and MIME media type, multipart/form-data, was not found
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:155)
at com.sun.jersey.api.client.Client.handle(Client.java:652)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:570)
at Test.main.UploadFileRestClient.upload(UploadFileRestClient.java:66)
at Test.main.Main.main(Main.java:38)
Caused by: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class org.glassfish.jersey.media.multipart.MultiPart, and MIME media type, multipart/form-data, was not found
at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:288)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:217)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:153)
... 6 more
Exception in thread "main" java.lang.NullPointerException
at Test.main.Main.main(Main.java:40)
感谢您的帮助和建议。
您的错误报告有问题,尽管如果您是球衣新手,可能会有些含糊。您的客户目前不知道如何映射多部分请求。
Caused by: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class org.glassfish.jersey.media.multipart.MultiPart, and MIME media type, multipart/form-data, was not found
我不得不在我的ApplicationConfig服务器定义中注册多变的东西,才能使其正常工作,例如;
@Override
public Set<Class<?>> getClasses()
{
Set<Class<?>> resources = new java.util.HashSet<>();
addRestResourceClasses(resources);
resources.add(MultiPartFeature.class);
return resources;
}
阅读this应该可以帮助您解决问题。