RFC1867协议介绍

           RFC1867协议主要是在HTTP协议的基础上为INPUT标签添加了file属性。同一时候限定了Form的method必须为POST,ENCTYPE必须为multipart/form-data。 其他属性标签, <INPUT
TYPE=file>标记能够有一个VALUE属性来指定默认的文件名 ,能够用“SIZE=宽,高”来指定SIZE属性 。

multipart/form-data

multipart/form-data的媒体内容遵从RFC 1521所规定的多部分的数据流规则。

它主要被用来描写叙述表单填写后返回的数据。在一个表单中(这里指的是HTML,当然其它一些应用也可 能使用表单),有一系列字段提供给用户进行填写。每一个字段都有自己的名字。

在一个确定 的表单中。每一个名字都是唯一的。  

       multipart/form-data由多个部分组成,每一部分都有一个content-disposition标题头,它的 值是"form-data"。它的属性指明了其在表单内的字段名。举例来说,'content-disposition:  form-data; name="xxxxx"',这里的xxxxx就是相应于该字段的字段名。假设字段名包括非 ASCII码字符的话。还应该依照RFC 1522里面所规定的方法进行编码。

对全部的多部分MIME类型来说,每一部分有一个可选的Content-Type,默认的值是 text/plain。假设文件的内容是通过表单填写上传返回的话。那么输入的文件就被定义为 application/octet-stream,或者,假设知道是什么类型的话,就定义为对应的媒体类型。如 果一个表单返回多个文件,那么它们就作为multipart/form-data中所结合的multipart/mixed 被返回。  

假设所传送的内容不符合默认的编码方式的话。该部分都将被编码,并加上 "content-transfer-encoding"的标题头。

Android Post上传文件的实现

Android POST方式上传文件,能够基于通过 RFC1867协议来实现。

	/**
*
* @param urlPath
* @param params
* map 參数 <參数名称 , 參数值>
* @param fileParams
* map 文件类型 參数 <參数名称 , 文件路径>
*
*/
public String postFile(String urlPath, Map<String, Object> params,
Map<String, String> fileParams) throws FileNotFoundException {
String PREFIX = "--"; // 前缀
String LINE_END = "\r\n"; // 换行
String BOUNDARY = UUID.randomUUID().toString(); // 边界标识
URL url;
HttpURLConnection connection;
try { url = new URL(urlPath); connection = (HttpURLConnection) url.openConnection();
// 设置超时时间
connection.setReadTimeout(readTimeOut);
connection.setConnectTimeout(connectTimeOut);
// 请求方式
connection.setRequestMethod("POST");
connection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
// 开启输入流
connection.setDoInput(true);
// 开启输出流
connection.setDoOutput(true);
// 关闭缓存
connection.setUseCaches(false);
// 设置编码
connection.setRequestProperty("Charset", "utf-8");
connection.setRequestProperty("connection", "keep-alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
// 设置内容类型及定义BOUNDARY
connection.setRequestProperty("Content-Type", "multipart/form-data"
+ ";boundary=" + BOUNDARY); // 获取输出流
DataOutputStream dos = new DataOutputStream(
connection.getOutputStream());
StringBuffer sb = null; String result = "";
String paramStr;
// 发送非文件參数
if (mParams != null && mParams.size() > 0) { Iterator<String> it = mParams.keySet().iterator();
while (it.hasNext()) {
sb = null;
sb = new StringBuffer();
String key = it.next();
Object value = mParams.get(key);
sb.append(PREFIX).append(BOUNDARY).append(LINE_END);
sb.append("Content-Disposition: form-data; name=\"")
.append(key).append("\"").append(LINE_END)
.append(LINE_END);
sb.append(value).append(LINE_END);
paramStr = sb.toString(); dos.write(paramStr.getBytes());
dos.flush();
}
}
paramStr = null;
// 发送文件參数,读取文件流写入post输出流
if (mFileParams != null && !mFileParams.isEmpty()) { Iterator<Entry<String, String>> fileIter = mFileParams
.entrySet().iterator(); while (fileIter.hasNext()) {
sb = null;
sb = new StringBuffer();
Entry<String, String> entry = fileIter.next();
String fileKey = entry.getKey();
String filePath = entry.getValue(); File file = new File(filePath); if (file.exists() == false) { throw new FileNotFoundException();
}
// 设置边界标示,设置 Content-Disposition头传入文件流 sb.append(PREFIX).append(BOUNDARY).append(LINE_END);
sb.append("Content-Disposition:form-data; name=\""
+ fileKey + "\"; filename=\"" + file.getName()
+ "\"" + LINE_END);
sb.append("Content-Type:" + CONTENT_TYPE + LINE_END);
sb.append(LINE_END);
dos.write(sb.toString().getBytes()); InputStream is = new FileInputStream(file); byte[] bytes = new byte[1024];
int len = 0; while ((len = is.read(bytes)) != -1) { dos.write(bytes, 0, len); }
is.close();
dos.write(LINE_END.getBytes()); dos.flush();
}
byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINE_END)
.getBytes();
dos.write(end_data);
dos.flush();
}
dos.close();
int res = getResponseCode();
// 返回成功
if (res == 200) {
InputStream input = conn.getInputStream();
StringBuffer sb1 = new StringBuffer();
int ss;
while ((ss = input.read()) != -1) {
sb1.append((char) ss);
}
result = sb1.toString();
return result;
} else {
}
} catch (MalformedURLException e) {
Log.i(TAG, "MalformedURLException error");
} catch (IOException e) {
Log.i(TAG, "IOException error");
}
return null;
}

