JAVA,Jersy将文件发布到服务器并从服务器获取ing文件。使用多部分
代码: Iam在upload at return语句中遇到异常。所以它将捕获并给出空指针异常。我已经在Google上搜索了这个错误com.sun.jersey.api.client.clienthandler异常,但没有解决我的错误。我添加了mimepull.jar和jeresey-multipart.jar。这个问题仍然没有解决JAVA,Jersy将文件发布到服务器并从服务器获取ing文件。使用多部分,java,spring,rest,jersey,jax-rs,Java,Spring,Rest,Jersey,Jax Rs,代码: Iam在upload at return语句中遇到异常。所以它将捕获并给出空指针异常。我已经在Google上搜索了这个错误com.sun.jersey.api.client.clienthandler异常,但没有解决我的错误。我添加了mimepull.jar和jeresey-multipart.jar。这个问题仍然没有解决 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExc
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;
}
}
感谢您的帮助和建议。您的错误报告有问题,尽管如果您是从新泽西到新泽西,可能看起来有点神秘。您的客户端当前不知道如何映射多部分请求
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)
我必须在我的ApplicationConfig服务器定义中注册multipart内容才能让它工作,比如
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
并进行实验
this.client = Client.create(new DefaultClientConfig());
OP使用的是Jersey 1.x。查看堆栈跟踪中的包名com.sun.jersey。您添加了错误的多部分依赖项。您正在使用Jersey 1.x客户端。您添加的mulitpart是针对2.x的,您需要添加jersey multipart 1.x依赖项。
this.client = Client.create(new DefaultClientConfig());
final Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();