
废话不多说了,直接给大家贴关键代码了。
/*** 往服务器上上传文本 比如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);// 取得文件的fileinputStreamfileinputStream 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,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,"上传失败" + 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() {@OverrIDepublic 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 通过httppost上传文本文件到服务器的实例代码全部内容,希望文章能够帮你解决Android 通过httppost上传文本文件到服务器的实例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)