天天看點

Android網絡程式設計之使用HttpClient批量上傳檔案(一)1.預備知識:2.通過HttpCient上傳檔案程式運作效果圖:3.完成項目代碼:

請尊重他人的勞動成果,轉載請注明出處:Android網絡程式設計之使用HttpClient批量上傳檔案

我曾在《Android網絡程式設計之使用HTTP通路網絡資源》一文中介紹過HttpCient的使用,這裡就不在累述了,感興趣的朋友可以去看一下。在這裡主要介紹如何通過HttpClient實作檔案上傳。

1.預備知識:

          在HttpCient4.3之前上傳檔案主要使用MultipartEntity這個類,但現在這個類已經不在推薦使用了。随之替代它的類是MultipartEntityBuilder。

下面讓我們了解一下MultipartEntityBuilder類:

         MultipartEntityBuilder這個類主要用于建立HttpEntity。它的主要方法有:

修飾符和類型 方法和描述
MultipartEntityBuilder

addBinaryBody(String name, byte[] b) 

将位元組數組以二進制的形式添加資料。

MultipartEntityBuilder

addBinaryBody(String name, byte[] b, ContentType contentType, String filename) 

将位元組數組以二進制的形式添加資料。

MultipartEntityBuilder

addBinaryBody(String name, File file) 

将檔案以二進制的形式添加資料。

MultipartEntityBuilder

addBinaryBody(String name, File file, ContentType contentType, String filename) 

将檔案以二進制的形式添加資料。

MultipartEntityBuilder addBinaryBody(String name, InputStream stream) 
MultipartEntityBuilder

addBinaryBody(String name, InputStream stream, ContentType contentType, String filename) 

将輸入流以二進制的形式添加資料。

MultipartEntityBuilder

addPart(String name, ContentBody contentBody) 

添加ContentBody 類型的資料。

MultipartEntityBuilder

addTextBody(String name, String text) 

添加文本資料。

MultipartEntityBuilder

addTextBody(String name, String text, ContentType contentType) 

以指定的内容類型添加文本資料。

HttpEntity

build() 

建立一個HttpEntity。

static MultipartEntityBuilder

create() 

建立一個MultipartEntityBuilder對象。

MultipartEntityBuilder

setBoundary(String boundary) 

設定邊界。

MultipartEntityBuilder

setCharset(Charset charset) 

設定請求的編碼格式。

MultipartEntityBuilder setLaxMode() 
MultipartEntityBuilder

setMode(HttpMultipartMode mode) 

設定模式。

MultipartEntityBuilder setStrictMode() 

主要方法說明:

addBinaryBody、addPart、addTextBody方法用于添加要上傳的資料,從上面的表格中可以發現用于添加資料的方法,都是key-value類型。是以在伺服器端我們可以通過request.getPart("keyname")方式擷取對應key的資料。也可以通過request.getParts()方式擷取用戶端通過以上三種方法送出所有資料。

1.通過addBinaryBody方法直接可以添加File、InputStream、byte[]類型的資料。

2.通過addPart方法隻能添加ContentBody類型的資料,在org.apache.http.entity.mime.content包中已經提供了String、File以及InputStream對應的ContentBody類型的子類,如FileBody、InputStreamBody、StringBody,通過這些類我們可以将String、File以及InputStream類型的資料轉換成ContentBody類型的資料。

3.通過addTextBody方法我們可以很友善的添加文本資料。

2.通過HttpCient上傳檔案

Android端需要添加httpcore-4.3.2.jar、httpmime-4.3.5.jar兩個包。兩個包缺一不可。

在這裡我用的是最新版的HttpCient,大家可以從http://hc.apache.org/downloads.cgi上下載下傳所需要的jar包,如果上面的網站打不開,大家也不用擔心,我已經将項目中所需要的jar包上傳到CSDN上《httpcomponents-client-4.3.5-bin.zip》需要的朋友可以去下載下傳。

      Android端項目核心代碼:

