Example #1
0
  static int downloadPost(String srcPath, String decPath, String clusterId, String owner) {
    SessionState ss = SessionState.get();
    Result result = new Result();
    // 创建HttpClientBuilder
    // HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    // HttpClient
    // CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    // 传入svn用户名和密码
    credsProvider.setCredentials(
        new AuthScope(
            AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthPolicy.BASIC),
        new UsernamePasswordCredentials("dw_zouruochen", "*****@*****.**"));
    CloseableHttpClient closeableHttpClient =
        HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();

    HttpPost httpPost = new HttpPost(DOWNLOAD_URL);
    try {
      MultipartEntity reqEntity = new MultipartEntity();
      StringBody sp = new StringBody(srcPath, Consts.UTF_8);
      StringBody cId = new StringBody(clusterId, Consts.UTF_8);
      StringBody own = new StringBody(owner, Consts.UTF_8);

      reqEntity.addPart("path", sp);
      reqEntity.addPart("clusterId", cId);
      reqEntity.addPart("owner", own);
      httpPost.setEntity(reqEntity);
      // 执行get请求
      HttpResponse httpResponse = closeableHttpClient.execute(httpPost);
      // 获取响应消息实体
      HttpEntity entity = httpResponse.getEntity();
      // 响应状态
      int statusCode = httpResponse.getStatusLine().getStatusCode();
      System.out.println("status:" + statusCode);
      if (statusCode == HttpStatus.SC_OK) {
        // 判断响应实体是否为空
        if (entity != null) {
          ss.info.println("下载服务器正常响应.....");
          ss.info.println(
              "contentEncoding:" + entity.getContentEncoding() == null
                  ? ""
                  : entity.getContentEncoding());
          InputStream in = entity.getContent();
          // 必须用linux文件分隔符,不能是本地文件系统的分隔符
          String name = srcPath.substring(srcPath.lastIndexOf("/") + 1);
          FileOutputStream out = new FileOutputStream(new File(decPath + File.separator + name));

          byte[] b = new byte[BUFFER_SIZE];
          int len;
          while ((len = in.read(b)) != -1) {
            out.write(b, 0, len);
          }
          in.close();
          out.close();
        }
      } else {
        result.setCode(-1);
        result.setMsg("downLoadPost error!");
        ss.err.println(result.toString());
        return result.getCode();
      }

    } catch (Exception e) {
      LOG.error("downLoadPost error!", e);
      result.setCode(-1);
      result.setMsg("downLoadPost error!");
      ss.err.println(result.toString());
      return result.getCode();
    } finally {
      httpPost.releaseConnection();
    }

    result.setCode(0);
    result.setMsg("downLoadPost success!");
    ss.err.println(result.toString());
    return result.getCode();
  }
Example #2
0
  /**
   * 上传文件http请求客户端
   *
   * @param url 请求地址
   * @param srcPath 本地文件全路径
   * @param decPath 服务端存储目录
   * @param clusterId 集群id
   * @param owner 所属业务方
   * @return
   */
  static int uploadPost(Path localFile, String decPath, String clusterId, String owner) {
    SessionState ss = SessionState.get();
    Result result = new Result();

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    // 传入svn用户名和密码
    credsProvider.setCredentials(
        new AuthScope(
            AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthPolicy.BASIC),
        new UsernamePasswordCredentials("dw_zouruochen", "*****@*****.**"));
    CloseableHttpClient closeableHttpClient =
        HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    HttpPost httpPost = new HttpPost(UPLOAD_URL);

    try {
      FileBody srcFile = new FileBody(localFile.toFile());
      StringBody dec = new StringBody(decPath, Consts.UTF_8);
      StringBody id = new StringBody(clusterId, Consts.UTF_8);
      StringBody ownerStr = new StringBody(owner, Consts.UTF_8);

      MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
      String name = localFile.getFileName().toString();

      reqEntity.addPart("clusterId", id);
      reqEntity.addPart("decPath", dec);
      reqEntity.addPart("srcFile", srcFile);
      reqEntity.addPart("owner", ownerStr);

      StringBody fileName = new StringBody(name, Consts.UTF_8);
      reqEntity.addPart("fileName", fileName);

      httpPost.setEntity(reqEntity);
      HttpResponse response = closeableHttpClient.execute(httpPost);
      int statusCode = response.getStatusLine().getStatusCode();

      if (statusCode == HttpStatus.SC_OK) {
        // System.out.println("上传服务器正常响应.....");
        ss.info.println("上传服务器正常响应.....");
        HttpEntity resEntity = response.getEntity();
        ss.info.println("contentEncoding:" + resEntity.getContentEncoding());
        result.setData(EntityUtils.toString(resEntity));
        result.setCode(0);
        EntityUtils.consume(resEntity);
        ss.info.println(result.toString());
        return result.getCode();
      } else {
        result.setCode(-1);
        result.setMsg("uploadPost error!" + response.toString());
        ss.err.println(result.toString());
        return result.getCode();
      }
    } catch (Exception e) {
      LOG.error("uploadPost error!", e);
      result.setCode(-1);
      result.setMsg("uploadPost error!");
      ss.err.println(result.toString());
      return result.getCode();
    } finally {
      try {
        httpPost.releaseConnection();
      } catch (Exception ignore) {
        LOG.error("uploadPost error!", ignore);
      }
    }
  }