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

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

[url="http://www.cnblogs.com/shanyou/archive/2013/06/07/3123155.html"]关于混编格式[/url]
[url="http://www.faqs.org/rfcs/rfc2388.html"]multipart/form-data官方介绍[/url]
[url="http://www.cnblogs.com/jdonson/archive/2009/07/22/1528466.html"]关于Java UUID[/url]


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;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值