java Jersey 图片上传客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27859956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:32:33  来源:igfitidea点击:

Jersey Image Upload Client

javajerseyjax-rsjersey-clientjersey-1.0

提问by Jitendra Kumawat

I am trying to upload image using Jersey webservice , i am using jersey client to upload image. below is jersey web service which takes inputstream and uploads image on server. it works fine when i directly call it using jsp multipart form upload but fails when i upload images using jersey client

我正在尝试使用 Jersey webservice 上传图像,我正在使用 jersey 客户端上传图像。下面是球衣网络服务,它接受输入流并在服务器上上传图像。当我使用 jsp multipart form upload 直接调用它时它工作正常,但当我使用 jersey 客户端上传图像时失败

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

Below is Jersey Client to upload Image, the client code is part of another web service which is called from php rest client and this jersey client call to jersey web service to upload image, if i directly call jersey web service to upload image that work's fine but it is not working when i upload using jersey client.

下面是 Jersey 客户端上传图像,客户端代码是从 php rest 客户端调用的另一个 web 服务的一部分,这个 jersey 客户端调用 jersey web 服务上传图像,如果我直接调用 jersey web 服务上传图像,那么工作正常但是当我使用球衣客户端上传时它不起作用。

ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
client.setChunkedEncodingSize(1024);
WebResource wr = client
        .resource("http://localhost:8080/rest/upload");

String contentDisposition = "attachment; filename=\""
        + fileDetail.getName() + "\"";
FormDataMultiPart form = new FormDataMultiPart();
ContentDisposition contentDisposition2 = new ContentDisposition(contentDisposition);
form.setContentDisposition(contentDisposition2);
FormDataBodyPart fdp = new FormDataBodyPart("file",
        uploadedInputStream, MediaType.MULTIPART_FORM_DATA_TYPE);
form.bodyPart(fdp);
ClientResponse response = wr.type(MediaType.MULTIPART_FORM_DATA).post(
        ClientResponse.class, form)

Please help me not sure what i am missing here. Thanks.

请帮助我不确定我在这里缺少什么。谢谢。

回答by abdotalaat

this full example to upload image using jersey client and webservice you client code

这个使用 jersey 客户端和 webservice 上传图像的完整示例你的客户端代码

public class Test {

    private static URI getBaseURI() {
        return UriBuilder.fromUri("http://localhost:8080/restfullwebservice/resources/generic").build("");
    }

    public static void main(String[] args) throws FileNotFoundException {
        final ClientConfig config = new DefaultClientConfig();
        final Client client = Client.create(config);

        final WebResource resource = client.resource(getBaseURI()).path("upload");

        final File fileToUpload = new File("C:/Users/Public/Pictures/Desert.jpg");

        final FormDataMultiPart multiPart = new FormDataMultiPart();
        if (fileToUpload != null) {
            multiPart.bodyPart(new FileDataBodyPart("file", fileToUpload,
                    MediaType.APPLICATION_OCTET_STREAM_TYPE));
        }

        final ClientResponse clientResp = resource.type(
                MediaType.MULTIPART_FORM_DATA_TYPE).post(ClientResponse.class,
                multiPart);
        System.out.println("Response: " + clientResp.getClientResponseStatus());

        client.destroy();
    }
}

your webservice

你的网络服务

@POST
@Path("upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void uploadFile(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail) throws ServiceChannelException {
    OutputStream os = null;
    try {
        File fileToUpload = new File("C:/Users/Public/Pictures/Desert1.jpg");
        os = new FileOutputStream(fileToUpload);
        byte[] b = new byte[2048];
        int length;
        while ((length = uploadedInputStream.read(b)) != -1) {
            os.write(b, 0, length);
        }
    } catch (IOException ex) {
        Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            os.close();
        } catch (IOException ex) {
            Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Full application https://github.com/abdotalaat/upladeimageusingjersy

完整申请 https://github.com/abdotalaat/upladeimageusingjersy