在 JAVA 中使用 Rest API 客户端上传文件

File Upload Using Rest API Client in JAVA

我创建了一个客户端 rest api 以在 api 端点上传文件。但这给了我错误:

Exception in thread "main" org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=multipart/form-data, type=class org.glassfish.jersey.media.multipart.FormDataMultiPart, genericType=class org.glassfish.jersey.media.multipart.FormDataMultiPart.
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:191)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:139)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1005)
    at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:430)
    at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:287)
    at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:200)
    at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:215)
    at org.glassfish.jersey.client.JerseyInvocation.call(JerseyInvocation.java:635)
    at org.glassfish.jersey.client.JerseyInvocation.call(JerseyInvocation.java:632)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:421)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:632)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:392)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:301)
    at Test.main(Test.java:35)

这是我的客户休息api代码:

import java.io.File;

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.Response;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;

public class Test {

    public void configureClient(ClientConfig config) {
        config.register(MultiPartFeature.class);
    }
    public static void main(String[] args) {
        
        Client client = ClientBuilder.newClient().register(MultiPart.class);
        WebTarget server = client.target("http://localhost:8080/repositories/file/upload");
        
        FileDataBodyPart filePart = new FileDataBodyPart("file", new File("C:\Users\kunal\Downloads\JWTUtility.java"));
        filePart.setContentDisposition(FormDataContentDisposition.name("file").fileName("JWTUtility.java").build());
        
        MultiPart multipartEntity = new FormDataMultiPart().bodyPart(filePart);
        
        Response result = server.request().post(Entity.entity(multipartEntity, multipartEntity.getMediaType()));
        
        System.out.println(result.getStatus());
        System.out.println(result.readEntity(String.class));

        result.close();
        //assertEquals(Response.Status.OK.getStatusCode(), result.getStatus());
    }

}

API端点代码:

package com.howtodoinjava.jersey;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;

@Path("/upload")
public class JerseyService 
{
    @POST
    @Path("/pdf")
    @Consumes({MediaType.MULTIPART_FORM_DATA})
    public Response uploadPdfFile(  @FormDataParam("file") InputStream fileInputStream,
                                    @FormDataParam("file") FormDataContentDisposition fileMetaData) throws Exception
    {
        String UPLOAD_PATH = "D://Test/";
        try
        {
            int read = 0;
            byte[] bytes = new byte[1024];
     
            OutputStream out = new FileOutputStream(new File(UPLOAD_PATH + fileMetaData.getFileName()));
            while ((read = fileInputStream.read(bytes)) != -1)
            {
                out.write(bytes, 0, read);
            }
            out.flush();
            out.close();
        } catch (IOException e)
        {
            throw new WebApplicationException("Error while uploading file. Please try again !!");
        }
        return Response.ok("Data uploaded successfully !!").build();
    }
}

在客户端,您需要添加对 multipart/form-data 功能的支持,它将注册所需的 reader 和编写器:

public class Test {

    public void configureClient(ClientConfig config) {
        config.register(MultiPartFeature.class);
    }

    public static void main(String[] args) {
        DefaultClientConfig config = new DefaultClientConfig();
        configureClient(config);
        Client client = ClientBuilder.newClient(config);
        WebTarget server = client.target("http://localhost:8080/repositories/file/upload");
        // or alternatively at the WebTarget level
        // server.register(MultiPartFeature.class);
        
        // ...
    }

}