java-如何创建Jersey FormDataContentDisposition对象?

我在服务中有以下方法:

public interface MyService {    
    @POST
    @Path("/upload-file")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    void uploadFile(@FormDataParam("file") InputStream inputStream,
                    @FormDataParam("file") FormDataContentDisposition contentDisposition);
}

然后使用WebResourceFactory创建此接口的实例:

final MyService buildProxy() {
    final ClientBuilder clientBuidler = ClientBuilder.newBuilder();
    // register components, trust manager, etc.
    final WebTarget target = clientBuilder.build().target("http://myservice.example.com");
    return WebResourceFactory.newResource(
            MyService.class, target. false, ImmutableMultivaluedMap.empty(), 
            Collections.emptyList(), new Form());
}

我想使用此代理来调用我的方法,但是我找不到创建FormDataContentDisposition的便捷方法.我最好的选择是创建标题字符串,然后将其传递到新的FormDataContentDisposition(String header)吗?我可以在某个地方放松我的类型以访问更方便的构造函数吗?我可以在文件上传中使用其他更方便的界面吗?我主要在界面中包含内容配置,因此在将整个内容保存到磁盘之前,我可以拒绝太大的文件.

解决方法:

您既可以在客户端使用FormDataMultipart,也可以将其用作服务器参数. FormDataMultiPart使您可以以编程方式(而不是声明方式)访问所有身体部位.这将使您在客户端具有更多的*度.请参阅下面的完整测试案例

public class ProxyMultiPartTest extends JerseyTest {

    public static interface IUploadResource {
        @POST
        @Consumes(MediaType.MULTIPART_FORM_DATA)
        String upload(FormDataMultiPart multipart) throws Exception;
    }

    @Path("upload")
    public static class UploadResource implements IUploadResource {
        @Override
        public String upload(FormDataMultiPart multiPart) throws Exception {
            FormDataBodyPart bodyPart = multiPart.getField("file");
            FormDataContentDisposition fdcd = bodyPart.getFormDataContentDisposition();
            System.out.println("filename: " + fdcd.getFileName());
            System.out.println("size: " + fdcd.getSize());
            InputStream body = bodyPart.getValueAs(InputStream.class);
            StringWriter writer = new StringWriter();
            ReaderWriter.writeTo(new InputStreamReader(body), writer);
            return writer.toString();
        }
    }

    @Override
    public ResourceConfig configure() {
        return new ResourceConfig()
                .register(UploadResource.class)
                .register(MultiPartFeature.class)
                .register(new LoggingFilter(Logger.getAnonymousLogger(), true));

    }

    @Test
    public void testProxyClientUpload() throws Exception {
        try (Writer writer = new BufferedWriter(new FileWriter("demo.txt"))) {
            writer.write("Hello World");
        }
        WebTarget target = target("upload").register(MultiPartFeature.class);
        IUploadResource resource = WebResourceFactory.newResource(IUploadResource.class, target);
        FileDataBodyPart filePart = new FileDataBodyPart("file", new File("demo.txt"));
        FormDataMultiPart multiPart = new FormDataMultiPart();
        multiPart.bodyPart(filePart);

        String response = resource.upload(multiPart);
        assertEquals("Hello World", response);
    }
}
上一篇:java-如何使用Jersey REST客户端发送包含JSON格式的dom元素的请求


下一篇:java-Jersey REST / JAXB错误,映射接口