Java Examples for com.google.api.client.http.MultipartContent

The following java examples will help you to understand the usage of com.google.api.client.http.MultipartContent. These source code samples are taken from different open source projects.

Example 1
Project: java-asana-master  File: Attachments.java View source code
/**
     * Upload a file and attach it to a task
     *
     * @param task        Globally unique identifier for the task.
     * @param fileContent Content of the file to be uploaded
     * @param fileName    Name of the file to be uploaded
     * @param fileType    MIME type of the file to be uploaded
     * @return Request object
     */
public ItemRequest<Attachment> createOnTask(String task, InputStream fileContent, String fileName, String fileType) {
    MultipartContent.Part part = new MultipartContent.Part().setContent(new InputStreamContent(fileType, fileContent)).setHeaders(new HttpHeaders().set("Content-Disposition", // TODO: escape fileName?
    String.format("form-data; name=\"file\"; filename=\"%s\"", fileName)));
    MultipartContent content = new MultipartContent().setMediaType(new HttpMediaType("multipart/form-data").setParameter("boundary", UUID.randomUUID().toString())).addPart(part);
    String path = String.format("/tasks/%s/attachments", task);
    return new ItemRequest<Attachment>(this, Attachment.class, path, "POST").data(content);
}
Example 2
Project: project-l-master  File: GyazoUploader.java View source code
@Override
public Uri loadInBackground() {
    try {
        final GenericUrl url = new GenericUrl("https://upload.gyazo.com");
        url.appendRawPath("/upload.cgi");
        final HttpContent content;
        {
            final MultipartContent data = new MultipartContent();
            data.setMediaType(new HttpMediaType("multipart/form-data"));
            data.setBoundary("__END_OF_PART__");
            data.addPart(new MultipartContent.Part(new HttpHeaders().set("Content-Disposition", "form-data; name=\"id\""), new EmptyContent()));
            data.addPart(new MultipartContent.Part(new HttpHeaders().set("Content-Disposition", "form-data; name=\"imagedata\"; filename=\"gyazo.com\""), new ByteArrayContent(null, this.readBytes())));
            content = data;
        }
        final HttpRequestFactory factory = transport.createRequestFactory(new HttpRequestInitializer() {

            @Override
            public void initialize(HttpRequest request) throws IOException {
                request.setLoggingEnabled(true);
            }
        });
        final HttpRequest request = factory.buildPostRequest(url, content);
        final HttpResponse response = request.execute();
        Log.i("gyazo", String.format("code=%d, content=%s", response.getStatusCode(), response.toString()));
        if (response.getStatusCode() >= 200 && response.getStatusCode() < 300) {
            ByteArrayOutputStream out = null;
            try {
                out = new ByteArrayOutputStream();
                IOUtils.copy(response.getContent(), out);
                return Uri.parse(out.toString());
            } finally {
                response.disconnect();
                if (out != null) {
                    out.close();
                }
            }
        } else {
            return null;
        }
    } catch (IOException e) {
        Log.e("gyazo", "Oops", e);
        return null;
    }
}
Example 3
Project: PicasaUploadSample-master  File: PicasaClient.java View source code
public PhotoEntry executeInsertPhotoEntryWithMetadata(PhotoEntry photo, PicasaUrl albumFeedUrl, InputStreamContent content, GmlPoint point) throws IOException {
    GeorssWhere georss = new GeorssWhere();
    georss.point = point;
    photo.georssWhere = georss;
    HttpRequest request = getRequestFactory().buildPostRequest(albumFeedUrl, null);
    AtomContent atomContent = AtomContent.forEntry(DICTIONARY, photo);
    request.setContent(new MultipartContent().setContentParts(Arrays.asList(atomContent, content)));
    request.getHeaders().setMimeVersion("1.0");
    return execute(request).parseAs(PhotoEntry.class);
}
Example 4
Project: google-api-java-client-master  File: MediaHttpUploader.java View source code
/**
   * Direct Uploads the media.
   *
   * @param initiationRequestUrl The request URL where the initiation request will be sent
   * @return HTTP response
   */
private HttpResponse directUpload(GenericUrl initiationRequestUrl) throws IOException {
    updateStateAndNotifyListener(UploadState.MEDIA_IN_PROGRESS);
    HttpContent content = mediaContent;
    if (metadata != null) {
        content = new MultipartContent().setContentParts(Arrays.asList(metadata, mediaContent));
        initiationRequestUrl.put("uploadType", "multipart");
    } else {
        initiationRequestUrl.put("uploadType", "media");
    }
    HttpRequest request = requestFactory.buildRequest(initiationRequestMethod, initiationRequestUrl, content);
    request.getHeaders().putAll(initiationHeaders);
    // We do not have to do anything special here if media content length is unspecified because
    // direct media upload works even when the media content length == -1.
    HttpResponse response = executeCurrentRequest(request);
    boolean responseProcessed = false;
    try {
        if (isMediaLengthKnown()) {
            totalBytesServerReceived = getMediaContentLength();
        }
        updateStateAndNotifyListener(UploadState.MEDIA_COMPLETE);
        responseProcessed = true;
    } finally {
        if (!responseProcessed) {
            response.disconnect();
        }
    }
    return response;
}
Example 5
Project: downlords-faf-client-master  File: FafApiAccessorImpl.java View source code
@Override
public void uploadMap(Path file, boolean isRanked, ByteCountListener listener) throws IOException {
    MultipartContent multipartContent = createFileMultipart(file, listener);
    multipartContent.addPart(new MultipartContent.Part(new HttpHeaders().set("Content-Disposition", "form-data; name=\"metadata\";"), new JsonHttpContent(jsonFactory, new GenericJson() {

        {
            set("is_ranked", isRanked);
        }
    })));
    postMultipart("/maps/upload", multipartContent);
}
Example 6
Project: api-client-java-master  File: MediaHttpUploader.java View source code
/**
   * Direct Uploads the media.
   *
   * @param initiationRequestUrl The request URL where the initiation request will be sent
   * @return HTTP response
   */
private HttpResponse directUpload(GenericUrl initiationRequestUrl) throws IOException {
    updateStateAndNotifyListener(UploadState.MEDIA_IN_PROGRESS);
    HttpContent content = mediaContent;
    if (metadata != null) {
        content = new MultipartContent().setContentParts(Arrays.asList(metadata, mediaContent));
        initiationRequestUrl.put("uploadType", "multipart");
    } else {
        initiationRequestUrl.put("uploadType", "media");
    }
    HttpRequest request = requestFactory.buildRequest(initiationRequestMethod, initiationRequestUrl, content);
    request.getHeaders().putAll(initiationHeaders);
    // We do not have to do anything special here if media content length is unspecified because
    // direct media upload works even when the media content length == -1.
    HttpResponse response = executeCurrentRequest(request);
    boolean responseProcessed = false;
    try {
        if (isMediaLengthKnown()) {
            totalBytesServerReceived = getMediaContentLength();
        }
        updateStateAndNotifyListener(UploadState.MEDIA_COMPLETE);
        responseProcessed = true;
    } finally {
        if (!responseProcessed) {
            response.disconnect();
        }
    }
    return response;
}
Example 7
Project: drive-uploader-master  File: MediaHttpUploader.java View source code
/**
	 * Direct Uploads the media.
	 *
	 * @param initiationRequestUrl
	 *            The request URL where the initiation request will be sent
	 * @return HTTP response
	 */
private HttpResponse directUpload(GenericUrl initiationRequestUrl) throws IOException {
    updateStateAndNotifyListener(UploadState.MEDIA_IN_PROGRESS);
    HttpContent content = mediaContent;
    if (metadata != null) {
        content = new MultipartContent().setContentParts(Arrays.asList(metadata, mediaContent));
        initiationRequestUrl.put("uploadType", "multipart");
    } else {
        initiationRequestUrl.put("uploadType", "media");
    }
    HttpRequest request = requestFactory.buildRequest(initiationRequestMethod, initiationRequestUrl, content);
    request.getHeaders().putAll(initiationHeaders);
    // We do not have to do anything special here if media content length is
    // unspecified because
    // direct media upload works even when the media content length == -1.
    HttpResponse response = executeCurrentRequest(request);
    boolean responseProcessed = false;
    try {
        if (isMediaLengthKnown()) {
            totalBytesServerReceived = getMediaContentLength();
        }
        updateStateAndNotifyListener(UploadState.MEDIA_COMPLETE);
        responseProcessed = true;
    } finally {
        if (!responseProcessed) {
            response.disconnect();
        }
    }
    return response;
}