Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android二進制文件轉碼到Base64並通過Post進行提交

Android二進制文件轉碼到Base64並通過Post進行提交

編輯:高級開發

類似於Ruby,在上面調試成功後,將功能遷移到了android應用上面。

  Java代碼

  import org.apache.http.HttpResponse;

  import org.apache.http.NameValuePair;

  import org.apache.http.clIEnt.entity.UrlEncodedFormEntity;

  import org.apache.http.clIEnt.methods.HttpPost;

  import org.apache.http.impl.client.DefaultHttpClIEnt;

  import org.apache.http.message.BasicNameValuePair;

  import org.apache.http.protocol.HTTP;

  import android.util.Base64;

  import android.util.Log;

  /**

  * TODO FIX Check the file length, it's long not int.This may lead the

  * problem.

  *

  * @param fileName

  * @param url

  * @return

  */

  public static boolean httpPostBase64(String fileName, String url) {

  try {

  File file = new File(fileName);

  FileInputStream in = new FileInputStream(file);

  byte[] buffer = new byte[(int) file.length() + 100];

  int length = in.read(buffer);

  String data = Base64.encodeToString(buffer, 0, length,

  Base64.DEFAULT);

  HttpPost httpRequest = new HttpPost(url);

  /*

  * NameValuePair實現請求參數的封裝

  */

  List params = new LinkedList();

  params.add(new BasicNameValuePair("data", data));

  /* 添加請求參數到請求對象 */

  httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

  /* 發送請求並等待響應 */

  HttpResponse httpResponse = new DefaultHttpClIEnt()

  .execute(httpRequest);

  /* 若狀態碼為200 ok */

  if (httpResponse.getStatusLine().getStatusCode() != 200) {

  Log.d("Code", httpResponse.getStatusLine().toString());

  return false;

  }

  } catch (Exception e) {

  for(StackTraceElement s:e.getStackTrace())

  接上頁

  Log.d("Exception",s.toString());

  Log.d("Exception",e.getLocalizedMessage());

  return false;

  }

  return true;

  }

  import org.apache.http.HttpResponse;

  import org.apache.http.NameValuePair;

  import org.apache.http.clIEnt.entity.UrlEncodedFormEntity;

  import org.apache.http.clIEnt.methods.HttpPost;

  import org.apache.http.impl.client.DefaultHttpClIEnt;

  import org.apache.http.message.BasicNameValuePair;

  import org.apache.http.protocol.HTTP;

  import android.util.Base64;

  import android.util.Log;

  /**

  * TODO FIX Check the file length, it's long not int.This may lead the

  * problem.

  *

  * @param fileName

  * @param url

  * @return

  */

  public static boolean httpPostBase64(String fileName, String url) {

  try {

  File file = new File(fileName);

  FileInputStream in = new FileInputStream(file);

  byte[] buffer = new byte[(int) file.length() + 100];

  int length = in.read(buffer);

  String data = Base64.encodeToString(buffer, 0, length,

  Base64.DEFAULT);

  HttpPost httpRequest = new HttpPost(url);

  /*

  * NameValuePair實現請求參數的封裝

  */

  List params = new LinkedList();

  params.add(new BasicNameValuePair("data", data));

  /* 添加請求參數到請求對象 */

  httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

  /* 發送請求並等待響應 */

  HttpResponse httpResponse = new DefaultHttpClIEnt()

  .execute(httpRequest);

  /* 若狀態碼為200 ok */

  if (httpResponse.getStatusLine().getStatusCode() != 200) {

  Log.d("Code", httpResponse.getStatusLine().toString());

  return false;

  }

  } catch (Exception e) {

  for(StackTraceElement s:e.getStackTrace())

  Log.d("Exception",s.toString());

  Log.d("Exception",e.getLocalizedMessage());

  return false;

  }

  return true;

  }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved