HttpClient调用接口传递文件参数

方法一:使用 org.apache.commons.httpclient;的HttpClient
  • 具体代码:
public Map<String, Object> officialOcrUpload(String filePath) {
        Map<String, Object> reContent = new HashMap<>();
        String soapResponseInfo = "";
        File targeFile = new File(filePath);
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(OFFICIAL_ENDPOINT+"?bizType=tm");
        int state = 500;
        try {
            FilePart filePart = new FilePart(targeFile.getName(), targeFile);
            filePart.setContentType("multipart/form-data");
            filePart.setName("file");
            Part[] parts = {filePart};

            postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(Integer.valueOf(OFFICIAL_TIMEOUT));//设置连接超时
            state = httpClient.executeMethod(postMethod);
            if (state == HttpStatus.SC_OK) {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(postMethod.getResponseBodyAsStream(), "utf-8"));
                String responseLine = "";
                while((responseLine = bufferedReader.readLine()) != null){
                    soapResponseInfo += responseLine;
                }
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (HttpException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            reContent.put("status", state);
            reContent.put("body", soapResponseInfo);
            postMethod.releaseConnection();
            if (state == HttpStatus.SC_OK) {
                reContent = queryOfficalDoc(soapResponseInfo);
            }
        }
        return reContent;
    }
方法二:使用org.apache.http.impl.client;的HttpClient
<dependency>
          <groupId>org.apache.httpcomponents</groupId>
          <artifactId>httpmime</artifactId>
          <version>4.3</version>
        </dependency>

<dependency>
          <groupId>org.apache.httpcomponents</groupId>
          <artifactId>httpcore</artifactId>
          <version>4.4.6</version>
        </dependency>
  • 具体代码
public String officialFileUpload(String filepath){
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(OFFICIAL_ENDPOINT+"?bizType=tm");
        String result = "";
        try {
            FileBody bin = new FileBody(new File(filepath));
            MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
            reqEntity.addPart("file", bin);
            HttpEntity httpEntity = reqEntity.build();
            httpPost.setEntity(httpEntity);
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            httpPost.releaseConnection();
        }
        return result;
    }

目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值