package com.home.upload;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List; import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity implements OnClickListener {
private Button uploadBtn; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
uploadBtn = (Button) findViewById(R.id.main_btn);
uploadBtn.setOnClickListener(this);
} @Override
public void onClick(View v) {
if (v == uploadBtn) {
final List<String> list = new ArrayList<String>();
list.add("/storage/sdcard0/updateAdtech/orgpic/1.png");
list.add("/storage/sdcard0/updateAdtech/orgpic/2.png");
new Thread() {
public void run() {
postMethod("just test", "sdcard/image/a.amr", list);
};
}.start();
}
} /**
*
* @Description: 上传方法
*
* @param audioPath
* 上传音频文件地址 例:sdcard/image/a.amr
*
* @param text
* 上传文本的值
*
* @param imageUrlList
* 图片地址的集合 例:sdcard/image/a.jpg, sdcard/image/b.jpg
*
* @return void
*/ private synchronized void postMethod(String text, String audioPath,
List<String> imageUrlList) {
try {
String[] filePath = new String[imageUrlList.size()];
int size = imageUrlList.size();
for (int i = 0; i < size; i++) {
filePath[i] = imageUrlList.get(i);
}
// 链接超时,请求超时设置
BasicHttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10 * 1000);
HttpConnectionParams.setSoTimeout(httpParams, 10 * 1000); // 请求参数设置
HttpClient client = new DefaultHttpClient(httpParams);
HttpPost post = new HttpPost(
"http://service.ireadhome.com/api/Upload/Image");
MultipartEntity entity = new MultipartEntity();
// 上传 文本, 转换编码为utf-8 其中"text" 为字段名,
// 后边new StringBody(text,
// Charset.forName(CHARSET))为参数值,其实就是正常的值转换成utf-8的编码格式
entity.addPart("text",
new StringBody(text, Charset.forName("UTF-8")));
// 上传多个文本可以在此处添加上边代码,修改字段和值即可 // 上传音频文件
entity.addPart("audio",
new FileBody(new File(audioPath), "audio/*"));
// 上传图片
for (String p : filePath) {
entity.addPart("fileimg", new FileBody(new File(p), "image/*"));
}
post.setEntity(entity);
HttpResponse resp = client.execute(post);
// 获取回调值
System.out.println("Response:"
+ EntityUtils.toString(resp.getEntity()));
System.out.println("StatusCode:"
+ resp.getStatusLine().getStatusCode());
} catch (Exception e) {
e.printStackTrace();
}
}
}

使用MultipartEntity对文字、图片、视频进行综合上传的更多相关文章

  1. 使用Spring和JQuery实现视频文件的上传和播放

    Spring MVC可以很方便用户进行WEB应用的开发,实现Model.View和Controller的分离,再结合Spring boot可以很方便.轻量级部署WEB应用,这里为大家介绍如何使用Spr ...

  2. 使用html5 FileReader获取图片,并异步上传到服务器(不使用iframe)

    使用html5 FileReader获取图片,并异步上传到服务器(不使用iframe)   原理: 1.使用FileReader 读取图片的base64编码 2.使用ajax,把图片的base64编码 ...

  3. #添加图片,最多只能上传9张.md

    #添加图片,最多只能上传9张.md 前端页面: ```javascript <form id="imgForm" enctype="multipart/form-d ...

  4. 适应各浏览器图片裁剪无刷新上传jQuery插件(转)

    看到一篇兼容性很强的图片无刷新裁剪上传的帖子,感觉很棒.分享下!~ 废话不多说,上效果图. 一.首先建立如下的一个page <!DOCTYPE html> <html xmlns=& ...

  5. puzz: 图片和表单上传的不一致问题

    1.    方向1 用户提交表单, 图片和表单同步上传.(由同一服务器处理, 服务器压力大. 没有分离) 2.    方向2 图片和表单分开上传. 如图片访问ftp,表单提交后台(图片和后台分离) 2 ...

  6. 【Android实战】----基于Retrofit实现多图片/文件、图文上传

    本文代码详见:https://github.com/honghailiang/RetrofitUpLoadImage 一.再次膜拜下Retrofit Retrofit不管从性能还是使用方便性上都非常屌 ...

  7. 使用html5 FileReader获取图片,并异步上传到server(不使用iframe)

    使用html5 FileReader获取图片,并异步上传到server(不使用iframe) 原理: 1.使用FileReader 读取图片的base64编码 2.使用ajax.把图片的base64编 ...

  8. [.NET]c#.net程序中使用ffmpeg.exe来处理视频并生成上传视频的截图

    添加如下前台代码: <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Butto ...

  9. h5实现本地图片或文件的上传

    首先放一个今天学到的小demo: <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

随机推荐

  1. 在vi 按了Ctrl s 之后..

    习惯了在windows下写程序,也习惯了按ctrl+s 保存代码,在用vi的时候,也习惯性的按了ctrl+s 然后vi终端就像卡住了一样. 原来: ctrl+s 终止屏幕输出(即停止回显),你敲的依然 ...

  2. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array 带权并查集

    C. Destroying Array 题目连接: http://codeforces.com/contest/722/problem/C Description You are given an a ...

  3. 精益软件研发的秘密 IT大咖说 - 大咖干货,不再错过

    精益软件研发的秘密 IT大咖说 - 大咖干货,不再错过   http://www.itdks.com/dakashuo/new/dakalive/detail/3662

  4. 用 consul + consul-template + registrator + nginx 打造真正可动态扩展的服务架构

        https://mp.weixin.qq.com/s?src=3&timestamp=1503654544&ver=1&signature=UcJdgd4vgt*3AR ...

  5. request.getRequestDispatcher("").forward()中文乱码

    即使jsp页面的编码已设为“UTF-8”,有中文的地方还是会出现乱码,但是用response.sendRedirect不会出现此问题. 解决方案一: 不使用PrintWriter out=respon ...

  6. 巧用CSS3 :target 伪类制作Dropdown下拉菜单(无JS)

    :target 是CSS3 中新增的一个伪类,用以匹配当前页面的URI中某个标志符的目标元素(比如说当前页面URL下添加#comment就会定位到id=“comment”的位置,俗称锚).CSS3 为 ...

  7. BZOJ 1207 DP

    打一次鼹鼠必然是从曾经的某一次打鼹鼠转移过来的 以打每一个鼹鼠时的最优解为DP方程 #include<iostream> #include<cstdio> #include&l ...

  8. Parallel Programming--perfbook

    https://www.kernel.org/pub/linux/kernel/people/paulmck/perfbook/perfbook.html

  9. jQuery $('div>ul') $('div ul'

    $('div>ul')是<div>的直接后代里找<ul>: 而$('div ul')是在<div>的所有后代里找<ul>.

  10. Revit API过滤元素类别(FamilySymbol与FamilyInstance)

    仅OfCategory()过滤的元素包含系统FamilySymbolOfClass(typeof(FamilyInstance))过滤出来文档中族实例. ;         ;         ;   ...