Android应用开发之实现视频文件的上传

协议分析

l  运行服务器端web应用,

上传视频文件,同时用httpwatch捕获数据包:

POST /videoweb/video/manage.do HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://192.168.1.102:8080/videoweb/
Accept-Language: zh-cn,en-GB;q=0.5
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)
Content-Type: multipart/form-data; boundary=---------------------------7db4e3237099a
Accept-Encoding: gzip, deflate
Host: 192.168.1.102:8080
Content-Length: 3224599
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=D677BD29B65CD7D9A973767BEB945CCF

-----------------------------7db4e3237099a
Content-Disposition: form-data; name="method"

save
-----------------------------7db4e3237099a
Content-Disposition: form-data; name="name"

YMGM
-----------------------------7db4e3237099a
Content-Disposition: form-data; name="timelength"

1
-----------------------------7db4e3237099a
Content-Disposition: form-data; name="video"; filename="F:\2011_Android\14\YMGM.rm"
Content-Type: application/octet-stream

…
-----------------------------7dbcb1c10762--

注意:详细解释分割线的使用,包括分割线的定义,使用

定义:

boundary=---------------------------7db4e3237099a

分割线的定义和使用

---------------------------7db4e3237099a   //定义
-----------------------------7db4e3237099a   //实体参数分割线,前面比定义时多了两个“-”
-----------------------------7db4e3237099a--  //实体参数结束分割线,后面比定义时多了两个“-”

上传文件的类型描述(可以参看tomcat\conf\web.xml文件中的字典)

 

下面是关于实体内容中上传文件的基本信息描述:

Content-Disposition: form-data; name="video"; filename="F:\2011_Android\14\YMGM.rm"
Content-Type: application/octet-stream

注意数清楚回车和换行的个数

 

 

客户端应用的实现

1、  工具类FormFile

2、  工具类SocketHttpRequester

 

将上面用到的工具类复制到工程中

 

3、  具体操作

 

添加权限

<uses-permission android:name="android.permission.INTERNET" />
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

布局:增加两个UI组件

<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/videoFile" />

    <EditText
        android:id="@+id/videoFileEt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="YMGM.rm" />

VideoService:增加方法saveVideo

public static boolean saveVideo(String name, String timelength,
 File file) throws Exception{
	String path = 
"http://192.168.1.102:8080/videoweb/video/manage.do";
	Map<String, String> params = new HashMap<String, String>();
	params.put("name", name);
	params.put("timelength", timelength);
	params.put("method", "save"); 
	FormFile formfile = new FormFile(file.getName(), file, "video", "application/octet-stream");
	return SocketHttpRequester.post(path, params, formfile);
}

activity

package cn.class3g.video;
…
public class VideoClientActivity extends Activity implements OnClickListener {

	EditText nameEt, timeLengthEt, fileEt;
	Button saveBtn;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.save_video_info);
		findViews();
	}

	private void findViews() {
		nameEt = (EditText) this.findViewById(R.id.nameEt);
		timeLengthEt = (EditText) this.findViewById(R.id.timeLengthEt);
		fileEt = (EditText) this.findViewById(R.id.videoFileEt);
		saveBtn = (Button) this.findViewById(R.id.saveBtn);

		saveBtn.setOnClickListener(this);
	}

	public void onClick(View v) {

		String videoName = nameEt.getText().toString();
		String timeLength = timeLengthEt.getText().toString();
		String videoFile = fileEt.getText().toString();

		try {
			File file = new File(Environment.getExternalStorageDirectory(),
					videoFile);
			if (VideoService.saveVideo(videoName, timeLength, file)) {
				Toast.makeText(this, R.string.success, 1).show();
			} else {
				Toast.makeText(this, R.string.error, 1).show();
			}

		} catch (Exception e) {
			Log.e("TAG", e.toString());
			Toast.makeText(this, R.string.error, Toast.LENGTH_SHORT).show();
		}
	}
}

测试:

先把测试用的视频文件拖入模拟器的sd card中

运行程序,执行上传,查看服务器是否有被上传文件



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值