public static String post(String actionUrl, Map<String, String> params,
Map<String, File> files) throws IOException {
StringBuilder sb2 = new StringBuilder();
String BOUNDARY = java.util.UUID.randomUUID().toString();
String PREFIX = "--" , LINEND = "\r\n";
String MULTIPART_FROM_DATA = "multipart/form-data";
String CHARSET = "UTF-8";
URL uri = new URL(actionUrl);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
conn.setReadTimeout(5 * 1000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY);
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
sb.append(PREFIX);
sb.append(BOUNDARY);
sb.append(LINEND);
sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINEND);
sb.append("Content-Type: text/plain; charset=" + CHARSET+LINEND);
sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
sb.append(LINEND);
sb.append(entry.getValue());
sb.append(LINEND);
}
DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
outStream.write(sb.toString().getBytes());
if(files!=null){
//int i = 0;
for (Map.Entry<String, File> file: files.entrySet()) {
StringBuilder sb1 = new StringBuilder();
sb1.append(PREFIX);
sb1.append(BOUNDARY);
sb1.append(LINEND);
sb1.append("Content-Disposition: form-data; name=\"file"+(i++)+"\"; filename=\""+file.getKey()+"\""+LINEND);
//sb1.append("Content-Disposition: form-data; name=\"userupfile\"; filename=\""+file.getKey()+"\""+LINEND);
sb1.append("Content-Type: application/octet-stream; charset="+CHARSET+LINEND);
sb1.append(LINEND);
outStream.write(sb1.toString().getBytes());
InputStream is = new FileInputStream(file.getValue());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
is.close();
outStream.write(LINEND.getBytes());
}
}
byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
outStream.write(end_data);
outStream.flush();
int res = conn.getResponseCode();
InputStream in = null;
if (res == 200) {
in = conn.getInputStream();
int ch;
while ((ch = in.read()) != -1) {
sb2.append((char) ch);
}
Log.i("CAMERA", sb2.toString());
}
return in == null ? null : sb2.toString();
}
分享到:
相关推荐
Android端使用get post 方法提交数据到服务器demo 传智播客张泽华Android视频54-57代码 包括web和Android端 仅供学习
Android Studio实现POST请求,每个步骤都有注释,比较适合萌新了解如果通过POST方式发起网络请求。
C# 以Post方式提交数据 C# 以Post方式提交数据
java、android http使用post进行提交数据,把数据提交给服务器,服务器通过获取可以存储到数据库
POST提交数据加密使用HttpWatch获取的数据是加密后的 用户名user1 密码:123456
android采用post方式获取服务器数据,这是源代码和例子。
c# post 提交数据 测试通过 上传代码
android 后台 post和get 请求数据。定义接收数据编码
android get和post 获取json xml数据
post方式提交数据 android 老鸟勿入
android HttpClient post上传文件
Android GetPost数据提交实例,发送post必须设置 conn.setDoInput(true);conn.setDoOutput(true);这两行代码,从用户界面提交过来的数据上传到服务器接收,类似于WEB网页提交表单的操作,不过Android有自己独特的...
易语言post提交示例,易语言post提交示例易语言post提交示例易语言post提交示例易语言post提交示例
老罗Android开发视频教程汇总课程目录:【】android之http协议编程第1集:http协议的介绍【】android之http协议编程第2集:http协议GET方式获取图片【】android之http协议编程第3集:使用Post方式进行提交数据【】...
libcurl 提交post表单数据 上传图片 demo 根据网页请求协议,模拟post表单的方式上传数据,并最返回数据进行处理
VB开发的通过httppost向服务器提交数据。Http协议Post提交。VB开发的通过httppost向服务器提交数据。Http协议Post提交
如何使用post提交网页表单中的数据,vb使用post提交数据
此demo演示android通过get和post请求方法同服务器交互,测试需要tomcat,具体参考 http://blog.csdn.net/youmingyu/article/details/52524538
Android客户端post上传字段和多张图片 c#接收
《Android开发案例驱动教程》 配套代码。 注: 由于第12,13,14章代码太大,无法上传到一个包中。 这三节代码会放到其他压缩包中。 作者:关东升,赵志荣 Java或C++程序员转变成为Android程序员 采用案例驱动模式...