Example usage for org.apache.http.entity.mime.content FileBody FileBody

List of usage examples for org.apache.http.entity.mime.content FileBody FileBody

Introduction

In this page you can find the example usage for org.apache.http.entity.mime.content FileBody FileBody.

Prototype

public FileBody(final File file) 

Source Link

Usage

From source file:org.openml.apiconnector.io.OpenmlConnector.java

/**
 * Uploads trace results in the database, typically used when an internal parameter optimization loop was executed. (admin rights required, typically executed by evaluation engine)
 * //from ww w.j a  v  a2  s .co  m
 * @param trace - the trace description xml
 * @return
 * @throws Exception
 */
public RunTraceUpload runTraceUpload(File trace) throws Exception {
    MultipartEntity params = new MultipartEntity();
    params.addPart("trace", new FileBody(trace));

    Object apiResult = HttpConnector.doApiRequest(OPENML_URL + API_PART + "run/trace", params, getApiKey(),
            verboseLevel);
    if (apiResult instanceof RunTraceUpload) {
        return (RunTraceUpload) apiResult;
    } else {
        throw new DataFormatException("Casting Api Object to RunTraceUpload");
    }
}

From source file:org.openml.apiconnector.io.OpenmlConnector.java

/**
 * A list with the parameter settings of a setup
 * //from   ww  w.j  av  a 2 s  .com
 * @param a file equivalent to run description, but only featuring the parts important to parameters
 * @return
 * @throws Exception
 */
public SetupExists setupExists(File description) throws Exception {
    MultipartEntity params = new MultipartEntity();
    if (verboseLevel >= Constants.VERBOSE_LEVEL_XML) {
        System.out.println(Conversion.fileToString(description) + "\n==========");
    }
    params.addPart("description", new FileBody(description));
    Object apiResult = HttpConnector.doApiRequest(OPENML_URL + API_PART + "setup/exists", params, getApiKey(),
            verboseLevel);
    if (apiResult instanceof SetupExists) {
        return (SetupExists) apiResult;
    } else {
        throw new DataFormatException("Casting Api Object to SetupExists");
    }
}

From source file:org.openml.apiconnector.io.OpenmlConnector.java

public FileUpload fileUpload(File file) throws Exception {
    MultipartEntity params = new MultipartEntity();
    if (verboseLevel >= Constants.VERBOSE_LEVEL_ARFF) {
        System.out.println(Conversion.fileToString(file) + "\n==========\n");
    }//from  w  w  w  .  j  a  va2  s.  c o m
    params.addPart("file", new FileBody(file));

    Object apiResult = HttpConnector.doApiRequest(OPENML_URL + API_PART + "file/upload", params, getApiKey(),
            verboseLevel);
    if (apiResult instanceof FileUpload) {
        return (FileUpload) apiResult;
    } else {
        throw new DataFormatException("Casting Api Object to UploadFile");
    }
}

From source file:org.openstreetmap.josm.plugins.mapillary.oauth.UploadUtils.java

/**
 * @param file File that is going to be uploaded
 * @param hash Information attached to the upload
 * @throws IllegalArgumentException if the hash doesn't contain all the needed keys.
 *//*from  w w  w .ja  v a  2s  . c o m*/
private static void uploadFile(File file, Map<String, String> hash) throws IOException {
    HttpClientBuilder builder = HttpClientBuilder.create();
    HttpPost httpPost = new HttpPost(UPLOAD_URL);

    try (CloseableHttpClient httpClient = builder.build()) {
        MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
        for (String key : keys) {
            if (hash.get(key) == null)
                throw new IllegalArgumentException();
            entityBuilder.addPart(key, new StringBody(hash.get(key), ContentType.TEXT_PLAIN));
        }
        entityBuilder.addPart("file", new FileBody(file));
        HttpEntity entity = entityBuilder.build();
        httpPost.setEntity(entity);
        try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
            if (response.getStatusLine().toString().contains("204")) {
                PluginState.imageUploaded();
                Main.info(PluginState.getUploadString() + " (Mapillary)");
            } else {
                Main.info("Upload error");
            }
        }
    }
    if (!file.delete()) {
        Main.error("MapillaryPlugin: File could not be deleted during upload");
    }
    MapillaryUtils.updateHelpText();
}