美文网首页
jersey client上传文件

jersey client上传文件

作者: kevinp | 来源:发表于2015-05-22 20:38 被阅读1745次

    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上传文件

          本文链接:https://www.haomeiwen.com/subject/ooalqttx.html