newmultipartentity php,MultipartEntity使用,批量上传

try {

HttpClient httpClient = new DefaultHttpClient();

HttpPost postRequest = new HttpPost(URL);

httpClient.getParams().setParameter(

CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

File f = null;

FileBody fo = null;

MultipartEntity reqEntity = new MultipartEntity(

HttpMultipartMode.BROWSER_COMPATIBLE);

f = new File("/mnt/sdcard/a.png");

fo = new FileBody(f);

reqEntity.addPart("uploaded", fo);

Log.i("uploaded", "image added Parameter added");

postRequest.setEntity(reqEntity);

HttpResponse response = httpClient.execute(postRequest);

BufferedReader reader = new BufferedReader(new InputStreamReader(

response.getEntity().getContent(), "UTF-8"));

String sResponse;

StringBuilder s = new StringBuilder();

while ((sResponse = reader.readLine()) != null) {

s = s.append(sResponse);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

用于文件批量上传等MultipartEntity及http的使用

/**

* 发送文件和基础数据

*

* @param netpath

* @return

*/

public boolean sendForm(String netpath) {

String path = netpath;

boolean res = false;

try {

HttpPost httpPost = new HttpPost(path);

MultipartEntityBuilder entityBuilder = MultipartEntityBuilder

.create();

// 设置为浏览器兼容模式

entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

// 设置请求的编码格式

// entityBuilder.setCharset(Charset.forName(HTTP.UTF_8));

entityBuilder.setCharset(Charset.forName(HTTP.UTF_8));

for (KeyValue keyValue : beanData.getKeyvalues()) {

entityBuilder.addTextBody(

keyValue.getKey(),

keyValue.getValue(),

ContentType.create("text/plain",

Charset.forName(this.charset)));

}

for (int i = 0; i 

entityBuilder.addBinaryBody("file", new File(values.get(i)));

}

httpPost.setEntity(entityBuilder.build());

// httpPost.setHeader("Content-type", "text/html;charset=UTF-8");

HttpResponse response = new DefaultHttpClient().execute(httpPost);

if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

res = true;

resultString = EntityUtils.toString(response.getEntity(),

HTTP.UTF_8);

}

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return res;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值