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

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

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

本文所述為一個(gè)Android上傳文件的源代碼,每一步實(shí)現(xiàn)過(guò)程都備有詳盡的注釋,思路比較清楚,學(xué)習(xí)了本例所述上傳文件代碼之后,你可以應(yīng)對(duì)其它格式文件的上傳。實(shí)例中主要實(shí)現(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ù)器上對(duì)應(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è)置每次寫(xiě)入1024bytes */
    int bufferSize = 1024;
    byte[] buffer = new byte[bufferSize];

    int length = -1;
    /* 從文件讀取數(shù)據(jù)至緩沖區(qū) */
    while((length = fStream.read(buffer)) != -1)
    {
     /* 將資料寫(xiě)入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();
  }
}

讀者如果覺(jué)得功能不足的話可以對(duì)代碼進(jìn)行擴(kuò)展與完善,使之更加符合自身的應(yīng)用需求。

相關(guān)文章

  • Android簡(jiǎn)單自定義音樂(lè)波動(dòng)特效圖

    Android簡(jiǎn)單自定義音樂(lè)波動(dòng)特效圖

    這篇文章主要為大家詳細(xì)介紹了Android簡(jiǎn)單自定義音樂(lè)波動(dòng)特效圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • Android自定義照相機(jī)的實(shí)例

    Android自定義照相機(jī)的實(shí)例

    這篇文章主要介紹了Android自定義照相機(jī)的實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Android實(shí)現(xiàn)樹(shù)形層級(jí)ListView

    Android實(shí)現(xiàn)樹(shù)形層級(jí)ListView

    這篇文章主要介紹了Android實(shí)現(xiàn)樹(shù)形層級(jí)ListView的相關(guān)資料,需要的朋友可以參考下
    2016-02-02
  • Android 串口通信編程及串口協(xié)議分析

    Android 串口通信編程及串口協(xié)議分析

    這篇文章主要介紹了Android 串口通信編程及串口協(xié)議分析的相關(guān)資料,這里對(duì)Android 串口通信進(jìn)行詳解,及簡(jiǎn)單實(shí)現(xiàn)步驟和協(xié)議進(jìn)行分析,需要的朋友可以參考下
    2016-11-11
  • flutter的環(huán)境安裝配置問(wèn)題及解決方法

    flutter的環(huán)境安裝配置問(wèn)題及解決方法

    Flutter是Google推出的基于Dart語(yǔ)言開(kāi)發(fā)的跨平臺(tái)開(kāi)源UI框架,旨在統(tǒng)一紛紛擾擾的跨平臺(tái)開(kāi)發(fā)框架,在UI層面上多端共用一套Dart代碼來(lái)實(shí)現(xiàn)多平臺(tái)適配開(kāi)發(fā),這篇文章主要介紹了flutter的環(huán)境安裝配置問(wèn)題,需要的朋友可以參考下
    2020-06-06
  • Android Studio輕松構(gòu)建自定義模板的步驟記錄

    Android Studio輕松構(gòu)建自定義模板的步驟記錄

    這篇文章主要給大家介紹了關(guān)于Android Studio輕松構(gòu)建自定義模板的相關(guān)資料,文中通過(guò)示例代碼以及圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10
  • Android自定義View實(shí)現(xiàn)數(shù)字密碼鎖

    Android自定義View實(shí)現(xiàn)數(shù)字密碼鎖

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)數(shù)字密碼鎖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Android 夜間模式的實(shí)現(xiàn)代碼示例

    Android 夜間模式的實(shí)現(xiàn)代碼示例

    本篇文章主要介紹了Android 夜間模式的實(shí)現(xiàn)代碼示例,實(shí)現(xiàn)能夠根據(jù)不同的設(shè)定,呈現(xiàn)不同風(fēng)格的界面給用戶,有興趣的可以了解一下。
    2017-03-03
  • android打開(kāi)本地圖像的方法

    android打開(kāi)本地圖像的方法

    這篇文章主要介紹了android打開(kāi)本地圖像的方法,涉及Android操作圖像的相關(guān)技巧,需要的朋友可以參考下
    2015-04-04
  • Flutter onTap中讓你脫穎而出的5條規(guī)則

    Flutter onTap中讓你脫穎而出的5條規(guī)則

    這篇文章主要為大家介紹了Flutter onTap中讓你脫穎而出的5條規(guī)則,小事情決定了你的熟練程度,這些小細(xì)節(jié)的有趣之處在于它們的豐富性
    2023-11-11

最新評(píng)論

壶关县| 壤塘县| 滦平县| 泰和县| 华安县| 中江县| 伽师县| 讷河市| 得荣县| 自贡市| 东平县| 朝阳县| 连山| 西贡区| 嘉祥县| 周宁县| 容城县| 丘北县| 灵川县| 治县。| 喀什市| 厦门市| 治县。| 乃东县| 昌吉市| 梅河口市| 墨竹工卡县| 凤台县| 郧西县| 昌宁县| 靖西县| 崇左市| 琼结县| 南城县| 光山县| 岳池县| 栾川县| 宁国市| 卢龙县| 桂林市| 通榆县|