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

所属分类: 软件编程 / Android 阅读数: 969
收藏 0 赞 0 分享

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

/**
* 往服务器上上传文本 比如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上传文本文件到服务器的实例代码,代码简单易懂,附有注释,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多