jersey client上传下载文件

jersey client上传文件demo

    File file = new File("/tmp/test.jpg");
    System.out.println(file.exists());
    FormDataMultiPart part = new FormDataMultiPart();
    part.bodyPart(new FileDataBodyPart("file", file));
    part.bodyPart(new FormDataBodyPart("myparam", "false"));

    ClientConfig cc = new DefaultClientConfig();
    cc.getClasses().add(MultiPartWriter.class);
    Client writerClient = Client.create(cc);
    // 处理文件将超时设置为10S
    writerClient.setConnectTimeout(new Integer(3000));
    writerClient.setReadTimeout(new Integer(3000));
    try {
        WebResource resource = writerClient.resource("http://127.0.0.1:8080/rest/upload");
        String response = resource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(String.class, part);
        System.out.println(response);
    } finally {
        writerClient.destroy();
    }

jersey client下载文件demo

Client client = Client.create(config);

String sucaisss = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" + fuwuAccessToken + "&media_id=" + map.get("MediaId");

ClientResponse zongshushuchu
= client.resource(sucaisss).get(ClientResponse.class);

 

posted @ 2016-12-08 14:01  龙昊雪  阅读(1311)  评论(0编辑  收藏  举报