Android實現(xiàn)上傳文件功能的方法
本文所述為一個Android上傳文件的源代碼,每一步實現(xiàn)過程都備有詳盡的注釋,思路比較清楚,學(xué)習(xí)了本例所述上傳文件代碼之后,你可以應(yīng)對其它格式文件的上傳。實例中主要實現(xiàn)上傳文件至Server的方法,允許Input、Output,不使用Cache,使Androiod上傳文件變得輕松。
主要功能代碼如下:
package com.test; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class Main extends Activity { /* 變量聲明 * newName:上傳后在服務(wù)器上的文件名稱 * uploadFile:要上傳的文件路徑 * actionUrl:服務(wù)器上對應(yīng)的程序路徑 */ private String newName="image.jpg"; private String uploadFile="/data/image.jpg"; private String actionUrl="http://l27.0.0.1/upload/upload.jsp"; private TextView mText1; private TextView mText2; private Button mButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mText1 = (TextView) findViewById(R.id.myText2); mText1.setText("文件路徑:\n"+uploadFile); mText2 = (TextView) findViewById(R.id.myText3); mText2.setText("上傳網(wǎng)址:\n"+actionUrl); /* 設(shè)置mButton的onClick事件處理 */ mButton = (Button) findViewById(R.id.myButton); mButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { uploadFile(); } }); } /* 上傳文件至Server的方法 */ private void uploadFile() { String end = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; try { URL url =new URL(actionUrl); HttpURLConnection con=(HttpURLConnection)url.openConnection(); /* 允許Input、Output,不使用Cache */ con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); /* 設(shè)置傳送的method=POST */ con.setRequestMethod("POST"); /* setRequestProperty */ con.setRequestProperty("Connection", "Keep-Alive"); con.setRequestProperty("Charset", "UTF-8"); con.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); /* 設(shè)置DataOutputStream */ DataOutputStream ds = new DataOutputStream(con.getOutputStream()); ds.writeBytes(twoHyphens + boundary + end); ds.writeBytes("Content-Disposition: form-data; " + "name=\"file1\";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); /* close streams */ fStream.close(); ds.flush(); /* 取得Response內(nèi)容 */ InputStream is = con.getInputStream(); int ch; StringBuffer b =new StringBuffer(); while( ( ch = is.read() ) != -1 ) { b.append( (char)ch ); } /* 將Response顯示于Dialog */ showDialog(b.toString().trim()); /* 關(guān)閉DataOutputStream */ ds.close(); } catch(Exception e) { showDialog(""+e); } } /* 顯示Dialog的method */ private void showDialog(String mess) { new AlertDialog.Builder(Main.this).setTitle("Message") .setMessage(mess) .setNegativeButton("確定",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }) .show(); } }
讀者如果覺得功能不足的話可以對代碼進(jìn)行擴(kuò)展與完善,使之更加符合自身的應(yīng)用需求。
- Android retrofit上傳文件實例(包含頭像)
- Android OkHttp Post上傳文件并且攜帶參數(shù)實例詳解
- android 上傳文件到服務(wù)器代碼實例
- Android中實現(xiàn)OkHttp上傳文件到服務(wù)器并帶進(jìn)度
- Android實現(xiàn)上傳文件到服務(wù)器實例詳解
- Android上傳文件到服務(wù)端并顯示進(jìn)度條
- android 開發(fā)中使用okhttp上傳文件到服務(wù)器
- Android上傳文件到服務(wù)器的方法
- Android WebView 上傳文件支持全解析
- Android程序開發(fā)通過HttpURLConnection上傳文件到服務(wù)器
- Android使用Retrofit上傳文件功能
相關(guān)文章
深入淺出RxJava+Retrofit+OkHttp網(wǎng)絡(luò)請求
本篇文章主要介紹了深入淺出RxJava+Retrofit+OkHttp網(wǎng)絡(luò)請求,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11Android實現(xiàn)繪制LocationMarkerView圖的示例代碼
LocationMarker是運(yùn)動軌跡上Start、End, 以及整公里點(diǎn)上筆者自定義繪制的一個MarkerView。這篇文章主要介紹了Android實現(xiàn)繪制LocationMarkerView圖的示例代碼,希望對大家有所幫助2023-02-02Android實現(xiàn)QQ手機(jī)管家懸浮小火箭效果
這篇文章主要介紹了Android實現(xiàn)QQ手機(jī)管家懸浮小火箭效果,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05Android實現(xiàn)仿今日頭條點(diǎn)贊動畫效果實例
我想看到今日頭條的點(diǎn)贊效果,應(yīng)該都覺得很絢麗吧,下面這篇文章主要給大家介紹了關(guān)于Android實現(xiàn)仿今日頭條點(diǎn)贊動畫效果的相關(guān)資料,文中通過示例代價介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02深入Android HandlerThread 使用及其源碼完全解析
這篇文章主要介紹了深入Android HandlerThread 使用及其源碼完全解析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08Android檢測Activity或者Service是否運(yùn)行的方法
下面小編就為大家分享一篇Android檢測Activity或者Service是否運(yùn)行的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03