在 Android 上通过模拟 HTTP multipart/form-data 请求协议信息实现图片上传

简介:

通过构造基于 HTTP 协议的传输内容实现图片自动上传到服务器功能 。如果自己编码构造 HTTP 协议,那么编写的代码质量肯定不高,建议模仿 HttpClient .zip examples\mime\ClientMultipartFormPost.java 来实现,并通过源码来进一步理解如何优雅高效地构造 HTTP 协议传输内容。

 

自己构造 HTTP 协议传输内容的想法,从何而来呢?灵感启迪于这篇博文“Android下的应用编程——用HTTP协议实现文件上传功能 ”,以前从未想过通过抓取 HTTP 请求数据格式,根据协议自己构造数据来实现数据提交。哎,Out 了。因为 Apache HttpClient 框架就是通过此方式来实现的,以前从未注意到,看来以后要多多向前人学习啊!结果是:阅读了此框架的源码后,才知道自己编写的代码和人家相比真不是一个档次的。现在已经下定决心了,多读开源框架代码,不但可以熟悉相关业务流程,而且还可以学到设计模式在实际业务需求中的应用,更重要的是领悟其中的思想。业务流程、实践能力、框架思想,一举三得,何乐而不为呢。^_^

 

test.html 部分源码:

<form action="Your_Action_Url " method="post" enctype="multipart/form-data " name="form1" id="form1">
  <p>
    <label for="upload_file"></label>
    <input type="file" name="upload_file" id="upload_file " />
  </p>
  <p>
    <input type="submit" name="action" id="action " value="upload " />
  </p>
</form>

通过 HttpWatch 查看抓取到的包数据格式:

通过 HttpWatch 查看抓取到的包数据格式

 

下面将分别通过按照 HttpWatch 抓取下来的协议格式内容构造传输内容实现文件上传功能和基于 HttpClient 框架实现文件上传功能。

项目配置目录Your_Project/config ,相关文件 如下:

actionUrl.properties 文件内容:

Your_Action_Url

formDataParams.properties 文件内容(对应 HTML Form 属性内容):

action =upload

imageParams.properties 文件内容(这里文件路径已配置死了,不好!建议在程序中动态设置,即通过传入相关参数实现。):

upload_file =images/roewe.jpg

MIMETypes.properties 文件内容(参考自 Multimedia MIME Reference ):

jpeg:image/jpeg
jpg:image/jpeg
png:image/png
gif:image/gif

 

1. 在《Android下的应用编程——用HTTP协议实现文件上传功能 》代码的基础上,通过进一步改进得到如下代码(Java、Android 都可以 run):

