android模拟post请求上传文件
2010-09-02 14:50
295 查看
这个是查看网上的资料及android sdk 开发范例书上的例子之后做出来的,基本上是粘过来的,只是添加了几句代码。下面是代码:
public static String uploadBitmap1(String urlString,byte[] imageBytes){
String endString = "/r/n";
String twoHyphen = "--";
String boundary = "*****";
try {
URL url = new URL(urlString);
final HttpURLConnection con = (HttpURLConnection)url.openConnection();
//允许input、Output,不使用Cache
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
//设置传送的method=POST
con.setRequestMethod("POST");
//setRequestProperty
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
//从主机读取数据的超时时间(单位:毫秒)
con.setReadTimeout(50000);
//设置连接主机的超时时间(单位:毫秒)
con.setConnectTimeout(50000);
//System.out.println(con.getResponseCode());
//设置DataOutputStream
DataOutputStream dsDataOutputStream = new DataOutputStream(con.getOutputStream());
dsDataOutputStream.writeBytes(twoHyphen + boundary + endString);
dsDataOutputStream.writeBytes("Content-Disposition:form-data;" + "name=/"file1/";filename=/"" +
"11.jpg/"" + endString);
dsDataOutputStream.writeBytes(endString);
dsDataOutputStream.write(imageBytes,0,imageBytes.length);
dsDataOutputStream.writeBytes(endString);
dsDataOutputStream.writeBytes(twoHyphen + boundary + twoHyphen + endString);
dsDataOutputStream.close();
int cah = con.getResponseCode();
if(cah == 200){
InputStream isInputStream = con.getInputStream();
int ch;
StringBuffer buffer = new StringBuffer();
while((ch = isInputStream.read()) != -1){
buffer.append((char)ch);
}
return buffer.toString();
}else{
return "false";
}
} catch (Exception e) {
e.printStackTrace();
return "false";
}
}
传过来的参数中byte[] imageBytes是将图片或者是要上传的文件转换后得到的byte数组。其他的就不解释了,查看下面几个文章就知道了(网上找到的很不错的文章)。
http://zsnlovewl.javaeye.com/blog/636650
http://student.csdn.net/space.php?uid=46868&do=blog&id=25873
再写一个简单的Bitmap转换成Byte数组的代码:
ByteArrayOutputStream os = new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG, 70, os);
byte[] imgBytes = os.toByteArray();
bm是Bitmap对象。
public static String uploadBitmap1(String urlString,byte[] imageBytes){
String endString = "/r/n";
String twoHyphen = "--";
String boundary = "*****";
try {
URL url = new URL(urlString);
final HttpURLConnection con = (HttpURLConnection)url.openConnection();
//允许input、Output,不使用Cache
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
//设置传送的method=POST
con.setRequestMethod("POST");
//setRequestProperty
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
//从主机读取数据的超时时间(单位:毫秒)
con.setReadTimeout(50000);
//设置连接主机的超时时间(单位:毫秒)
con.setConnectTimeout(50000);
//System.out.println(con.getResponseCode());
//设置DataOutputStream
DataOutputStream dsDataOutputStream = new DataOutputStream(con.getOutputStream());
dsDataOutputStream.writeBytes(twoHyphen + boundary + endString);
dsDataOutputStream.writeBytes("Content-Disposition:form-data;" + "name=/"file1/";filename=/"" +
"11.jpg/"" + endString);
dsDataOutputStream.writeBytes(endString);
dsDataOutputStream.write(imageBytes,0,imageBytes.length);
dsDataOutputStream.writeBytes(endString);
dsDataOutputStream.writeBytes(twoHyphen + boundary + twoHyphen + endString);
dsDataOutputStream.close();
int cah = con.getResponseCode();
if(cah == 200){
InputStream isInputStream = con.getInputStream();
int ch;
StringBuffer buffer = new StringBuffer();
while((ch = isInputStream.read()) != -1){
buffer.append((char)ch);
}
return buffer.toString();
}else{
return "false";
}
} catch (Exception e) {
e.printStackTrace();
return "false";
}
}
传过来的参数中byte[] imageBytes是将图片或者是要上传的文件转换后得到的byte数组。其他的就不解释了,查看下面几个文章就知道了(网上找到的很不错的文章)。
http://zsnlovewl.javaeye.com/blog/636650
http://student.csdn.net/space.php?uid=46868&do=blog&id=25873
再写一个简单的Bitmap转换成Byte数组的代码:
ByteArrayOutputStream os = new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG, 70, os);
byte[] imgBytes = os.toByteArray();
bm是Bitmap对象。
相关文章推荐
- ASP模拟POST提交请求上传文件
- Java后台模拟向Servlet发送POST文件上传请求(转载)
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件(二)
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件(二)
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件
- android 文件上传(POST方式模拟表单提交)
- 模拟浏览器post请求 用java写上传文件后台
- asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- Java后台模拟向Servlet发送POST文件上传请求
- rest client模拟带上传文件的post请求到ror中
- [轉]ASP模拟POST提交请求上传文件
- PHP CURL 模拟POST请求 提交数据或上传文件
- 【转】asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- Android Volley框架的几种post提交请求方式及上传文件(Https单双向认证)
- java模拟post请求上传文件
- Android网络编程专栏--get和post请求,上传下载文件,提交Cookie
- PHP curl 模拟POST 上传文件
- 使用HttpURlConnection 发送POST请求上传文件(带参数)