您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Android 实现文件上传功能

2025/3/25 18:27:11发布25次查看
文件上传在b/s应用中是一种十分常见的功能,那么在android平台下是否可以实现像b/s那样的文件上传功能呢?答案是肯定的。下面是一个模拟网站程序上传文件的例子。这里只写出了android部分的代码,服务器的代码没有贴出来,有需要完整上传功能代码的朋友可以跟我联系哟…
首先新建一个android工程,新建主启动activity:
mainactivity.java: package com.xzq.upload; import java.io.dataoutputstream; import java.io.fileinputstream; import java.io.inputstream; import java.net.httpurlconnection; import java.net.url; import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.textview; import android.widget.toast; public class mainactivity extends activity { private string newname = htys.mp3; //要上传的本地文件路径 private string uploadfile = /data/data/com.xzq/htys.mp3; //上传到服务器的指定位置 private string actionurl = http://192.168.100.100:8080/upload/upload.jsp; private textview mtextview1; private textview mtextview2; private button mbutton1; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); mtextview1 = (textview) findviewbyid(r.id.mytext2); mtextview1.settext(filepath:/n + uploadfile); mtextview2 = (textview) findviewbyid(r.id.mytext3); mtextview2.settext(uploadpath:/n + actionurl); /* 设定mbutton的onclick事件处理 */ mbutton1 = (button) findviewbyid(r.id.mybutton); mbutton1.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { uploadfile(); } }); } private void uploadfile() { string end = /r/n; string hyphens = --; string boundary = *****; try { url url = new url(actionurl); httpurlconnection con = (httpurlconnection) url.openconnection(); /* 允许input、output,不使用cache */ con.setdoinput(true); con.setdooutput(true); con.setusecaches(false); /* 设定传送的method=post */ con.setrequestmethod(post); /* setrequestproperty */ con.setrequestproperty(connection, keep-alive); con.setrequestproperty(charset, utf-8); con.setrequestproperty(content-type, multipart/form-data;boundary= + boundary); /* 设定dataoutputstream */ dataoutputstream ds = new dataoutputstream(con.getoutputstream()); ds.writebytes(hyphens + boundary + end); ds.writebytes(content-disposition: form-data; + name=/file1/;filename=/ + newname + / + end); ds.writebytes(end); /* 取得文件的fileinputstream */ fileinputstream fstream = new fileinputstream(uploadfile); /* 设定每次写入1024bytes */ int buffersize = 1024; byte[] buffer = new byte[buffersize]; int length = -1; /* 从文件读取数据到缓冲区 */ while ((length = fstream.read(buffer)) != -1) { /* 将数据写入dataoutputstream中 */ ds.write(buffer, 0, length); } ds.writebytes(end); ds.writebytes(hyphens + boundary + hyphens + end); fstream.close(); ds.flush(); /* 取得response内容 */ inputstream is = con.getinputstream(); int ch; stringbuffer b = new stringbuffer(); while ((ch = is.read()) != -1) { b.append((char) ch); } system.out.println(上传成功); toast.maketext(mainactivity.this, 上传成功, toast.length_long) .show(); ds.close(); } catch (exception e) { system.out.println(上传失败 + e.getmessage()); toast.maketext(mainactivity.this, 上传失败 + e.getmessage(), toast.length_long).show(); } } }
最后别忘了在androidmanifest.xml中设置访问internet的权限:

该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product