欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 通过httppost上传文本文件到服务器的实例代码

程序员文章站 2024-03-06 17:43:02
废话不多说了,直接给大家贴关键代码了。 /** * 往服务器上上传文本 比如log日志 * @param urlstr 请求的url * @param u...

废话不多说了,直接给大家贴关键代码了。

/**
* 往服务器上上传文本 比如log日志
* @param urlstr 请求的url 
* @param uploadfile log日志的路径 
* /mnt/shell/emulated/0/log/log.log 
* @param newname log日志的名字 log.log
* @return
*/
public static void httppost(activity activity,string urlstr,string uploadfile,string newname) {
logutil.info("getehttppostt", "urlstr="+urlstr+";uploadfile="+uploadfile+";newname="+newname,"i");
string end = "\r\n";
string twohyphens = "--";
string boundary = "*****";//边界标识 
int time_out = 10*1000; //超时时间
httpurlconnection con = null; 
dataoutputstream ds = null; 
inputstream is = null;
try {
url url = new url(urlstr);
con = (httpurlconnection) url.openconnection();
con.setreadtimeout(time_out);
con.setconnecttimeout(time_out);
/* 允许input、output,不使用cache */
con.setdoinput(true);
con.setdooutput(true);
con.setusecaches(false);
// 设置http连接属性
con.setrequestmethod("post");//请求方式
con.setrequestproperty("connection", "keep-alive");//在一次tcp连接中可以持续发送多份数据而不会断开连接
con.setrequestproperty("charset", "utf-8");//设置编码
con.setrequestproperty("content-type",//multipart/form-data能上传文件的编码格式
"multipart/form-data;boundary=" + boundary);
ds = new dataoutputstream(con.getoutputstream());
ds.writebytes(twohyphens + boundary + end);
ds.writebytes("content-disposition: form-data; "
+ "name=\"stblog\";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(twohyphens + boundary + twohyphens + end);//结束
fstream.close();
ds.flush();
/* 取得response内容 */
is = con.getinputstream();
int ch;
stringbuffer b = new stringbuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
/* 将response显示于dialog */
showdialog(activity,true,uploadfile,"上传成功" + b.tostring().trim());
} catch (exception e) {
showdialog(activity,false,uploadfile,"上传失败" + e);
}finally {
/* 关闭dataoutputstream */
if(ds!=null){
try {
ds.close();
} catch (ioexception e) {
e.printstacktrace();
}
}
if (is != null) {
try {
is.close();
} catch (ioexception e) {
e.printstacktrace();
}
}
if (con != null) {
con.disconnect();
}
}
}
/* 显示dialog的method */
private static void showdialog(final activity activity,final boolean issuccess,final string uploadfile,final string mess) {
activity.runonuithread(new runnable() {
@override
public void run() {
new alertdialog.builder(activity).settitle("message")
.setmessage(mess)
.setnegativebutton("确定", new dialoginterface.onclicklistener() {
public void onclick(dialoginterface dialog, int which) {
file file = new file(uploadfile);
if(file.exists()&&issuccess){//日志文件存在且上传日志成功
file.delete();
toast.maketext(activity, "log日志已删除", toast.length_short).show();
}
}
}).show();
}
});
}

以上内容是小编给大家介绍的android 通过httppost上传文本文件到服务器的实例代码,代码简单易懂,附有注释