Java代码   收藏代码
  1. /** 
  2.  * 文件名称:UploadImage.java 
  3.  * 
  4.  * 版权信息:Apache License, Version 2.0 
  5.  * 
  6.  * 功能描述:实现图片文件上传。 
  7.  * 
  8.  * 创建日期:2011-5-10 
  9.  * 
  10.  * 作者:Bert Lee 
  11.  */  
  12.   
  13. /* 
  14.  * 修改历史: 
  15.  */  
  16. public class UploadImage {  
  17.     String multipart_form_data = "multipart/form-data";  
  18.     String twoHyphens = "--";  
  19.     String boundary = "****************fD4fH3gL0hK7aI6";    // 数据分隔符  
  20.     String lineEnd = System.getProperty("line.separator");    // The value is "\r\n" in Windows.  
  21.       
  22.     /* 
  23.      * 上传图片内容,格式请参考HTTP 协议格式。 
  24.      * 人人网Photos.upload中的”程序调用“http://wiki.dev.renren.com/wiki/Photos.upload#.E7.A8.8B.E5.BA.8F.E8.B0.83.E7.94.A8 
  25.      * 对其格式解释的非常清晰。 
  26.      * 格式如下所示: 
  27.      * --****************fD4fH3hK7aI6 
  28.      * Content-Disposition: form-data; name="upload_file"; filename="apple.jpg" 
  29.      * Content-Type: image/jpeg 
  30.      * 
  31.      * 这儿是文件的内容,二进制流的形式 
  32.      */  
  33.     private void addImageContent(Image[] files, DataOutputStream output) {  
  34.         for(Image file : files) {  
  35.             StringBuilder split = new StringBuilder();  
  36.             split.append(twoHyphens + boundary + lineEnd);  
  37.             split.append("Content-Disposition: form-data; name=\"" + file.getFormName() + "\"; filename=\"" + file.getFileName() + "\"" + lineEnd);  
  38.             split.append("Content-Type: " + file.getContentType() + lineEnd);  
  39.             split.append(lineEnd);  
  40.             try {  
  41.                 // 发送图片数据  
  42.                 output.writeBytes(split.toString());  
  43.                 output.write(file.getData(), 0, file.getData().length);  
  44.                 output.writeBytes(lineEnd);  
  45.             } catch (IOException e) {  
  46.                 throw new RuntimeException(e);  
  47.             }  
  48.         }  
  49.     }  
  50.       
  51.     /* 
  52.      * 构建表单字段内容,格式请参考HTTP 协议格式(用FireBug可以抓取到相关数据)。(以便上传表单相对应的参数值) 
  53.      * 格式如下所示: 
  54.      * --****************fD4fH3hK7aI6 
  55.      * Content-Disposition: form-data; name="action" 
  56.      * // 一空行,必须有 
  57.      * upload 
  58.      */  
  59.     private void addFormField(Set<Map.Entry<Object,Object>> params, DataOutputStream output) {  
  60.         StringBuilder sb = new StringBuilder();  
  61.         for(Map.Entry<Object, Object> param : params) {  
  62.             sb.append(twoHyphens + boundary + lineEnd);  
  63.             sb.append("Content-Disposition: form-data; name=\"" + param.getKey() + "\"" + lineEnd);  
  64.             sb.append(lineEnd);  
  65.             sb.append(param.getValue() + lineEnd);  
  66.         }  
  67.         try {  
  68.             output.writeBytes(sb.toString());// 发送表单字段数据  
  69.         } catch (IOException e) {  
  70.             throw new RuntimeException(e);  
  71.         }  
  72.     }  
  73.       
  74.     /** 
  75.      * 直接通过 HTTP 协议提交数据到服务器,实现表单提交功能。 
  76.      * @param actionUrl 上传路径 
  77.      * @param params 请求参数key为参数名,value为参数值 
  78.      * @param files 上传文件信息 
  79.      * @return 返回请求结果 
  80.      */  
  81.     public String post(String actionUrl, Set<Map.Entry<Object,Object>> params, Image[] files) {  
  82.         HttpURLConnection conn = null;  
  83.         DataOutputStream output = null;  
  84.         BufferedReader input = null;  
  85.         try {  
  86.             URL url = new URL(actionUrl);  
  87.             conn = (HttpURLConnection) url.openConnection();  
  88.             conn.setConnectTimeout(120000);  
  89.             conn.setDoInput(true);        // 允许输入  
  90.             conn.setDoOutput(true);        // 允许输出  
  91.             conn.setUseCaches(false);    // 不使用Cache  
  92.             conn.setRequestMethod("POST");  
  93.             conn.setRequestProperty("Connection""keep-alive");  
  94.             conn.setRequestProperty("Content-Type", multipart_form_data + "; boundary=" + boundary);  
  95.               
  96.             conn.connect();  
  97.             output = new DataOutputStream(conn.getOutputStream());  
  98.               
  99.             addImageContent(files, output);    // 添加图片内容  
  100.               
  101.             addFormField(params, output);    // 添加表单字段内容  
  102.               
  103.             output.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);// 数据结束标志  
  104.             output.flush();  
  105.               
  106.             int code = conn.getResponseCode();  
  107.             if(code != 200) {  
  108.                 throw new RuntimeException("请求‘" + actionUrl +"’失败!");  
  109.             }  
  110.               
  111.             input = new BufferedReader(new InputStreamReader(conn.getInputStream()));  
  112.             StringBuilder response = new StringBuilder();  
  113.             String oneLine;  
  114.             while((oneLine = input.readLine()) != null) {  
  115.                 response.append(oneLine + lineEnd);  
  116.             }  
  117.               
  118.             return response.toString();  
  119.         } catch (IOException e) {  
  120.             throw new RuntimeException(e);  
  121.         } finally {  
  122.             // 统一释放资源  
  123.             try {  
  124.                 if(output != null) {  
  125.                     output.close();  
  126.                 }  
  127.                 if(input != null) {  
  128.                     input.close();  
  129.                 }  
  130.             } catch (IOException e) {  
  131.                 throw new RuntimeException(e);  
  132.             }  
  133.               
  134.             if(conn != null) {  
  135.                 conn.disconnect();  
  136.             }  
  137.         }  
  138.     }  
  139.       
  140.     public static void main(String[] args) {  
  141.         try {  
  142.             String response = "";  
  143.               
  144.             BufferedReader in = new BufferedReader(new FileReader("config/actionUrl.properties"));  
  145.             String actionUrl = in.readLine();  
  146.               
  147.             // 读取表单对应的字段名称及其值  
  148.             Properties formDataParams = new Properties();  
  149.             formDataParams.load(new FileInputStream(new File("config/formDataParams.properties")));  
  150.             Set<Map.Entry<Object,Object>> params = formDataParams.entrySet();  
  151.               
  152.             // 读取图片所对应的表单字段名称及图片路径  
  153.             Properties imageParams = new Properties();  
  154.             imageParams.load(new FileInputStream(new File("config/imageParams.properties")));  
  155.             Set<Map.Entry<Object,Object>> images = imageParams.entrySet();  
  156.             Image[] files = new Image[images.size()];  
  157.             int i = 0;  
  158.             for(Map.Entry<Object,Object> image : images) {  
  159.                 Image file = new Image(image.getValue().toString(), image.getKey().toString());  
  160.                 files[i++] = file;  
  161.             }  
  162. //            Image file = new Image("images/apple.jpg", "upload_file");  
  163. //            Image[] files = new Image[0];  
  164. //            files[0] = file;  
  165.               
  166.             response = new UploadImage().post(actionUrl, params, files);  
  167.             System.out.println("返回结果:" + response);  
  168.         } catch (IOException e) {  
  169.             e.printStackTrace();  
  170.         }  
  171.     }  
  172. }  


