android上传数据到服务器,【图片】【转】通过Android 客户端上传数据到服务器【aide吧】_百度贴吧...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

public class HttpRequester {

/**

* 直接通过HTTP 协议提交数据到服务器,实现如下面表单提交功能:

*

ACTION="http://192.168.0.200:8080/ssi/fileload/test.do" enctype="multipart/form-data">

* @param actionUrl 上传路径(注:避免使用localhost 或127.0.0.1这样的路径

测试, 因为它会指向手机模拟器, 你可以使用http://www.itcast.cn 或

http://192.168.1.10:8080这样的路径测试)

* @param params 请求参数key 为参数名,value 为参数值

* @param file 上传文件

*/

public static String post(String actionUrl, Map params, FormFile[]

files) {

try {

String BOUNDARY = "---------7d4a6d158c9"; //数据分隔线

String MULTIPART_FORM_DATA = "multipart/form-data";

URL url = new URL(actionUrl);

HttpURLConnection conn = (HttpURLConnection)

url.openConnection();

conn.setConnectTimeout(5* 1000);

conn.setDoInput(true);//允许输入

conn.setDoOutput(true);//允许输出

conn.setUseCaches(false);//不使用Cache

conn.setRequestMethod("POST");

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

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

conn.setRequestProperty("Content-Type", MULTIPART_FORM_DATA

+ "; boundary=" + BOUNDARY);

StringBuilder sb = new StringBuilder();

for (Map.Entry entry : params.entrySet()) {//构建表单

字段内容

sb.append("--");

sb.append(BOUNDARY);

sb.append("\r\n");

sb.append("Content-Disposition: form-data; name=\""+

entry.getKey() + "\"\r\n\r\n");

sb.append(entry.getValue());

sb.append("\r\n");

}

DataOutputStream outStream = new

DataOutputStream(conn.getOutputStream());

outStream.write(sb.toString().getBytes());//发送表单字段数据

for(FormFile file : files){//发送文件数据

StringBuilder split = new StringBuilder();

split.append("--");

split.append(BOUNDARY);

split.append("\r\n");

split.append("Content-Disposition: form-data;name=\""+

file.getFormname()+"\";filename=\""+ file.getFilname() + "\"\r\n");

split.append("Content-Type: "+ file.getContentType()+"\r\n\r\n");

outStream.write(split.toString().getBytes());

if(file.getInStream()!=null){

byte[] buffer = new byte[1024];

int len = 0;

while((len = file.getInStream().read(buffer))!=-1){

outStream.write(buffer, 0, len);

}

file.getInStream().close();

}else{

outStream.write(file.getData(), 0, file.getData().length);

}

outStream.write("\r\n".getBytes());

}

byte[] end_data = ("--" + BOUNDARY + "--\r\n").getBytes();//数据结

束标志

outStream.write(end_data);

outStream.flush();

int cah = conn.getResponseCode();

if (cah != 200) throw new RuntimeException("请求url 失败");

InputStream is = conn.getInputStream();

int ch;

StringBuilder b = new StringBuilder();

while( (ch = is.read()) != -1 ){

b.append((char)ch);

}

outStream.close();

conn.disconnect();

return b.toString();

} catch (Exception e) {

throw new RuntimeException(e);

}

}

/**

* 提交数据到服务器

* @param actionUrl 上传路径(注:避免使用localhost 或127.0.0.1这样的路径

测试, 因为它会指向手机模拟器, 你可以使用http://www.itcast.cn 或

http://192.168.1.10:8080这样的路径测试)

* @param params 请求参数key 为参数名,value 为参数值

* @param file 上传文件

*/

public static String post(String actionUrl, Map params, FormFile

file) {

return post(actionUrl, params, new FormFile[]{file});

}

public static byte[] postFromHttpClient(String path, Map params,

String encode) throws Exception{

List formparams = new ArrayList();// 用

于存放请求参数

for(Map.Entry entry : params.entrySet()){

formparams.add(new BasicNameValuePair(entry.getKey(),

entry.getValue()));

}

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams,

"UTF-8");

HttpPost httppost = new HttpPost(path);

httppost.setEntity(entity);

HttpClient httpclient = new DefaultHttpClient();//看作是浏览器

HttpResponse response = httpclient.execute(httppost);//发送post 请求

return StreamTool.readInputStream(response.getEntity().getContent());

}

/**

* 发送请求

* @param path 请求路径

* @param params 请求参数key 为参数名称value 为参数值

* @param encode 请求参数的编码

*/

public static byte[] post(String path, Map params, String encode)

throws Exception{

//String params = "method=save&name="+ URLEncoder.encode(" 老毕",

"UTF-8")+ "&age=28&";//需要发送的参数

StringBuilder parambuilder = new StringBuilder("");

if(params!=null && !params.isEmpty()){

for(Map.Entry entry : params.entrySet()){

parambuilder.append(entry.getKey()).append("=")

.append(URLEncoder.encode(entry.getValue(),

encode)).append("&");

}

parambuilder.deleteCharAt(parambuilder.length()-1);

}

byte[] data = parambuilder.toString().getBytes();

URL url = new URL(path);

HttpURLConnection conn = (HttpURLConnection)url.openConnection();

conn.setDoOutput(true);//允许对外发送请求参数

conn.setUseCaches(false);//不进行缓存

conn.setConnectTimeout(5 * 1000);

conn.setRequestMethod("POST");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值