最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 通過httppost上傳文本文件到服務(wù)器的實例代碼

 更新時間:2016年08月03日 09:55:04   作者:李培能  
這篇文章主要介紹了Android 通過httppost上傳文本文件到服務(wù)器的實例代碼,非常簡單易懂,非常實用,需要的朋友可以參考下

廢話不多說了,直接給大家貼關(guān)鍵代碼了。

/**
* 往服務(wù)器上上傳文本 比如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 = "*****";//邊界標(biāo)識 
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);
// 設(shè)置http連接屬性
con.setRequestMethod("POST");//請求方式
con.setRequestProperty("Connection", "Keep-Alive");//在一次TCP連接中可以持續(xù)發(fā)送多份數(shù)據(jù)而不會斷開連接
con.setRequestProperty("Charset", "UTF-8");//設(shè)置編碼
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);
/* 設(shè)置每次寫入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int length = -1;
/* 從文件讀取數(shù)據(jù)至緩沖區(qū) */
while ((length = fStream.read(buffer)) != -1) {
/* 將資料寫入DataOutputStream中 */
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);//結(jié)束
fStream.close();
ds.flush();
/* 取得Response內(nèi)容 */
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 {
/* 關(guān)閉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();
}
});
}

以上內(nèi)容是小編給大家介紹的Android 通過httppost上傳文本文件到服務(wù)器的實例代碼,代碼簡單易懂,附有注釋,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論

牙克石市| 德安县| 怀化市| 汕尾市| 巴马| 微博| 东乌珠穆沁旗| 巴林右旗| 辉县市| 潜江市| 昭平县| 新巴尔虎左旗| 修文县| 姚安县| 南充市| 方山县| 辉县市| 广昌县| 布拖县| 铅山县| 黑河市| 岳阳市| 东山县| 康定县| 桦南县| 莲花县| 嘉鱼县| 阜新市| 许昌县| 白朗县| 徐水县| 海城市| 滨海县| 临西县| 浑源县| 赣榆县| 温州市| 香格里拉县| 横峰县| 新竹县| 富平县|