Android http附带参数的文件上传
2015-11-21 21:04
633 查看
/** * * @param urlString 路径 * @param params 上传参数 * @param files 上传文件 * @return 返回结果 * @throws IOException */ public static String doPost(String urlString, Map<String, Object> params, Map<String, byte[]> files) throws IOException { 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(urlString+encodeParams(params)); HttpURLConnection conn = (HttpURLConnection) uri.openConnection(); conn.setReadTimeout(10 * 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); DataOutputStream outStream = new DataOutputStream( conn.getOutputStream()); if (files != null) { for (Map.Entry<String, byte[]> file : files.entrySet()) { StringBuilder sb1 = new StringBuilder(); sb1.append(PREFIX); sb1.append(BOUNDARY); sb1.append(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()); outStream.write(file.getValue()); outStream.write(LINEND.getBytes()); } } byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes(); outStream.write(end_data); outStream.flush(); int res = conn.getResponseCode(); String in = null; if (res == HttpURLConnection.HTTP_OK) { in = getStringFromStream(conn.getInputStream()); } return in; }
// Accept: text/plain, */*// Accept-Language: zh-cn// Host: 192.168.24.56// Content-Type:multipart/form-data;boundary=-----------------------------7db372eb000e2// User-Agent: WinHttpClient// Content-Length: 3693// Connection: Keep-Alive// -------------------------------7db372eb000e2// Content-Disposition: form-data; name="file"; filename="kn.jpg"// Content-Type: image/jpeg// (此处省略jpeg文件二进制数据...)// -------------------------------7db372eb000e2--
/*** 本方法需要导入httpmime的jar包** @param urlPath 请求路径* @param params 上传参数
* @param files 上传文件的本地路径
* @return 服务器返回结果
* @throws IOException*/public static String doPostByHttpClient(String urlPath, Map<String, Object> params, Map<String, String> files) throws IOException {HttpClient client = new DefaultHttpClient();HttpPost post = new HttpPost(urlPath);MultipartEntity mpEntity = new MultipartEntity();for (Map.Entry<String, String> file : files.entrySet()) {ContentBody cbFile = new FileBody(new File(file.getValue()));mpEntity.addPart(file.getKey(), cbFile);}for (Map.Entry<String, Object> entry : params.entrySet()) {mpEntity.addPart(entry.getKey(), new StringBody((String) entry.getValue(), "text/plain", Charset.forName("UTF-8")));}post.setEntity(mpEntity);String res = null;HttpResponse response = client.execute(post);if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {res = getStringFromStream(response.getEntity().getContent());}String content = EntityUtils.toString(response.getEntity());client.getConnectionManager().shutdown();return res;}
相关文章推荐
- 虚拟机网络连接方式
- (转载)AS3 HTTPCLIENT读取视频流信息
- 网络安全学习网站
- Netcat:TCP/IP瑞士军刀
- 基于ARP和WinPcap的网络嗅探
- 协议森林06 瑞士军刀 (ICMP协议)
- AngularJs $http 请求服务
- get与post区别详解,session与cookie区别,转发和重定向区别,如何实现session共享,webservice与httpservice区别
- http Basics
- 0024网络爬虫的基本原理(二)
- 0023网络爬虫的基本原理(一)
- AFN监测网络状态
- poj 1459 Power Network(网络流 最大流 dinic模板题)
- CornerStone使用教程(配置SVN,HTTP及svn简单使用)
- C#遍历窗体控件(原文出自http://www.liangshunet.com/ca/201403/286434593.htm)
- CCLHTTPServer
- 网络传输文件方案
- tcp协议中mss的理解
- tcp的精髓:滑动窗口
- 《图解HTTP》读书笔记