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、  具体操作

 

添加权限







布局:增加两个UI组件



    

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 params = new HashMap();
	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中

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



你可能感兴趣的:(Android应用开发初步)