Example usage for org.apache.commons.httpclient.methods.multipart StringPart StringPart

List of usage examples for org.apache.commons.httpclient.methods.multipart StringPart StringPart

Introduction

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

Prototype

public StringPart(String paramString1, String paramString2) 

Source Link

Usage

From source file:com.mycompany.semconsolewebapp.FileUpload.java

public static void uploadFileAndMetaDataToServer(String imagePath, String operators, String channel, int kv,
        int mag, int wd) throws FileNotFoundException {
    File f = new File(imagePath);
    Part[] parts = { new StringPart("working_depth", Integer.toString(wd)),
            new StringPart("magnification", Integer.toString(mag)),
            new StringPart("voltage", Integer.toString(kv * 1000)), new StringPart("operators", operators),
            new StringPart("sensor", channel), new StringPart("update_metadata", "1"), new FilePart("img", f) };
    try {//from w w  w  .  ja  va  2  s . co m
        uploadFileToServer(parts);
    } catch (IOException e) {
    }
}

From source file:com.infosupport.service.LPRServiceCaller.java

public static void postData(String urlString, File file) {
    try {/*from  w  w w  . j a v a  2 s  .c om*/
        PostMethod postMessage = new PostMethod(urlString);
        Part[] parts = { new FilePart("lpimage", file), new StringPart("country", "eu"),
                new StringPart("nonce", "123456789") };
        postMessage.setRequestEntity(new MultipartRequestEntity(parts, postMessage.getParams()));
        HttpClient client = new HttpClient();
        client.executeMethod(postMessage);
    } catch (IOException e) {
        Log.w(TAG, "IOException, waarschijnlijk geen internet connectie aanwezig...");
    }
}

From source file:ca.uvic.cs.tagsea.wizards.TagNetworkSender.java

public static synchronized void send(String xml, IProgressMonitor sendMonitor, int id)
        throws InvocationTargetException {
    sendMonitor.beginTask("Uploading Tags", 100);
    sendMonitor.subTask("Saving Temporary file...");
    File location = TagSEAPlugin.getDefault().getStateLocation().toFile();
    File temp = new File(location, "tagsea.temp.file.txt");
    if (temp.exists()) {
        String message = "Unable to send tags. Unable to create temporary file.";
        IOException ex = new IOException(message);
        throw (new InvocationTargetException(ex, message));
    }//from  w  ww.  j  a  v a 2s  .c  o  m
    try {
        FileWriter writer = new FileWriter(temp);
        writer.write(xml);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        throw new InvocationTargetException(e);
    }
    sendMonitor.worked(5);
    sendMonitor.subTask("Uploading Tags...");
    String uploadScript = "http://stgild.cs.uvic.ca/cgi-bin/tagSEAUpload.cgi";
    PostMethod post = new PostMethod(uploadScript);

    String fileName = getToday() + ".txt";
    try {
        Part[] parts = { new StringPart("KIND", "tag"), new FilePart("MYLAR" + id, fileName, temp) };
        post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
        HttpClient client = new HttpClient();
        int status = client.executeMethod(post);
        String resp = getData(post.getResponseBodyAsStream());
        if (status != 200) {
            IOException ex = new IOException(resp);
            throw (ex);
        }
    } catch (IOException e) {
        throw new InvocationTargetException(e, e.getLocalizedMessage());
    } finally {
        sendMonitor.worked(90);
        sendMonitor.subTask("Deleting Temporary File");
        temp.delete();
        sendMonitor.done();
    }

}

From source file:net.morphbank.mbsvc3.test.SendImageTest.java

public void sendImage(String strURL, String id, String originalFileName, String imageFileName)
        throws Exception {
    File input = new File(imageFileName);
    // Prepare HTTP post
    PostMethod post = new PostMethod(strURL);

    Part[] parts = { new StringPart("id", id), new StringPart("fileName", originalFileName),
            new FilePart("image", originalFileName, input) };
    RequestEntity entity = new MultipartRequestEntity(parts, post.getParams());
    post.setRequestEntity(entity);/*from ww  w  .  j av a  2  s .  c om*/
    // Get HTTP client
    HttpClient httpclient = new HttpClient();
    // Execute request
    try {
        System.out.println("Trying post");
        int result = httpclient.executeMethod(post);
        // Display status code
        System.out.println("Response status code: " + result);
        // Display response
        System.out.println("Response body: ");
        InputStream response = post.getResponseBodyAsStream();
        // int j = response.read(); System.out.write(j);
        for (int i = response.read(); i != -1; i = response.read()) {
            System.out.write(i);
        }
        // System.out.flush();
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }
}

