简单实现Android文件上传
程序员文章站
2024-03-31 14:33:10
文件上传在b/s应用中是一种十分常见的功能,那么在android平台下是否可以实现像b/s那样的文件上传功能呢?答案是肯定的。下面是一个模拟网站程序上传文件的例子。...
文件上传在b/s应用中是一种十分常见的功能,那么在android平台下是否可以实现像b/s那样的文件上传功能呢?答案是肯定的。下面是一个模拟网站程序上传文件的例子。
首先新建一个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的权限:
<uses-permission android:name="android.permission.internet" />
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: MySQL提高分页效率