2. 基于 HttpClient 框架实现文件上传,实例代码如下:

Java代码   收藏代码
  1. /** 
  2.  * 文件名称:ClientMultipartFormPost.java 
  3.  * 
  4.  * 版权信息:Apache License, Version 2.0 
  5.  * 
  6.  * 功能描述:通过 HttpClient 4.1.1 实现文件上传。 
  7.  * 
  8.  * 创建日期:2011-5-15 
  9.  * 
  10.  * 作者:Bert Lee 
  11.  */  
  12.   
  13. /* 
  14.  * 修改历史: 
  15.  */  
  16. public class ClientMultipartFormPost {  
  17.     /** 
  18.      * 直接通过 HttpMime's MultipartEntity 提交数据到服务器,实现表单提交功能。 
  19.      * @return Post 请求所返回的内容 
  20.      */  
  21.     public static String filePost() {  
  22.         HttpClient httpclient = new DefaultHttpClient();  
  23.           
  24.         try {  
  25.             BufferedReader in = new BufferedReader(new FileReader("config/actionUrl.properties"));  
  26.             String actionUrl;  
  27.             actionUrl = in.readLine();  
  28.             HttpPost httppost = new HttpPost(actionUrl);  
  29.               
  30.             // 通过阅读源码可知,要想实现图片上传功能,必须将 MultipartEntity 的模式设置为 BROWSER_COMPATIBLE 。  
  31.             MultipartEntity multiEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  
  32. //            MultipartEntity multiEntity = new MultipartEntity();  
  33.               
  34.             // 读取图片的 MIME Type 类型集  
  35.             Properties mimeTypes = new Properties();  
  36.             mimeTypes.load(new FileInputStream(new File("config/MIMETypes.properties")));  
  37.               
  38.             // 构造图片数据  
  39.             Properties imageParams = new Properties();  
  40.             imageParams.load(new FileInputStream(new File("config/imageParams.properties")));  
  41.             String fileType;  
  42.             for(Map.Entry<Object,Object> image : imageParams.entrySet()) {  
  43.                 String path = image.getValue().toString();  
  44.                 fileType = path.substring(path.lastIndexOf(".") + 1);  
  45.                 FileBody binaryContent = new FileBody(new File(path), mimeTypes.get(fileType).toString());  
  46. //                FileBody binaryContent = new FileBody(new File(path));  
  47.                 multiEntity.addPart(image.getKey().toString(), binaryContent);  
  48.             }  
  49.               
  50.             // 构造表单参数数据  
  51.             Properties formDataParams = new Properties();  
  52.             formDataParams.load(new FileInputStream(new File("config/formDataParams.properties")));  
  53.             for(Entry<Object, Object> param : formDataParams.entrySet()) {  
  54.                 multiEntity.addPart(param.getKey().toString(), new StringBody(param.getValue().toString()));  
  55.             }  
  56.               
  57.             httppost.setEntity(multiEntity);  
  58. //            Out.println("executing request " + httppost.getRequestLine());  
  59.               
  60.             HttpResponse response = httpclient.execute(httppost);  
  61.             HttpEntity resEntity = response.getEntity();  
  62.               
  63. //            Out.println("-------------------");  
  64. //            Out.println(response.getStatusLine());  
  65.             if(resEntity != null) {  
  66.                 String returnContent = EntityUtils.toString(resEntity);  
  67.                 EntityUtils.consume(resEntity);  
  68.                   
  69.                 return returnContent; // 返回页面内容  
  70.             }  
  71.         } catch (IOException e) {  
  72.             e.printStackTrace();  
  73.         } finally {  
  74.             // 释放资源  
  75.             httpclient.getConnectionManager().shutdown();  
  76.         }  
  77.         return null;  
  78.     }  
  79.   
  80.     // 测试  
  81.     public static void main(String[] args) {  
  82.         Out.println("Response content: " + ClientMultipartFormPost.filePost());  
  83.     }  
  84.   
  85. }  
 

 

