使用GoogleHttpClient发送的multipart / form-data的 [英] Using GoogleHttpClient to send multipart/form-data

查看:1127
本文介绍了使用GoogleHttpClient发送的multipart / form-data的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用谷歌的-HTTP客户端的Java来处理Android上的HTTP连接。

I am using the google-http-client for java to handle http connections on Android.

一切都是HunkyDori它迄今为止的直到试图发送一个的multipart / form-data的内容。

Everything is HunkyDori with it thus far until trying to send a multipart/form-data Content.

我有点卡住,我想适应的<一个href=\"https://$c$c.google.com/p/google-http-java-client/source/browse/google-http-client/src/main/java/com/google/api/client/http/MultipartContent.java?name=1.14.1-beta\"相对=nofollow> MultipartContent 类来创建我自己的版本 MultipartFormContent

I am a little bit stuck, I am thinking about adapting the MultipartContent class to create my own version of MultipartFormContent.

我需要一个版本的<一个href=\"http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntity.html#addPart%28java.lang.String,%20org.apache.http.entity.mime.content.ContentBody%29\"相对=nofollow> MultipartEntity 这将用途不同的工作,如:

I need a version of MultipartEntity which would effectivly work like:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);

FileBody bin = new FileBody(new File(fileName));
StringBody comment = new StringBody("Filename: " + fileName);

MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("bin", bin);
reqEntity.addPart("comment", comment);
httppost.setEntity(reqEntity);

HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();

从阅读周围,的multipart /相对的multipart /表单之间的唯一区别是,一个加打印出来的时候名字?

From reading around, the only difference between, multipart/relative and multipart/form is that one adds the name when printing out?

任何人有关于适应<一个任何想法href=\"https://$c$c.google.com/p/google-http-java-client/source/browse/google-http-client/src/main/java/com/google/api/client/http/MultipartContent.java?name=1.14.1-beta\"相对=nofollow> MultipartContent 类,以支持姓名字段。

Anyone got any ideas about adapting the MultipartContent class to support the 'name' field.

问候。

推荐答案

好吧,我创建了基于该MultipartContent类以下类:

Ok, I created the following class based off of the MultipartContent class:

/**
 * Serializes MIME multipart content to the form-data standard. Adapated from {@link MultipartContent}
 * <p/>
 * <p>
 * By default the media type is {@code "multipart/form-data; boundary=__END_OF_PART__"}, but this may
 * be customized by calling {@link #setMediaType(HttpMediaType)}, {@link #getMediaType()}, or
 * {@link #setBoundary(String)}.
 * </p>
 * <p/>
 * <p>
 * Implementation is not thread-safe.
 * </p>
 *
 * @author Christopher Jenkins
 * @since 1.15
 */
public class MultipartFormContent extends AbstractHttpContent
{

    static final String NEWLINE = "\r\n";
    private static final String TWO_DASHES = "--";
    /**
     * Parts of the HTTP multipart request.
     */
    private ArrayList<Part> parts = new ArrayList<Part>();

    public MultipartFormContent()
    {
        super(new HttpMediaType("multipart/form-data").setParameter("boundary", "__END_OF_PART__"));
    }

    public void writeTo(OutputStream out) throws IOException
    {
        Writer writer = new OutputStreamWriter(out, getCharset());
        String boundary = getBoundary();

        //TODO use http headers again like MultipartContent
        HttpHeaders headers;
        String contentDisposition = null;
        HttpContent content = null;
        StreamingContent streamingContent = null;
        for (Part part : parts)
        {
            // analyze the headers
            headers = new HttpHeaders().setAcceptEncoding(null);
            if (part.headers != null)
                headers.fromHttpHeaders(part.headers);
            headers.setContentEncoding(null)
                    .setUserAgent(null)
                    .setContentType(null)
                    .setContentLength(null)
                    .set("Content-Transfer-Encoding", null);

            // Write disposition
            if (part.getName() != null)
            {
                contentDisposition = String.format("form-data; name=\"%s\"", part.name);
                // Do we have a filename?
                // Then add to the content dispos
                if (part.filename != null)
                    contentDisposition += String.format("; filename=\"%s\"", part.filename);
                headers.set("Content-Disposition", contentDisposition);
            }

            // analyze the content
            content = part.content;
            streamingContent = null;
            if (content != null)
            {
                headers.setContentType(content.getType());
                headers.set("Content-Transfer-Encoding", Arrays.asList("binary"));
                final HttpEncoding encoding = part.encoding;
                if (encoding == null)
                    streamingContent = content;
                else
                {
                    headers.setContentEncoding(encoding.getName());
                    streamingContent = new HttpEncodingStreamingContent(content, encoding);
                }
            }

            // write separator
            writer.write(TWO_DASHES);
            writer.write(boundary);
            writer.write(NEWLINE);
            // Write Headers
            HttpHeaders.serializeHeadersForMultipartRequests(headers, null, null, writer);
            // write content
            if (streamingContent != null)
            {
                writer.write(NEWLINE);
                writer.flush();
                streamingContent.writeTo(out);
                writer.write(NEWLINE);
            }
        }
        // write end separator
        writer.write(TWO_DASHES);
        writer.write(boundary);
        writer.write(TWO_DASHES);
        writer.write(NEWLINE);
        writer.flush();
    }

