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

簡單實現(xiàn)Android文件上傳

 更新時間:2016年12月14日 14:27:10   作者:小澄  
這篇文章主要為大家詳細(xì)介紹了如何簡單實現(xiàn)Android文件上傳的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

文件上傳在B/S應(yīng)用中是一種十分常見的功能,那么在Android平臺下是否可以實現(xiàn)像B/S那樣的文件上傳功能呢?答案是肯定的。下面是一個模擬網(wǎng)站程序上傳文件的例子。

首先新建一個Android工程,新建主啟動Activity:

MainActivity.java:

package com.xzq.upload;

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.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity{

 private String newName = "htys.mp3";
 //要上傳的本地文件路徑
 private String uploadFile = "/data/data/com.xzq/htys.mp3";
 //上傳到服務(wù)器的指定位置
 private String actionUrl = "http://192.168.100.100:8080/upload/upload.jsp";
 private TextView mTextView1;
 private TextView mTextView2;
 private Button mButton1;

 @Override
 public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  mTextView1 = (TextView) findViewById(R.id.myText2);
  mTextView1.setText("FilePath:/n" + uploadFile);
  mTextView2 = (TextView) findViewById(R.id.myText3);
  mTextView2.setText("UploadPath:/n" + actionUrl);
  /* 設(shè)定mButton的onClick事件處理 */
  mButton1 = (Button) findViewById(R.id.myButton);
  mButton1.setOnClickListener(new View.OnClickListener(){
   public void onClick(View v){
    uploadFile();
   }
  });
 }

 private void uploadFile(){
  String end = "/r/n";
  String Hyphens = "--";
  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(Hyphens + 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){
    /* 將數(shù)據(jù)寫入DataOutputStream中 */
    ds.write(buffer, 0, length);
   }
   ds.writeBytes(end);
   ds.writeBytes(Hyphens + boundary + Hyphens + end);
   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);
   }
   System.out.println("上傳成功");
   Toast.makeText(MainActivity.this, "上傳成功", Toast.LENGTH_LONG)
     .show();
   ds.close();
  } catch (Exception e){
   System.out.println("上傳失敗" + e.getMessage());
   Toast.makeText(MainActivity.this, "上傳失敗" + e.getMessage(),
     Toast.LENGTH_LONG).show();
  }
 }
}

最后別忘了在AndroidManifest.xml中設(shè)置訪問Internet的權(quán)限:

 <uses-permission android:name="android.permission.INTERNET" />

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

佛坪县| 天柱县| 湖北省| 萨迦县| 温宿县| 抚顺市| 凤台县| 盐源县| 蕲春县| 泸定县| 肥城市| 新兴县| 斗六市| 资源县| 桐梓县| 太仓市| 和田市| 琼结县| 临沂市| 轮台县| 大姚县| 晴隆县| 鄂尔多斯市| 长汀县| 巩留县| 仪征市| 彭山县| 双峰县| 体育| 新竹县| 松滋市| 广河县| 名山县| 霍州市| 湘潭市| 黑山县| 宜春市| 临澧县| 泽库县| 环江| 麻城市|