欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)上傳文件功能的方法

 更新時間:2014年07月30日 10:34:40   投稿:shichen2014  
這篇文章主要介紹了Android實現(xiàn)上傳文件功能的方法,對Android初學(xué)者有一定的借鑒價值,需要的朋友可以參考下

本文所述為一個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)用需求。

相關(guān)文章

  • 深入淺出RxJava+Retrofit+OkHttp網(wǎng)絡(luò)請求

    深入淺出RxJava+Retrofit+OkHttp網(wǎng)絡(luò)請求

    本篇文章主要介紹了深入淺出RxJava+Retrofit+OkHttp網(wǎng)絡(luò)請求,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Android 獲取IP地址的實現(xiàn)方法

    Android 獲取IP地址的實現(xiàn)方法

    這篇文章主要介紹了Android 獲取IP地址的實現(xiàn)方法的相關(guān)資料,這里提供了具體實現(xiàn)的方法及代碼,使用WIFI 和GPRS的思路,需要的朋友可以參考下
    2016-11-11
  • Android實現(xiàn)繪制LocationMarkerView圖的示例代碼

    Android實現(xiàn)繪制LocationMarkerView圖的示例代碼

    LocationMarker是運(yùn)動軌跡上Start、End, 以及整公里點(diǎn)上筆者自定義繪制的一個MarkerView。這篇文章主要介紹了Android實現(xiàn)繪制LocationMarkerView圖的示例代碼,希望對大家有所幫助
    2023-02-02
  • flutter 實現(xiàn)多布局列表的示例代碼

    flutter 實現(xiàn)多布局列表的示例代碼

    這篇文章主要介紹了flutter 實現(xiàn)多布局列表的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • Android實現(xiàn)QQ手機(jī)管家懸浮小火箭效果

    Android實現(xiàn)QQ手機(jī)管家懸浮小火箭效果

    這篇文章主要介紹了Android實現(xiàn)QQ手機(jī)管家懸浮小火箭效果,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • 簡單實現(xiàn)Android滾動公告欄

    簡單實現(xiàn)Android滾動公告欄

    這篇文章主要為大家詳細(xì)介紹了如何簡單實現(xiàn)Android滾動公告欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Android實現(xiàn)仿今日頭條點(diǎn)贊動畫效果實例

    Android實現(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 使用及其源碼完全解析

    這篇文章主要介紹了深入Android HandlerThread 使用及其源碼完全解析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • 快速了解Android Room使用細(xì)則

    快速了解Android Room使用細(xì)則

    這篇文章主要為大家介紹了快速了解Android Room使用細(xì)則,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Android檢測Activity或者Service是否運(yùn)行的方法

    Android檢測Activity或者Service是否運(yùn)行的方法

    下面小編就為大家分享一篇Android檢測Activity或者Service是否運(yùn)行的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03

最新評論