    @Override
    public boolean retrySupported()
    {
        for (Part part : parts)
        {
            if (!part.content.retrySupported())
            {
                return false;
            }
        }
        return true;
    }

    @Override
    public MultipartFormContent setMediaType(HttpMediaType mediaType)
    {
        super.setMediaType(mediaType);
        return this;
    }

    /**
     * Returns an unmodifiable view of the parts of the HTTP multipart request.
     */
    public final Collection<Part> getParts()
    {
        return Collections.unmodifiableCollection(parts);
    }

    /**
     * Sets the parts of the HTTP multipart request.
     * <p/>
     * <p>
     * Overriding is only supported for the purpose of calling the super implementation and changing
     * the return type, but nothing else.
     * </p>
     */
    public MultipartFormContent setParts(Collection<Part> parts)
    {
        this.parts = new ArrayList<Part>(parts);
        return this;
    }

    /**
     * Adds an HTTP multipart part.
     * <p/>
     * <p>
     * Overriding is only supported for the purpose of calling the super implementation and changing
     * the return type, but nothing else.
     * </p>
     */
    public MultipartFormContent addPart(Part part)
    {
        parts.add(Preconditions.checkNotNull(part));
        return this;
    }

    /**
     * Returns the boundary string to use.
     */
    public final String getBoundary()
    {
        return getMediaType().getParameter("boundary");
    }

    /**
     * Sets the boundary string to use.
     * <p/>
     * <p>
     * Defaults to {@code "__END_OF_PART__"}.
     * </p>
     * <p/>
     * <p>
     * Overriding is only supported for the purpose of calling the super implementation and changing
     * the return type, but nothing else.
     * </p>
     */
    public MultipartFormContent setBoundary(String boundary)
    {
        getMediaType().setParameter("boundary", Preconditions.checkNotNull(boundary));
        return this;
    }

    /**
     * Single part of a multi-part request.
     * <p/>
     * <p>
     * Implementation is not thread-safe.
     * </p>
     */
    public static final class Part
    {

        /**
         * Name of this FormPart
         */
        private String name;
        /**
         * FileName of the File being uploaded or {@code null} for none.
         */
        private String filename;
        /**
         * HTTP header or {@code null} for none.
         */
        private HttpHeaders headers;
        /**
         * HTTP content or {@code null} for none.
         */
        private HttpContent content;
        /**
         * HTTP encoding or {@code null} for none.
         */
        private HttpEncoding encoding;

        public Part()
        {
            this(null, null);
        }

        /**
         * @param headers HTTP headers or {@code null} for none
         * @param content HTTP content or {@code null} for none
         */
        public Part(String name, HttpContent content)
        {
            setName(name);
            setContent(content);
        }

        /**
         * Returns the HTTP content or {@code null} for none.
         */
        public HttpContent getContent()
        {
            return content;
        }

        /**
         * Sets the HTTP content or {@code null} for none.
         */
        public Part setContent(HttpContent content)
        {
            this.content = content;
            if (content instanceof FileContent)
            {
                final File file = ((FileContent) content).getFile();
                if (file != null && file.exists())
                {
                    setFilename(file.getName());
                }
            }
            return this;
        }

        public String getName()
        {
            return name;
        }

        public Part setName(final String name)
        {
            this.name = name;
            return this;
        }

        public String getFilename()
        {
            return filename;
        }

        /**
         * Set the HTTP form-part filename or null for none.
         *
         * @param filename
         */
        public Part setFilename(final String filename)
        {
            this.filename = filename;
            return this;
        }

        /**
         * Returns the HTTP headers or {@code null} for none.
         */
        public HttpHeaders getHeaders()
        {
            return headers;
        }

        /**
         * Sets the HTTP headers or {@code null} for none.
         */
        public Part setHeaders(final HttpHeaders headers)
        {
            this.headers = headers;
            return this;
        }

        /**
         * Returns the HTTP encoding or {@code null} for none.
         */
        public HttpEncoding getEncoding()
        {
            return encoding;
        }

        /**
         * Sets the HTTP encoding or {@code null} for none.
         */
        public Part setEncoding(HttpEncoding encoding)
        {
            this.encoding = encoding;
            return this;
        }
    }
}

这是为我工作,我使用针对Cloudinary。

This is working for me, I am using against Cloudinary.

这篇关于使用GoogleHttpClient发送的multipart / form-data的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