Example usage for org.apache.commons.httpclient.methods MultipartPostMethod addParameter

List of usage examples for org.apache.commons.httpclient.methods MultipartPostMethod addParameter

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods MultipartPostMethod addParameter.

Prototype

public void addParameter(String paramString1, String paramString2, File paramFile)
            throws FileNotFoundException 

Source Link

Usage

From source file:at.tuwien.minimee.emulation.EmulationService.java

/**
 * Currently not exposed as a web service since miniMEE
 * has been integrated with Plato.//from  www . j  a  v a  2s.c  o m
 * This starts a session with GRATE
 * @param samplename filename of the object to be rendered remotely
 * @param data the file to be rendered remotely
 * @param toolID pointing to the corresponding minimee configuration
 * @return a URL to be posted to the browser for opening a GRATE session.
 * This URL points to a GRATE session that contains the object readily waiting
 * to be rendered, already injected into the appropriate environment.
 * @throws PlatoServiceException if the connection to the GRATE server failed
 */
public String startSession(String samplename, byte[] data, String toolID) throws PlatoServiceException {
    ToolConfig config = getToolConfig(toolID);

    String response;
    try {
        HttpClient client = new HttpClient();
        MultipartPostMethod mPost = new MultipartPostMethod(config.getTool().getExecutablePath());
        client.setConnectionTimeout(8000);

        // MultipartPostMethod needs a file instance
        File sample = File.createTempFile(samplename + System.nanoTime(), "tmp");
        OutputStream out = new BufferedOutputStream(new FileOutputStream(sample));
        out.write(data);
        out.close();

        mPost.addParameter("datei", samplename, sample);

        int statusCode = client.executeMethod(mPost);

        response = mPost.getResponseBodyAsString();

        return response + config.getParams();

    } catch (HttpException e) {
        throw new PlatoServiceException("Could not connect to GRATE.", e);
    } catch (FileNotFoundException e) {
        throw new PlatoServiceException("Could not create temp file.", e);
    } catch (IOException e) {
        throw new PlatoServiceException("Could not connect to GRATE.", e);
    }

}