</pre><pre>

文章地址 :http://blog.csdn.net/jmq_0000/article/details/30244297


Android Http POST文件上传之-----RFC1867协议的更多相关文章

  1. Android+jsp +html 文件上传案例 已测试 成功通过

    我文件上传一直是广大读者一个问题 今天就把成功案例写下 javaweb 网页前段 <%@ page language="java" import="java.uti ...

  2. web端、android端的文件上传

    1.web端的文件上传. 这里是利用了第三方的jar包.这里所需要的jar包我已经上传到本博客的资源里了,以下是连接 http://download.csdn.net/detail/caihongsh ...

  3. Python Socket实现文件上传(TCP协议)

    在TCP协议下通过socket模块实现文件上传 #!/usr/bin/env python # -*- coding: utf-8 -*- # desc: tcp_server_file_upload ...

  4. 【翻译】tus----一个可续传文件上传的开放协议

    tus tus是一个可续穿文件上传协议,它以Http协议为载体,统一了一个文件断点续传的标准. 这篇文章翻译自https://tus.io/ 目前该协议版本信息如下: Version: 1.0.0 ( ...

  5. TCP大文件上传与UDP协议

    一.UCP大文件上传(解决粘包问题) ①客户端 import socket, os, json, struct client = socket.socket() client.connect(('12 ...

  6. Android + https 实现 文件上传

    package com.example.wbdream.zigvine; import android.annotation.SuppressLint; import android.app.Acti ...

  7. 让Android中的webview支持页面中的文件上传

    android webview在默认情况下是不支持网页中的文件上传功能的: 如果在网页中有<input type="file" />,在android webview中 ...

  8. 补习系列(11)-springboot 文件上传原理

    目录 一.文件上传原理 二.springboot 文件机制 临时文件 定制配置 三.示例代码 A. 单文件上传 B. 多文件上传 C. 文件上传异常 D. Bean 配置 四.文件下载 小结 一.文件 ...

  9. 【SpringMVC】SpringMVC 实现文件上传

    SpringMVC 实现文件上传 文章源码 文件上传回顾 查看 JavaWeb 阶段的文件上传下载 实现步骤: 客户端: 发送 post 请求,告诉服务器要上传什么文件 服务器: 要有一个 form ...

随机推荐

  1. spoj p104 Matrix-Tree定理

    这个问题就是经典的生成树记数问题,题目为spoj p104 highway. 首先我们引入Matrix-Tree定理,由kirchhoff证明,定理的概述为,对于图G,我们定义若干个矩阵, D[G], ...

  2. bzoj 1951 lucas crt 费马小定理

    首先假设输入的是n,m 我们就是要求m^(Σ(c(n,i) i|n)) mod p 那么根据费马小定理,上式等于 m^(Σ(c(n,i) i|n) mod  (p-1)) mod p 那么问题的关键就 ...

  3. .NET中zip的压缩和解压

    在.NET可以通过多种方式实现zip的压缩和解压:1.使用System.IO.Packaging:2.使用第三方类库:3.通过 System.IO.Compression 命名空间中新增的ZipArc ...

  4. hdu 2817 A sequence of numbers(快速幂取余)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2817 题目大意:给出三个数,来判断是等差还是等比数列,再输入一个n,来计算第n个数的值. #inclu ...

  5. hashlib模块加密用法

    hashlib 加密模块 hashlib.md5() 构建一个md5的对象,用于调用对象的update方法去加密   例子: import hashlib hash = hashlib.md5() h ...

  6. HDU1028 (整数拆分)

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  7. WPF中添加一个文本输入框,按Enter回车,执行绑定的Command

    在WPF+WMMV模式中使用键盘和鼠标事件的绑定代码如下: <TextBox x:Name="SearchBox" Text="{Binding SearchTex ...

  8. jquery请求格式和返回类型 汇总

    常规请求基本格式 1 [WebMethod] 2 public string SayHello(string name) 3 { 4 return "Hello " + name; ...

  9. pandas求五日线并画图

    import pandas as pd import numpy as np import matplotlib.pyplot as plt stock_data = pd.read_csv('000 ...

  10. laravel获取checkbox值的小技巧

    以前老是用三元运算符来判断,现在有了更好的方法: 1.html代码 <input type="hidden" name="approved" value= ...