Android 通過(guò)httppost上傳文本文件到服務(wù)器的實(shí)例代碼
廢話不多說(shuō)了,直接給大家貼關(guān)鍵代碼了。
/**
* 往服務(wù)器上上傳文本 比如log日志
* @param urlstr 請(qǐng)求的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)識(shí)
int TIME_OUT = 10*1000; //超時(shí)時(shí)間
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");//請(qǐng)求方式
con.setRequestProperty("Connection", "Keep-Alive");//在一次TCP連接中可以持續(xù)發(fā)送多份數(shù)據(jù)而不會(huì)斷開(kāi)連接
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 通過(guò)httppost上傳文本文件到服務(wù)器的實(shí)例代碼,代碼簡(jiǎn)單易懂,附有注釋,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android 通過(guò)Base64上傳圖片到服務(wù)器實(shí)現(xiàn)實(shí)例
- android選擇視頻文件上傳到后臺(tái)服務(wù)器
- Android選擇圖片或拍照?qǐng)D片上傳到服務(wù)器
- Android開(kāi)發(fā)中調(diào)用系統(tǒng)相冊(cè)上傳圖片到服務(wù)器OPPO等部分手機(jī)上出現(xiàn)短暫的顯示桌面問(wèn)題的解決方法
- Android實(shí)現(xiàn)上傳文件到服務(wù)器實(shí)例詳解
- Android使用post方式上傳圖片到服務(wù)器的方法
- Android程序開(kāi)發(fā)通過(guò)HttpURLConnection上傳文件到服務(wù)器
- Android異步上傳圖片到PHP服務(wù)器
- android 上傳文件到服務(wù)器代碼實(shí)例
- Android上傳文件到服務(wù)器的方法
相關(guān)文章
Android實(shí)現(xiàn)Bitmap位圖旋轉(zhuǎn)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)Bitmap位圖旋轉(zhuǎn)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Android自定義SeekBar滑動(dòng)顯示數(shù)字
這篇文章主要為大家詳細(xì)介紹了Android自定義SeekBar滑動(dòng)顯示數(shù)字,使用FrameLayout結(jié)合SeekBar滑動(dòng)時(shí),數(shù)值顯示,滑動(dòng)停止時(shí)顯示數(shù)字,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Android答題APP的設(shè)計(jì)與實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android答題APP的設(shè)計(jì)與實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Android如何判斷當(dāng)前點(diǎn)擊位置是否在圓的內(nèi)部
這篇文章主要為大家詳細(xì)介紹了Android如何判斷當(dāng)前點(diǎn)擊位置是否在圓的內(nèi)部,解析拖動(dòng)圓形控件之內(nèi)響應(yīng)觸摸事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
ActivityManagerService廣播并行發(fā)送與串行發(fā)送示例解析
這篇文章主要為大家介紹了ActivityManagerService廣播并行發(fā)送與串行發(fā)送示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android編程實(shí)現(xiàn)在一個(gè)程序中啟動(dòng)另一個(gè)程序的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)在一個(gè)程序中啟動(dòng)另一個(gè)程序的方法,結(jié)合實(shí)例形式分析了Android通過(guò)ResolveInfo類來(lái)取得啟動(dòng)Acitivty類名的方法來(lái)啟動(dòng)另一個(gè)程序的方法,需要的朋友可以參考下2017-02-02

