Http一次上传多个文件(类型可以不同)的方式

Http上传中Header信息内要指定ContentType以告知浏览器你所上传的文件类型,如果需要在一次request中上传多个文件(甚至可以是多种文件类型),就可以使用CONTENT_TYPE = "multipart/form-data"这种混编格式来完成,具体做法可以参照示例代码,而相关的介绍则可以参看:

关于混编格式
multipart/form-data官方介绍
关于Java UUID

package com.example.pmudemo.helper;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.UUID;

import android.util.Log;

/**
 * Message upload helper class
 * 
 * @author weishijie
 *
 */
public class UploadHelper {

	private static final String TAG = "UploadHelper";
	/**
	 * Timeout
	 */
	private static final int TIME_OUT = 5 * 1000;
	/**
	 * Charset
	 */
	private static final String CHARSET = "utf-8";

	/**
	 * Upload file to server
	 * 本方法采用“混编格式”实现上传
	 * 混编格式,即混合多种资料格式并一次传送,当然非文字资料必须要编码为二进制字符串
	 * 参考:http://www.cnblogs.com/shanyou/archive/2013/06/07/3123155.html
	 * 
	 * @param file file need to upload
	 * @param RequestURL request url
	 * @return response string
	 */
	public static String uploadFile(File file, String requestURL)
	{
		String result = null;
		/*UUID(Universally Unique Identifier)全局唯一标识符,是指在一台机器上生成的数字,
		它保证对在同一时空(3240年)中的所有机器都是唯一的。UUID一般形如下例:
		550E8400-E29B-11D4-A716-446655440000*/
		// 边界标识 随机生成
		String BOUNDARY = UUID.randomUUID().toString();
		String PREFIX = "--";
		String LINE_END = "\r\n";
		String CONTENT_TYPE = "multipart/form-data";

		try
		{
			URL url = new URL(requestURL);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setReadTimeout(TIME_OUT);
			conn.setConnectTimeout(TIME_OUT);
			// Allow input
			conn.setDoInput(true);
			// Allow output
			conn.setDoOutput(true);
			// Forbidden cache
			conn.setUseCaches(false);
			conn.setRequestMethod("POST");
			// Set request header field
			conn.setRequestProperty("Charset", CHARSET);
			conn.setRequestProperty("connection", "keep-alive");
			conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary="
					+ BOUNDARY);

			if (file != null)
			{
				/**
				 * 当文件不为空,把文件包装并且上传
				 */
				DataOutputStream dos = new DataOutputStream(
						conn.getOutputStream());
				StringBuffer sb = new StringBuffer();
				sb.append(PREFIX);
				sb.append(BOUNDARY);
				sb.append(LINE_END);
				/**
				 * 这里重点注意: name里面的值为服务器端需要key 只有这个key 才可以得到对应的文件
				 * filename是文件的名字,包含后缀名的 比如:abc.png
				 */

				sb.append("Content-Disposition: form-data; name=\"log\"; filename=\""
						+ file.getName() + "\"" + LINE_END);
				sb.append("Content-Type: application/octet-stream; charset="
						+ CHARSET + LINE_END);
				sb.append(LINE_END);
				dos.write(sb.toString().getBytes());
				InputStream is = new FileInputStream(file);
				byte[] bytes = new byte[1024];
				int len = 0;
				while ((len = is.read(bytes)) != -1)
				{
					dos.write(bytes, 0, len);
				}
				is.close();
				dos.write(LINE_END.getBytes());
				byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINE_END)
						.getBytes();
				dos.write(end_data);
				dos.flush();
				/**
				 * 获取响应码 200=成功 当响应成功,获取响应的流
				 */
				int res = conn.getResponseCode();
				Log.i(TAG, "response code:" + res);
				Log.i(TAG, "request success");
				InputStream input = conn.getInputStream();
				StringBuffer sb1 = new StringBuffer();
				int ss;
				while ((ss = input.read()) != -1)
				{
					sb1.append((char) ss);
				}
				result = sb1.toString();
				Log.i(TAG, "result : " + result);
			}else{
				return null;
			}
		} catch (MalformedURLException e)
		{
			e.printStackTrace();
			return null;
		} catch (IOException e)
		{
			e.printStackTrace();
			return null;
		} catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
		return result;
	}
}

你可能感兴趣的:(java,http)