参考资料:


相关文章
|
17天前
|
网络协议 Linux iOS开发
推荐:实现RTSP/RTMP/HLS/HTTP协议的轻量级流媒体框架,支持大并发连接请求
推荐:实现RTSP/RTMP/HLS/HTTP协议的轻量级流媒体框架,支持大并发连接请求
38 1
|
1月前
|
网络协议 Linux
HTTP协议基本原理简介(二)
HTTP协议基本原理简介(二)
23 1
|
1月前
|
缓存 前端开发
HTTP协议基本原理简介(三)
HTTP协议基本原理简介(三)
14 1
|
2天前
|
网络协议 Java API
深度剖析:Java网络编程中的TCP/IP与HTTP协议实践
【4月更文挑战第17天】Java网络编程重在TCP/IP和HTTP协议的应用。TCP提供可靠数据传输,通过Socket和ServerSocket实现;HTTP用于Web服务,常借助HttpURLConnection或Apache HttpClient。两者结合,构成网络服务基础。Java有多种高级API和框架(如Netty、Spring Boot)简化开发,助力高效、高并发的网络通信。
|
3天前
|
网络协议 安全 API
Android网络和数据交互: 什么是HTTP和HTTPS?在Android中如何进行网络请求?
HTTP和HTTPS是网络数据传输协议,HTTP基于TCP/IP,简单快速,HTTPS则是加密的HTTP,确保数据安全。在Android中,过去常用HttpURLConnection和HttpClient,但HttpClient自Android 6.0起被移除。现在推荐使用支持TLS、流式上传下载、超时配置等特性的HttpsURLConnection进行网络请求。
5 0
|
4天前
|
缓存 安全 网络协议
Http协议是什么
【4月更文挑战第12天】HTTP是用于从WWW服务器传输超文本到浏览器的协议,基于TCP/IP,特点包括无连接、无状态、面向对象、无阻塞和可缓存。它的工作原理是客户端发送请求,服务器处理后返回响应。自1989年创建以来,HTTP已发展支持多媒体内容传输,并通过HTTPS提供安全保护。学习更多可参考计算机网络技术文献。
15 6
|
6天前
|
存储 JSON 前端开发
网络原理(4)HTTP协议(下)
网络原理(4)HTTP协议
19 0
|
17天前
|
XML JSON JavaScript
推荐一个比较好用的c++版本http协议库-cpp-httplib
推荐一个比较好用的c++版本http协议库-cpp-httplib
36 1
|
21天前
解决Error:All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com
解决Error:All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com
21 5
|
24天前
|
运维 监控 算法
slb监听协议http
SLB的HTTP监听器用于处理HTTP请求,配置时选择协议类型为HTTP和前端端口(如80)。SLB根据负载算法将请求转发至健康后端服务器,并支持会话保持。通过`X-Forwarded-For`和`X-Forwarded-Proto`头处理请求信息。由于不涉及SSL/TLS,数据传输不安全,推荐升级至HTTPS以加密通信。SLB提供性能监控和故障排查工具,帮助运维管理。
22 5