android multipartentity 怎么上传参数,Android使用MultipartEntityBuilder实现类似form表单提交方式的文件上传...

最近在做 Android 端文件上传,要求采用 form 表单的方式提交,项目使用的 afinal 框架有文件上传功能,但是始终无法与php写的服务端对接上,无法上传成功。读源码发现:afinal 使用了某大神写的 MultipartEntity.java 生成 form 表单内容,然而生成的内容格式不够标准,而且还存在诸多问题,如:首先将所有文件读入到内存,再生成字节流写入到 socket。那么问题来了:如果是几百MB的文件怎么办?

几番搜索,受到 这篇文章(已被我转载,但是示例代码已过期)的启发,我辗转找到了 Apache 源码 httpcomponents-client-4.3.6-src.zip,在一个示例里面发现了一个重要的组件 MultipartEntityBuilder, 可以生成 form 表单格式的 HttpEntity, 有了 HttpEntity, 无论你是什么 http 框架,应该都可以使用。

不知道怎么使用?like this:

HttpPost httppost = new HttpPost(url);

...

final HttpEntity entity = makeMultipartEntity(params, files);

httppost.addHeader(entity.getContentType());

//httppost.addHeader(entity.getContentEncoding());    //null

httppost.setEntity(entity);

HttpResponse response = getHttpClient().execute(httppost);

...

private static HttpClient mClient;

private static HttpClient getHttpClient() {

if(mClient == null) {

//if(Build.VERSION.SDK_INT >= 9);    //将不走本类的Case,基于HttpURLConnection

if(Build.VERSION.SDK_INT >= 8) {

mClient = AndroidHttpClient.newInstance(getUserAgent());

}else {

mClient = new DefaultHttpClient();

}

}

return mClient;

}

MultipartEntityBuilder 用法整理如下:

需要用到 httpcomponents-client-4.3.6-bin.zip 中的 httpmime-4.3.6.jar 和 httpcore-4.3.3.jar

public static HttpEntity makeMultipartEntity(List params, final Map files) {

MultipartEntityBuilder builder = MultipartEntityBuilder.create();

builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);//如果有SocketTimeoutException等情况,可修改这个枚举

//builder.setCharset(Charset.forName("UTF-8"));//不要用这个,会导致服务端接收不到参数

if (params != null && params.size() > 0) {

for (NameValuePair p : params) {

builder.addTextBody(p.getName(), p.getValue(), ContentType.TEXT_PLAIN.withCharset("UTF-8"));

}

}

if (files != null && files.size() > 0) {

Set> entries = files.entrySet();

for (Entry entry : entries) {

builder.addPart(entry.getKey(), new FileBody(entry.getValue()));

}

}

return builder.build();

}

/*

* ====================================================================

* Licensed to the Apache Software Foundation (ASF) under one

* or more contributor license agreements.  See the NOTICE file

* distributed with this work for additional information

* regarding copyright ownership.  The ASF licenses this file

* to you under the Apache License, Version 2.0 (the

* "License"); you may not use this file except in compliance

* with the License.  You may obtain a copy of the License at

*

*   http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing,

* software distributed under the License is distributed on an

* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY

* KIND, either express or implied.  See the License for the

* specific language governing permissions and limitations

* under the License.

* ====================================================================

*

* This software consists of voluntary contributions made by many

* individuals on behalf of the Apache Software Foundation.  For more

* information on the Apache Software Foundation, please see

* .

*

*/

package org.apache.http.examples.entity.mime;

import java.io.File;

import org.apache.http.HttpEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.ContentType;

import org.apache.http.entity.mime.MultipartEntityBuilder;

import org.apache.http.entity.mime.content.FileBody;

import org.apache.http.entity.mime.content.StringBody;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

/**

* Example how to use multipart/form encoded POST request.

*/

public class ClientMultipartFormPost {

public static void main(String[] args) throws Exception {

if (args.length != 1)  {

System.out.println("File path not given");

System.exit(1);

}

CloseableHttpClient httpclient = HttpClients.createDefault();

try {

HttpPost httppost = new HttpPost("http://localhost:8080" +

"/servlets-examples/servlet/RequestInfoExample");

FileBody bin = new FileBody(new File(args[0]));

StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);

HttpEntity reqEntity = MultipartEntityBuilder.create()

.addPart("bin", bin)

.addPart("comment", comment)

.build();

httppost.setEntity(reqEntity);

System.out.println("executing request " + httppost.getRequestLine());

CloseableHttpResponse response = httpclient.execute(httppost);

try {

System.out.println("----------------------------------------");

System.out.println(response.getStatusLine());

HttpEntity resEntity = response.getEntity();

if (resEntity != null) {

System.out.println("Response content length: " + resEntity.getContentLength());

}

EntityUtils.consume(resEntity);

} finally {

response.close();

}

} finally {

httpclient.close();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值