From source file:ca.uvic.cs.tagsea.monitor.NetworkLogging.java

public synchronized boolean uploadForDate(Date d) throws IOException {
    File f = SimpleDayLog.findLogFileForDay(d);
    if (f.length() == 0)
        return true;

    PostMethod post = new PostMethod(uploadScript);
    Display disp = TagSEAMonitorPlugin.getDefault().getWorkbench().getDisplay();
    int id = getUID();
    if (id < 0) {
        //error getting the user id. Quit.
        return false;
    }//  w  w w  . ja va 2 s .c o m
    Part[] parts = { new StringPart("KIND", "log"), new FilePart("MYLAR" + id, f.getName(), f) };
    post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
    HttpClient client = new HttpClient();
    int status = client.executeMethod(post);
    resp = getData(post.getResponseBodyAsStream());
    if (status != 200) {
        disp.syncExec(new Runnable() {
            public void run() {
                MessageDialog.openError(null, "Error Sending File", resp);
            }
        });
        return false;

    }
    MonitorPreferences.setLastSendDate(d);
    return true;

}

From source file:net.morphbank.webclient.RemoteImageProcessing.java

public void sendImage(String strURL, String id, String originalFileName, String imageFileName)
        throws Exception {
    File input = new File(imageFileName);
    // Prepare HTTP post
    PostMethod post = new PostMethod(strURL);

    Part[] parts = { new StringPart("id", id), new StringPart("fileName", originalFileName),
            new FilePart("image", originalFileName, input) };
    RequestEntity entity = new MultipartRequestEntity(parts, post.getParams());
    post.setRequestEntity(entity);/*from  w ww. ja  va  2  s .c o  m*/
    // Get HTTP client
    HttpClient httpclient = new HttpClient();
    // Execute request
    try {
        System.out.println("Trying post");
        int postStatus = httpclient.executeMethod(post);
        // Display status code
        System.out.println("Response status code: " + postStatus);
        // Display response
        System.out.print("Response body: ");
        InputStream response = post.getResponseBodyAsStream();
        for (int i = response.read(); i != -1; i = response.read()) {
            System.out.write(i);
        }
        System.out.flush();
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }
}

From source file:de.laeubisoft.tools.ant.validation.Tools.java

public static List<Part> nvToParts(List<NameValuePair> params) {
    List<Part> parts = new ArrayList<Part>();
    for (NameValuePair nameValuePair : params) {
        parts.add(new StringPart(nameValuePair.getName(), nameValuePair.getValue()));
    }//from  w ww .j a  v  a 2s . c o m
    return parts;
}

From source file:edu.caltech.ipac.firefly.server.util.multipart.MultiPartPostBuilder.java

public void addParam(String name, String value) {
    parts.add(new StringPart(name, value));
    params.add(new Param(name, value));
}

From source file:edu.tsinghua.lumaqq.test.CustomHeadUploader.java

/**
 * //www .j a va  2s . co  m
 * 
 * @param filename
 */
