示例#1
0
  /**
   * Upload an app file.
   *
   * <p>URL (PST): {scheme}://{host}:{port}/{contextRoot}/appstore/apps/{appid}/content (FormData:
   * InputStream and FormDataContentDisposition)
   *
   * @param appId
   * @param file
   * @return
   * @throws ClientException
   * @throws IOException
   */
  public StatusDTO uploadApp(String appId, File file) throws ClientException {
    if (file == null || !file.exists()) {
      throw new ClientException(401, "File doesn't exist.");
    }
    if (!file.isFile()) {
      throw new ClientException(401, "File is not a file.");
    }

    try {
      MultiPart multipart = new FormDataMultiPart();
      {
        FileDataBodyPart filePart =
            new FileDataBodyPart("file", file, MediaType.APPLICATION_OCTET_STREAM_TYPE);
        {
          FormDataContentDisposition.FormDataContentDispositionBuilder formBuilder =
              FormDataContentDisposition.name("file");
          formBuilder.fileName(URLEncoder.encode(file.getName(), "UTF-8"));
          formBuilder.size(file.length());
          formBuilder.modificationDate(new Date(file.lastModified()));
          filePart.setFormDataContentDisposition(formBuilder.build());
        }
        multipart.bodyPart(filePart);
      }

      WebTarget target = getRootPath().path("appstore/apps").path(appId).path("content");
      Builder builder = target.request(MediaType.APPLICATION_JSON);
      Response response =
          updateHeaders(builder).post(Entity.entity(multipart, multipart.getMediaType()));
      checkResponse(response);

      StatusDTO status = response.readEntity(StatusDTO.class);
      return status;

    } catch (ClientException | UnsupportedEncodingException e) {
      handleException(e);
    }

    return null;
  }
  /**
   * uploads zip file using the input HTTP URL
   *
   * @param httpURL
   * @param filePath
   * @param filename
   * @return
   * @throws Exception
   */
  public static String testUploadService(String httpURL, File filePath) throws Exception {

    // local variables
    ClientConfig clientConfig = null;
    Client client = null;
    WebTarget webTarget = null;
    Invocation.Builder invocationBuilder = null;
    Response response = null;
    FileDataBodyPart fileDataBodyPart = null;
    FormDataMultiPart formDataMultiPart = null;
    int responseCode;
    String responseMessageFromServer = null;
    String responseString = null;

    try {
      // invoke service after setting necessary parameters
      clientConfig = new ClientConfig();
      clientConfig.register(MultiPartFeature.class);
      client = ClientBuilder.newClient(clientConfig);
      webTarget = client.target(httpURL);

      // set file upload values
      fileDataBodyPart =
          new FileDataBodyPart("uploadFile", filePath, MediaType.APPLICATION_OCTET_STREAM_TYPE);
      formDataMultiPart = new FormDataMultiPart();
      formDataMultiPart.bodyPart(fileDataBodyPart);

      StreamDataBodyPart bodyPart = new StreamDataBodyPart();

      // invoke service
      invocationBuilder = webTarget.request();
      //          invocationBuilder.header("Authorization", "Basic " + authorization);
      response =
          invocationBuilder.post(Entity.entity(formDataMultiPart, MediaType.MULTIPART_FORM_DATA));

      // get response code
      responseCode = response.getStatus();
      System.out.println("Response code: " + responseCode);

      if (response.getStatus() != 200) {
        throw new RuntimeException("Failed with HTTP error code : " + responseCode);
      }

      // get response message
      responseMessageFromServer = response.getStatusInfo().getReasonPhrase();
      System.out.println("ResponseMessageFromServer: " + responseMessageFromServer);

      // get response string
      responseString = response.readEntity(String.class);
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {
      // release resources, if any
      fileDataBodyPart.cleanup();
      formDataMultiPart.cleanup();
      formDataMultiPart.close();
      response.close();
      client.close();
    }
    return responseString;
  }