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

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

Introduction

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

Prototype

public FilePart(String paramString1, String paramString2, File paramFile, String paramString3,
            String paramString4) throws FileNotFoundException 

Source Link

Usage

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 w  w  w . j a v  a  2  s .com*/
        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();
}

From source file:com.jmeter.alfresco.utils.HttpUtils.java

/**
 * Document upload./*from  w ww  .j av a  2s.  co m*/
 *
 * @param docFileObj the doc file obj
 * @param authTicket the auth ticket
 * @param uploadURI the upload uri
 * @param siteID the site id
 * @param uploadDir the upload dir
 * @return the string
 * @throws IOException Signals that an I/O exception has occurred.
 */
public String documentUpload(final File docFileObj, final String authTicket, final String uploadURI,
        final String siteID, final String uploadDir) throws IOException {

    String uploadResponse = Constants.EMPTY;
    PostMethod postRequest = null;
    try {
        final String uploadURL = getFileUploadURL(uploadURI, authTicket);
        LOG.info("documentUpload() | Upload URL: " + uploadURL);

        final HttpClient httpClient = new HttpClient();
        postRequest = new PostMethod(uploadURL);
        final String mimeType = getMimeType(docFileObj);
        final String docName = docFileObj.getName();
        LOG.debug("documentUpload() | Uploading document: " + docName + " , content-type: " + mimeType);

        final Part[] parts = { new FilePart("filedata", docName, docFileObj, mimeType, null),
                new StringPart("filename", docName), new StringPart("overwrite", "true"),
                new StringPart("siteid", siteID),
                new StringPart("containerid", ConfigReader.getProperty(Constants.CONTAINER_ID)),
                new StringPart("uploaddirectory", uploadDir) };

        postRequest.setRequestEntity(new MultipartRequestEntity(parts, postRequest.getParams()));
        final int statusCode = httpClient.executeMethod(postRequest);
        uploadResponse = postRequest.getResponseBodyAsString();
        LOG.info("documentUpload() | Upload status: " + statusCode);
        LOG.debug("documentUpload() | Upload response: " + uploadResponse);
    } finally {
        if (postRequest != null) {
            //releaseConnection http connection
            postRequest.releaseConnection();
        }
    }
    return uploadResponse;
}

From source file:net.sf.antcontrib.net.httpclient.PostMethodTask.java

protected void configureMethod(HttpMethodBase method) {
    PostMethod post = (PostMethod) method;

    if (parts.size() == 1 && !multipart) {
        Object part = parts.get(0);
        if (part instanceof FilePartType) {
            FilePartType filePart = (FilePartType) part;
            try {
                stream = new FileInputStream(filePart.getPath().getAbsolutePath());
                post.setRequestEntity(new InputStreamRequestEntity(stream, filePart.getPath().length(),
                        filePart.getContentType()));
            } catch (IOException e) {
                throw new BuildException(e);
            }/*from w  w  w.  java  2  s .  c om*/
        } else if (part instanceof TextPartType) {
            TextPartType textPart = (TextPartType) part;
            try {
                post.setRequestEntity(new StringRequestEntity(textPart.getValue(), textPart.getContentType(),
                        textPart.getCharSet()));
            } catch (UnsupportedEncodingException e) {
                throw new BuildException(e);
            }
        }
    } else if (!parts.isEmpty()) {
        Part partArray[] = new Part[parts.size()];
        for (int i = 0; i < parts.size(); i++) {
            Object part = parts.get(i);
            if (part instanceof FilePartType) {
                FilePartType filePart = (FilePartType) part;
                try {
                    partArray[i] = new FilePart(filePart.getPath().getName(), filePart.getPath().getName(),
                            filePart.getPath(), filePart.getContentType(), filePart.getCharSet());
                } catch (FileNotFoundException e) {
                    throw new BuildException(e);
                }
            } else if (part instanceof TextPartType) {
                TextPartType textPart = (TextPartType) part;
                partArray[i] = new StringPart(textPart.getName(), textPart.getValue(), textPart.getCharSet());
                ((StringPart) partArray[i]).setContentType(textPart.getContentType());
            }
        }
        MultipartRequestEntity entity = new MultipartRequestEntity(partArray, post.getParams());
        post.setRequestEntity(entity);
    }
}

From source file:org.alfresco.repo.web.scripts.custommodel.CustomModelImportTest.java

public PostRequest buildMultipartPostRequest(File file) throws IOException {
    Part[] parts = { new FilePart("filedata", file.getName(), file, "application/zip", null) };

    MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, new HttpMethodParams());

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    multipartRequestEntity.writeRequest(os);

    PostRequest postReq = new PostRequest(UPLOAD_URL, os.toByteArray(),
            multipartRequestEntity.getContentType());
    return postReq;
}

From source file:org.alfresco.rest.api.tests.util.MultiPartBuilder.java

public MultiPartRequest build() throws IOException {
    List<Part> parts = new ArrayList<>();

    if (fileData != null) {
        FilePart fp = new FilePart("filedata", fileData.getFileName(), fileData.getFile(),
                fileData.getMimetype(), null);
        // Get rid of the default values added upon FilePart instantiation
        fp.setCharSet(fileData.getEncoding());
        fp.setContentType(fileData.getMimetype());
        parts.add(fp);//  w ww  .  j  ava  2s .c om
        addPartIfNotNull(parts, "name", fileData.getFileName());
    }
    addPartIfNotNull(parts, "relativepath", relativePath);
    addPartIfNotNull(parts, "updatenoderef", updateNodeRef);
    addPartIfNotNull(parts, "description", description);
    addPartIfNotNull(parts, "contenttype", contentTypeQNameStr);
    addPartIfNotNull(parts, "aspects", getCommaSeparated(aspects));
    addPartIfNotNull(parts, "majorversion", majorVersion);
    addPartIfNotNull(parts, "overwrite", overwrite);
    addPartIfNotNull(parts, "autorename", autoRename);
    addPartIfNotNull(parts, "nodetype", nodeType);
    addPartIfNotNull(parts, "renditions", getCommaSeparated(renditionIds));

    if (!properties.isEmpty()) {
        for (Entry<String, String> prop : properties.entrySet()) {
            parts.add(new StringPart(prop.getKey(), prop.getValue()));
        }
    }

    MultipartRequestEntity req = new MultipartRequestEntity(parts.toArray(new Part[parts.size()]),
            new HttpMethodParams());

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    req.writeRequest(os);

    return new MultiPartRequest(os.toByteArray(), req.getContentType(), req.getContentLength());
}

From source file:slash.navigation.rest.MultipartRequest.java

public void addFile(String name, File value) throws IOException {
    parts.add(/*www . j  a va 2  s  .com*/
            new FilePart(name, Helper.encodeUri(value.getName()), value, "application/octet-stream", "UTF-8"));
}