HttpClient client=new DefaultHttpClient();// 開啟一個用戶端 HTTP 請求 
HttpPost post = new HttpPost(url);//建立 HTTP POST 請求  
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
//		builder.setCharset(Charset.forName("uft-8"));//設定請求的編碼格式
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);//設定浏覽器相容模式
int count=0;
for (File file:files) {
//			FileBody fileBody = new FileBody(file);//把檔案轉換成流對象FileBody
//			builder.addPart("file"+count, fileBody);
	builder.addBinaryBody("file"+count, file);
	count++;
}		
builder.addTextBody("method", params.get("method"));//設定請求參數
builder.addTextBody("fileTypes", params.get("fileTypes"));//設定請求參數
HttpEntity entity = builder.build();// 生成 HTTP POST 實體  	
post.setEntity(entity);//設定請求參數
HttpResponse response = client.execute(post);// 發起請求 并傳回請求的響應
if (response.getStatusLine().getStatusCode()==200) {
	return true;
}
return false;		
           

代碼分析: 

     上面代碼主要實作了多檔案上傳,為了友善伺服器端儲存檔案,上面代碼設定了名稱為fileTypes的參數,fileTypes是由上傳的檔案類型名拼接成的字元串,如”.jpg.png.docx“;

     伺服器端可以通過擷取名為fileTypes的參數,然後将其拆分成字元數組,即可得到要儲存檔案的類型。

伺服器端項目核心代碼:

伺服器段主要用到Servlet3.0的API,主要用到的方法有:

1.      request.getParameter("");//擷取用戶端通過addTextBody方法添加的String類型的資料。

2.      request.getPart("");//擷取用戶端通過addBinaryBody、addPart、addTextBody方法添加的指定資料,傳回Part類型的對象。

3.      request.getParts();//擷取用戶端通過addBinaryBody、addPart、addTextBody方法添加的所有資料,傳回Collection<Part>類型的對象。

4.      part.getName();//擷取上傳檔案的名稱即上傳時指定的key。

5.      part.getSize()//擷取上傳檔案的大小機關為位元組。

String fileTypes=request.getParameter("fileTypes");//擷取用戶端上傳的所有檔案類型
String[]typeArray=fileTypes.substring(1).split("\\.");//将檔案類型字元串拆分成String數組
try {
	Iterator<Part>iterator=request.getParts().iterator();
	int count=0;
	while (iterator.hasNext()) {//周遊用戶端上傳的所有檔案				
		if (count>=typeArray.length)break;//如果超出檔案類型數組的大小則跳出循環		
		Part part = (Part) iterator.next();				
//				System.out.println("part.getSize()"+part.getSize());//擷取上傳檔案的大小
//				System.out.println("part.getName()"+part.getName());//擷取上傳檔案的名及添加資料時的key名
		File file=new File("E:\\upload\\"+count+"."+typeArray[count++]);
		InputStream inputStream=part.getInputStream();
		FileOutputStream fos=new FileOutputStream(file);
		byte[]buffer=new byte[1024];
		int len=0;
		while ((len=inputStream.read(buffer))!=-1) {
			fos.write(buffer,0, len);
		}
		inputStream.close();
		fos.close();					
	}
}catch (Exception e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
           

代碼分析: 

         伺服器端是通過Servlet實作的,通過調用request.getParameter("fileTypes")方法來擷取用戶端上傳的所有檔案類型,然後将檔案類型字元串拆分成String數組。通過request.getParts()方法取出用戶端通過addBinaryBody、addPart、addTextBody上傳的所有資料,然後周遊資料集合即可進行檔案的儲存。

由于事先和用戶端協定,添加上傳檔案的順序在添加請求參數之前,是以可以根據拆分出的檔案類型數組的長度判斷出用戶端上傳檔案的個數,是以當上面代碼周遊超出了類型數組的長度時程式跳出循環,不再進行檔案的儲存,因為下面的Part都是些參數,而不是要儲存的檔案了。

程式運作效果圖:

Android網絡程式設計之使用HttpClient批量上傳檔案(一)1.預備知識:2.通過HttpCient上傳檔案程式運作效果圖:3.完成項目代碼:

3.完成項目代碼:

MainActivity.java

package com.jph.ufh.activity;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.jph.ufh.R;
import com.jph.ufh.service.UploadService;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

/**
 * 通過httpClient批量上傳檔案
 * @author jph
 * Date:2014.10.09  
 */
public class MainActivity extends Activity {
	private ArrayList<File>files;
	private Map<String, String>params;
	Handler mHandler=new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case UploadService.UPLOAD_SUCCESS:
				Toast.makeText(MainActivity.this, "上傳成功", Toast.LENGTH_LONG).show();
				break;			
			}
			super.handleMessage(msg);
		}		
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);		
		files=new ArrayList<File>();
		params=new HashMap<String, String>();
		
	}
	public void upload(View v) {
		files.clear();
		params.clear();
		File file=new File(Environment.getExternalStorageDirectory(),"kaola.jpg");
		File file2=new File(Environment.getExternalStorageDirectory(),"test.docx");
		File file3=new File(Environment.getExternalStorageDirectory(),"test.jpg");
		files.add(file);
		files.add(file2);
		files.add(file3);
		StringBuffer sbFileTypes=new StringBuffer();
		for (File tempFile:files) {
			String fileName=tempFile.getName();
			sbFileTypes.append(getFileType(fileName));			
		}
		params.put("fileTypes",sbFileTypes.toString());
		params.put("method", "upload");
		UploadService uploadService=new UploadService(mHandler);
		uploadService.uploadFileToServer(params, files);
	}
	/**
	 * 擷取檔案的類型
	 * @param fileName :檔案名
	 * @return 檔案類型
	 */
	private String getFileType(String fileName) {
		// TODO Auto-generated method stub
		return fileName.substring(fileName.lastIndexOf("."), fileName.length());
	}
}
           

