Example usage for org.apache.commons.httpclient.params HttpMethodParams MULTIPART_BOUNDARY

List of usage examples for org.apache.commons.httpclient.params HttpMethodParams MULTIPART_BOUNDARY

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.params HttpMethodParams MULTIPART_BOUNDARY.

Prototype

String MULTIPART_BOUNDARY

To view the source code for org.apache.commons.httpclient.params HttpMethodParams MULTIPART_BOUNDARY.

Click Source Link

Usage

From source file:com.kaltura.client.KalturaClientBase.java

private PostMethod getPostMultiPartWithFiles(PostMethod method, KalturaParams kparams, KalturaFiles kfiles) {

    String boundary = "---------------------------" + System.currentTimeMillis();
    List<Part> parts = new ArrayList<Part>();
    parts.add(new StringPart(HttpMethodParams.MULTIPART_BOUNDARY, boundary));

    for (Entry<String, String> itr : kparams.entrySet()) {
        parts.add(new StringPart(itr.getKey(), itr.getValue()));
    }/*from   w ww.j av  a2  s.c o  m*/

    for (String key : kfiles.keySet()) {
        final KalturaFile kFile = kfiles.get(key);
        parts.add(new StringPart(key, "filename=" + kFile.getName()));
        if (kFile.getFile() != null) {
            // use the file
            File file = kFile.getFile();
            try {
                parts.add(new FilePart(key, file));
            } catch (FileNotFoundException e) {
                // TODO this sort of leaves the submission in a weird state... -AZ
                if (logger.isEnabled())
                    logger.error("Exception while iterating over kfiles", e);
            }
        } else {
            // use the input stream
            PartSource fisPS = new PartSource() {
                public long getLength() {
                    return kFile.getSize();
                }

                public String getFileName() {
                    return kFile.getName();
                }

                public InputStream createInputStream() throws IOException {
                    return kFile.getInputStream();
                }
            };
            parts.add(new FilePart(key, fisPS));
        }
    }

    Part allParts[] = new Part[parts.size()];
    allParts = parts.toArray(allParts);

    method.setRequestEntity(new MultipartRequestEntity(allParts, method.getParams()));

    return method;
}

From source file:com.borhan.client.BorhanClientBase.java

private PostMethod getPostMultiPartWithFiles(PostMethod method, BorhanParams kparams, BorhanFiles kfiles) {

    String boundary = "---------------------------" + System.currentTimeMillis();
    List<Part> parts = new ArrayList<Part>();
    parts.add(new StringPart(HttpMethodParams.MULTIPART_BOUNDARY, boundary));

    parts.add(new StringPart("json", kparams.toString()));

    for (String key : kfiles.keySet()) {
        final BorhanFile kFile = kfiles.get(key);
        parts.add(new StringPart(key, "filename=" + kFile.getName()));
        if (kFile.getFile() != null) {
            // use the file
            File file = kFile.getFile();
            try {
                parts.add(new FilePart(key, file));
            } catch (FileNotFoundException e) {
                // TODO this sort of leaves the submission in a weird
                // state... -AZ
                if (logger.isEnabled())
                    logger.error("Exception while iterating over kfiles", e);
            }/*from  www .  j  a  v a 2s.  co  m*/
        } else {
            // use the input stream
            PartSource fisPS = new PartSource() {
                public long getLength() {
                    return kFile.getSize();
                }

                public String getFileName() {
                    return kFile.getName();
                }

                public InputStream createInputStream() throws IOException {
                    return kFile.getInputStream();
                }
            };
            parts.add(new FilePart(key, fisPS));
        }
    }

    Part allParts[] = new Part[parts.size()];
    allParts = parts.toArray(allParts);

    method.setRequestEntity(new MultipartRequestEntity(allParts, method.getParams()));

    return method;
}