java 对接webapi接口 之 传递文件 + token验证

  • Post author:
  • Post category:java


包依赖
<dependency>
   <groupId>org.apache.httpcomponents</groupId>
   <artifactId>httpclient</artifactId>
   <version>4.5.3</version>
</dependency>
<dependency>
   <groupId>org.apache.httpcomponents</groupId>
   <artifactId>httpmime</artifactId>
   <version>4.5.3</version>
</dependency>
java 代码:



private String url="http://testgateway/upload";
private String filePath="D:\\wlecome.mp3";
 @Test
    public void inputStreamUpload() {
        //创建HttpClient对象
        CloseableHttpClient client = HttpClients.createDefault();
        //构建POST请求
        //1)
        String result=null;

        HttpPost post = new HttpPost(url);
        String tokenJson=getToekn();
net.sf.json.JSONObject tokenObject =                            
        net.sf.json.JSONObject.fromObject(tokenJson);
        String access_token=(String)tokenObject.get("access_token");
        post.setHeader("Authorization","Bearer"+" "+access_token);


        // postMethod.setRequestHeader("Authorization", token_type+" "+access_token);  //获取token类型和值  放在协议的头信息中
        InputStream inputStream = null;
        try {
            //文件路径请换成自己的
            //2)
            inputStream = new FileInputStream(filePath);
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setMode(HttpMultipartMode.RFC6532);//防止附件中文乱码
            //第一个参数为 相当于 Form表单提交的file框的name值 第二个参数就是我们要发送的InputStream对象了
            //第三个参数是文件名
            //3)
            builder.addBinaryBody("file", inputStream, ContentType.create("multipart/form-data"), "4A.jpg");
            HttpEntity entity = builder.build();
            post.setEntity(entity);


            //发送请求
            HttpResponse response = client.execute(post);
            //获取请求响应码
            int statusCode = response.getStatusLine().getStatusCode();
            if(statusCode==200){
                entity = response.getEntity();
                if (entity != null) {
                    inputStream = entity.getContent();
                    //转换为字节输入流
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, Consts.UTF_8));
                    StringBuffer sb = new StringBuffer();
                    while((result = reader.readLine())!= null){
                        sb.append(result);
                    }
                    result=sb.toString();
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("result======="+result);
    }



版权声明:本文为zhaofuqiangmycomm原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。