UploadService.java

package com.jph.ufh.service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.Handler;
 
/**
 * 采用HttpClient上傳檔案,支援多檔案上傳
 * @author jph
 * Date:2014.10.09
 */
public class UploadService {
	private static String url="http://10.219.57.16:8080/ServerForUpload/ServletForUpload";
//	private static String url="http://10.110.6.58:8080/ServerForUpload/ServletForUpload";
	public static final int UPLOAD_SUCCESS=0x123;
	public static final int UPLOAD_FAIL=0x124;
	private Handler handler;
	public UploadService(Handler handler) {
		// TODO Auto-generated constructor stub
		this.handler=handler;
	}	
	/**
	 * @param params 請求參數,包括請求的的方法參數method如:“upload”,
	 * 請求上傳的檔案類型fileTypes如:“.jpg.png.docx”
	 * @param files 要上傳的檔案集合
	 */
	public void uploadFileToServer(final Map<String, String> params, final ArrayList<File>files) {
		// TODO Auto-generated method stub	
		new Thread(new Runnable() {			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					 if (uploadFiles(url,params,files)) {
						handler.sendEmptyMessage(UPLOAD_SUCCESS);//通知主線程資料發送成功
					}else {
						//将資料發送給伺服器失敗
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}
		}).start();
	}
	/**
	 * @param url servlet的位址
	 * @param params 要傳遞的參數
	 * @param files 要上傳的檔案
	 * @return true if upload success else false
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	private boolean uploadFiles(String url,Map<String, String>params,ArrayList<File>files) throws ClientProtocolException, IOException {
		HttpClient client=new DefaultHttpClient();// 開啟一個用戶端 HTTP 請求 
		HttpPost post = new HttpPost(url);//建立 HTTP POST 請求  
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
//		builder.setCharset(Charset.forName("uft-8"));//設定請求的編碼格式
		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);//設定浏覽器相容模式
		int count=0;
		for (File file:files) {
//			FileBody fileBody = new FileBody(file);//把檔案轉換成流對象FileBody
//			builder.addPart("file"+count, fileBody);
			builder.addBinaryBody("file"+count, file);
			count++;
		}		
		builder.addTextBody("method", params.get("method"));//設定請求參數
		builder.addTextBody("fileTypes", params.get("fileTypes"));//設定請求參數
		HttpEntity entity = builder.build();// 生成 HTTP POST 實體  	
		post.setEntity(entity);//設定請求參數
		HttpResponse response = client.execute(post);// 發起請求 并傳回請求的響應
		if (response.getStatusLine().getStatusCode()==200) {
			return true;
		}
		return false;		
	}
}
           

繼續閱讀