android 文件传输协议,android文件传输协议.doc

android文件传输协议

篇一:Android之使用Http协议实现文件上传功能

注意一般使用Http协议上传的文件都比较小,一般是小于2M

这里示例是上传一个小的MP3文件

1.主Activity:MainActivity.java

?

?

?

?

?

?

?

?

? public class MainActivity extends Activity { private static final String TAG = MainActivity; private EditText timelengthText; private EditText titleText; private EditText videoText; @Override public void onCreate(Bundle savedInstanceState)

?? {

?? super.onCreate(savedInstanceState);

?? setContentView(R.layout.main);

?? //提交上传按钮

?? Button button = (Button) this.findViewById(R.id.button);

?? timelengthText = (EditText) this.findViewById(R.id.timelength);?? videoText = (EditText) this.findViewById(R.id.video);

?? titleText = (EditText) this.findViewById(R.id.title);

?? button.setOnClickListener(new View.OnClickListener()

?? {

?? @Override

?? public void onClick(View v)

?? {

?? String title = titleText.getText().toString();

?? String timelength = timelengthText.getText().toString();?? Map

?? params.put(title, title);

?? params.put(timelength, timelength);

?? try

?? {

??//得到SDCard的目录

??File uploadFile = new

File(Environment.getExternalStorageDirectory(), videoText.getText().toString());??//上传音频文件

??FormFile formfile = new FormFile(02.mp3, uploadFile, video, audio/mpeg);

??

SocketHttpRequester.post(00:8080/videoweb/video/manage.do, params, formfile);

??Toast.makeText(MainActivity.this, R.string.success,

1).show();

?? }

?? catch (Exception e)

?? {

??Toast.makeText(MainActivity.this, R.string.error,

1).show();

??Log.e(TAG, e.toString());

?? }

?? }

??});

?? }

?? }

2.上传工具类,注意里面构造协议字符串需要根据不同的提交表单来处理

?? public class SocketHttpRequester

?? {

?? /**

??* 发送xml数据

??* @param path 请求地址

??* @param xml xml数据

??* @param encoding 编码

??* @return

??* @throws Exception

??*/

?? public static byte[] postXml(String path, String xml, String encoding) throws Exception{

??byte[] data =

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值