首页 文章

Jersey Client API - POSTing InputStream

提问于
浏览
3

实现这个的最佳方法是什么:

我的应用程序允许用户上传图像,这是通过RESTful服务完成的,编码为“multipart / form-data” .

现在,在服务的主体中,我真的不需要保存这个文件,但我想用它来传入并调用另一个服务 . 那么我是否可以使用Jersey Client API进行另一次调用,而无需将文件保存到磁盘,然后传入所谓的“临时”文件 .

这是我的一些代码:

@Path("/file")
public class UploadFileService {

    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(
        @FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition fileDetail) {

            //Given that I have ‘uploadedInputStream’ can I just pass this  
            //directly into the second call, below?

            ClientConfig config = new DefaultClientConfig();
            Client client = Client.create(config);
            WebResource wr = client.resource(baseURI);
            ClientResponse response = wr.type("image/*")
                                        .entity(uploadedInputStream)   //legal??
                                        .post(ClientResponse.class);

    }
}

我猜测上面的替代方法,就是暂时保存文件,然后将java.io.File的实例传入entity()方法 . 但是,有可能逃脱这个吗?

1 回答

相关问题