public void upload(String filename, QQUser user) {
    HttpClient client = new HttpClient();
    HostConfiguration conf = new HostConfiguration();
    conf.setHost(QQ.QQ_SERVER_UPLOAD_CUSTOM_HEAD);
    client.setHostConfiguration(conf);
    PostMethod method = new PostMethod("/cgi-bin/cface/upload");
    method.addRequestHeader("Accept", "*/*");
    method.addRequestHeader("Pragma", "no-cache");

    StringPart uid = new StringPart("clientuin", String.valueOf(user.getQQ()));
    uid.setContentType(null);
    uid.setTransferEncoding(null);

    //StringPart clientkey = new StringPart("clientkey", "0D649E66B0C1DBBDB522CE9C846754EF6AFA10BBF1A48A532DF6369BBCEF6EE7");
    //StringPart clientkey = new StringPart("clientkey", "3285284145CC19EC0FFB3B25E4F6817FF3818B0E72F1C4E017D9238053BA2040");
    StringPart clientkey = new StringPart("clientkey",
            "2FEEBE858DAEDFE6352870E32E5297ABBFC8C87125F198A5232FA7ADA9EADE67");
    //StringPart clientkey = new StringPart("clientkey", "8FD643A7913C785AB612F126C6CD68A253F459B90EBCFD9375053C159418AF16");
    //      StringPart clientkey = new StringPart("clientkey", Util.convertByteToHexStringWithoutSpace(user.getClientKey()));
    clientkey.setContentType(null);
    clientkey.setTransferEncoding(null);

    //StringPart sign = new StringPart("sign", "2D139466226299A73F8BCDA7EE9AB157E8D9DA0BB38B6F4942A1658A00BD4C1FEE415838810E5AEF40B90E2AA384A875");
    //StringPart sign = new StringPart("sign", "50F479417088F26EFC75B9BCCF945F35346188BB9ADD3BDF82098C9881DA086E3B28D56726D6CB2331909B62459E1E62");
    //StringPart sign = new StringPart("sign", "31A69198C19A7C4BFB60DCE352FE2CC92C9D27E7C7FEADE1CBAAFD988906981ECC0DD1782CBAE88A2B716F84F9E285AA");
    StringPart sign = new StringPart("sign",
            "68FFB636DE63D164D4072D7581213C77EC7B425DDFEB155428768E1E409935AA688AC88910A74C5D2D94D5EF2A3D1764");
    sign.setContentType(null);
    sign.setTransferEncoding(null);

    FilePart file;
    try {
        file = new FilePart("customfacefile", filename, new File(filename));
    } catch (FileNotFoundException e) {
        return;
    }

    Part[] parts = new Part[] { uid, clientkey, sign, file };
    MultipartRequestEntity entity = new MultipartRequestEntity(parts, method.getParams());

    method.setRequestEntity(entity);
    try {
        client.executeMethod(method);
        if (method.getStatusCode() == HttpStatus.SC_OK) {
            Header header = method.getResponseHeader("CFace-Msg");
            System.out.println(header.getValue());
            header = method.getResponseHeader("CFace-RetCode");
            System.out.println(header.getValue());
        }
    } catch (HttpException e) {
        return;
    } catch (IOException e) {
        return;
    } finally {
        method.releaseConnection();
    }
}

From source file:ca.uvic.chisel.logging.eclipse.internal.network.LogUploadRunnable.java

public void run(IProgressMonitor sendMonitor) throws InterruptedException, InvocationTargetException {
    File[] filesToUpload = log.getCategory().getFilesToUpload();
    sendMonitor.beginTask("Uploading Log " + log.getCategory().getName(), filesToUpload.length);
    LoggingCategory category = log.getCategory();
    IMemento memento = category.getMemento();
    for (File file : filesToUpload) {
        if (sendMonitor.isCanceled()) {
            throw new InterruptedException();
        }//from  ww  w. j a  v a 2s  .c om
        PostMethod post = new PostMethod(category.getURL().toString());

        try {
            Part[] parts = { new StringPart("KIND", "workbench-log"),
                    new StringPart("CATEGORY", category.getCategoryID()),
                    new StringPart("USER", WorkbenchLoggingPlugin.getDefault().getLocalUser()),
                    new FilePart("WorkbenchLogger", file.getName(), file, "application/zip", null) };
            post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
            HttpClient client = new HttpClient();
            int status = client.executeMethod(post);
            String resp = getData(post.getResponseBodyAsStream());
            if (status != 200 || !resp.startsWith("Status: 200 Success")) {
                IOException ex = new IOException(resp);
                throw (ex);
            }
            memento.putString("lastUpload", file.getName());
            file.delete();
        } catch (IOException e) {
            throw new InvocationTargetException(e);
        } finally {
            sendMonitor.worked(1);
        }
    }
    sendMonitor.done();
}