android 文件上傳

package com.figo.uploadfile;

 

import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.FileInputStream;

import java.io.InputStream;

import java.io.InputStreamReader;

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 UploadfileActivity extends Activity

{

  // 要上傳的文件路徑,理論上可以傳輸任何文件,實際使用時根據需要處理

  private String uploadFile = "/sdcard/testimg.jpg";

  private String srcPath = "/sdcard/testimg.jpg";

  // 服務器上接收文件的處理頁面,這裏根據需要換成自己的

  private String actionUrl = "http://10.100.1.208/receive_file.php";

  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("上傳網址:\n" + actionUrl);

    /* 設置mButton的onClick事件處理 */

    mButton = (Button) findViewById(R.id.myButton);

    mButton.setOnClickListener(new View.OnClickListener()

    {

      @Override

      public void onClick(View v)

      {

        uploadFile(actionUrl);

      }

    });

  }

 

  /* 上傳文件至Server,uploadUrl:接收文件的處理頁面 */

  private void uploadFile(String uploadUrl)

  {

    String end = "\r\n";

    String twoHyphens = "--";

    String boundary = "******";

    try

    {

      URL url = new URL(uploadUrl);

      HttpURLConnection httpURLConnection = (HttpURLConnection) url

          .openConnection();

      // 設置每次傳輸的流大小,可以有效防止手機因爲內存不足崩潰

      // 此方法用於在預先不知道內容長度時啓用沒有進行內部緩衝的 HTTP 請求正文的流。

      httpURLConnection.setChunkedStreamingMode(128 * 1024);// 128K

      // 允許輸入輸出流

      httpURLConnection.setDoInput(true);

      httpURLConnection.setDoOutput(true);

      httpURLConnection.setUseCaches(false);

      // 使用POST方法

      httpURLConnection.setRequestMethod("POST");

      httpURLConnection.setRequestProperty("Connection", "Keep-Alive");

      httpURLConnection.setRequestProperty("Charset", "UTF-8");

      httpURLConnection.setRequestProperty("Content-Type",

          "multipart/form-data;boundary=" + boundary);

 

      DataOutputStream dos = new DataOutputStream(

          httpURLConnection.getOutputStream());

      dos.writeBytes(twoHyphens + boundary + end);

      dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\""

          + srcPath.substring(srcPath.lastIndexOf("/") + 1)

          + "\""

          + end);

      dos.writeBytes(end);

 

      FileInputStream fis = new FileInputStream(srcPath);

      byte[] buffer = new byte[8192]; // 8k

      int count = 0;

      // 讀取文件

      while ((count = fis.read(buffer)) != -1)

      {

        dos.write(buffer, 0, count);

      }

      fis.close();

 

      dos.writeBytes(end);

      dos.writeBytes(twoHyphens + boundary + twoHyphens + end);

      dos.flush();

 

      InputStream is = httpURLConnection.getInputStream();

      InputStreamReader isr = new InputStreamReader(is, "utf-8");

      BufferedReader br = new BufferedReader(isr);

      String result = br.readLine();

 

      Toast.makeText(this, result, Toast.LENGTH_LONG).show();

      dos.close();

      is.close();

 

    } catch (Exception e)

    {

      e.printStackTrace();

      setTitle(e.getMessage());

    }

  }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章