HTTP协议之multipart/form-data请求分析
问题引出
在解决文件上传附带参数的时候,发现当form中添加enctype:"multipart/form-data",后台用request.getParameter(“XXX”)获取不到数据
原因分析:
1.get方式
get方式提交的话,表单项都保存在http header中,格式是
http://localhost:8080/hello.do?name1=value1&name2=value2这样的字符串。server端通过request.getParameter是可以取到值的。
2.post方式(enctype为缺省的application/x-www-form-urlencoded)
表单数据都保存在http的正文部分,格式类似于下面这样:用request.getParameter是可以取到数据的
name1=value1&name2=value2
3.post方式(enctype为multipart/form-data,多用于文件上传,对于只想传value的做法,显然使用application/json或者text/plain会好很多。可以把数据放到form的header或者body中,在后台使用相应的方法得到具体值)
表单数据都保存在http的正文部分,各个表单项之间用boundary隔开。格式类似于下面这样:用request.getParameter()是取不到数据的,这时需要通过request.getInputStream来取数据,不过取到的是个InputStream,所以无法直接获取指定的表单项(需要自己对取到的流进行解析,才能得到表单项以及上传的文件内容等信息)。这种需求属于比较共通的功能,所以有很多开源的组件可以直接利用。比如:apache的fileupload组件,smartupload等。通过这些开源的upload组件提供的API,就可以直接从request中取得指定的表单项了。
什么是multipart/form-data请求:
1、multipart/form-data的基础方法是post,也就是说是由post方法来组合实现的
2、multipart/form-data与post方法的不同之处:请求头,请求体。
3、multipart/form-data的请求头必须包含一个特殊的头信息:Content-Type,且其值也必须规定为multipart/form-data,同时还需要规定一个内容分割符用于分割请求体中的多个post的内容,如文件内容和文本内容自然需要分割开来,不然接收方就无法正常解析和还原这个文件了。具体的头信息如下:
Content-Type: multipart/form-data; boundary=${bound}
//其中${bound} 是一个占位符,代表我们规定的分割符,可以自己任意规定,但为了避免和正常文本重复了,尽量要使用复杂一点的内容。如:--------------------56423498738365
4、multipart/form-data的请求体也是一个字符串,不过和post的请求体不同的是它的构造方式,post是简单的name=value值连接,而multipart/form-data则是添加了分隔符等内容的构造体。具体格式如下:
--${bound}
Content-Disposition: form-data; name="Filename"
HTTP.pdf
--${bound}
Content-Disposition: form-data; name="file000"; filename="HTTP协议详解.pdf"
Content-Type: application/octet-stream
%PDF-1.5
file content
%%EOF
--${bound}
Content-Disposition: form-data; name="Upload"
Submit Query
--${bound}--
其中${bound}为之前头信息中的分割符,如果头信息中规定为123,那么这里也要为123,;可以很容易看出,这个请求体是多个相同的部分组成的:每一个部分都是以--加分隔符开始的,然后是该部分内容的描述信息,然后一个回车,然后是描述信息的具体内容;如果传送的内容是一个文件的话,那么还会包含文件名信息,以及文件内容的类型。上面的第二个小部分其实是一个文件体的结构,最后会以--分割符--结尾,表示请求体结束。
综上,可以知道要发送一个multipart/form-data的请求,其实任何支持post请求的工具或语言都可以支持,只是自己要稍微包装一下便可。
form表单设置enctype="multipart/form-data"后获取参数
方案一:JS修改上传路径
<form id="upload" name="upload" action="fileftp.jsp" method="post" ENCTYPE="multipart/form-data">
<input type="hidden" name="otherName" id="otherName" value="abcdefg"/>
<td nowrap>
<input type="file" id="file1" name="file1" value="" size="40" class="sbttn"/>
<input type="submit" value="上传" class="sbttn"/>
</td>
</form>
当表单提交到fileftp.jsp后,是无法使用request.getParameter("otherName")获得到abcdefg的值的。但是可以获得到file1的文件。
这里使用JS在表单提交的时候将参数拼接在表单action地址的后面。代码像这样的:
<script language="javascript">
function formSubmit(){
var action="fileftp.jsp";
action+="?otherName="+document.upload.otherName.value;
document.upload.action=action;
document.upload.submit();
}
</script>
第二个示例
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>上传文件开通业务</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<script type="text/javascript">
function pidImport() {
var url = "/lobby/view/product/doUpload.jsp?";
var inputList = document.getElementsByTagName("input");
var pid = document.upload.pid.value;
url += "pid=" + pid;
document.all.upload.action = url;
document.all.upload.submit();
}
</script>
<body>
<form name="upload" action="/lobby/view/product/doUpload.jsp" method="post" enctype="multipart/form-data">
产品id: <input type="text" name="pid" size="10"/><br/>
选择文件:<input type="file" name="upfile" size="50"/><br/>
<input type="button" value="提交" οnclick="pidImport();">
</form>
</body>
</html>
接收的时候直接使用reqeust.getParameter("pid")即可
方案二:使用开源fileupload
程序要求:
1、提供一个 HTML 文件,用户可在相应表单中选择需要上传的文件;
2、编写一个名叫 UploadServlet 的Servlet 文件,主要功能是解析上面 HTML 表单所提交的 HTTP 请求,把普通的文本域和文件域分离开来;
3、UploadServlet 根据 web.xml 配置文件中的初始化参数确定好需要在 Web 服务器存放该文件的目录,将上传的文件写入到该存放目录中,在我的这里我把上传的文件保存到 F:\java\JavaWeb\ch05\uploadFile 目录中,而 F:\java\JavaWeb\ch05\temp 目录则用作该 Web 应用的临时目录。
程序代码:
upload.html 文件
<html>
<head> <title>Servlet 上传文件</title></head>
<body >
<form name="uploadForm" method="POST"
enctype="MULTIPART/FORM-DATA"
action="upload">
User Name:<input type="text" name="username" size="30"/>
Upload File1:<input type="file" name="file1" size="30"/>
Upload File2:<input type="file" name="file2" size="30"/>
<input type="submit" name="submit" value="上传">
<input type="reset" name="reset" value="重置">
</form>
</body>
</html>
UploadServlet.java 文件
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.servlet.*;
import org.apache.commons.fileupload.disk.*;
// Servlet 文件上传
public class UploadServlet extends HttpServlet
{
private String filePath; // 文件存放目录
private String tempPath; // 临时文件目录
// 初始化
public void init(ServletConfig config) throws ServletException
{
super.init(config);
// 从配置文件中获得初始化参数
filePath = config.getInitParameter("filepath");
tempPath = config.getInitParameter("temppath");
ServletContext context = getServletContext();
filePath = context.getRealPath(filePath);
tempPath = context.getRealPath(tempPath);
System.out.println("文件存放目录、临时文件目录准备完毕 ...");
}
// doPost
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
res.setContentType("text/plain;charset=gbk");
PrintWriter pw = res.getWriter();
try{
DiskFileItemFactory diskFactory = new DiskFileItemFactory();
// threshold 极限、临界值,即硬盘缓存 1M
diskFactory.setSizeThreshold(4 * 1024);
// repository 贮藏室,即临时文件目录
diskFactory.setRepository(new File(tempPath));
ServletFileUpload upload = new ServletFileUpload(diskFactory);
// 设置允许上传的最大文件大小 4M
upload.setSizeMax(4 * 1024 * 1024);
// 解析HTTP请求消息头
List fileItems = upload.parseRequest(req);
Iterator iter = fileItems.iterator();
while(iter.hasNext())
{
FileItem item = (FileItem)iter.next();
if(item.isFormField())
{
System.out.println("处理表单内容 ...");
processFormField(item, pw);
}else{
System.out.println("处理上传的文件 ...");
processUploadFile(item, pw);
}
}// end while()
pw.close();
}catch(Exception e){
System.out.println("使用 fileupload 包时发生异常 ...");
e.printStackTrace();
}// end try ... catch ...
}// end doPost()
// 处理表单内容
private void processFormField(FileItem item, PrintWriter pw)
throws Exception
{
String name = item.getFieldName();
String value = item.getString();
pw.println(name + " : " + value + "\r\n");
}
// 处理上传的文件
private void processUploadFile(FileItem item, PrintWriter pw)
throws Exception
{
// 此时的文件名包含了完整的路径,得注意加工一下
String filename = item.getName();
System.out.println("完整的文件名:" + filename);
int index = filename.lastIndexOf("\\");
filename = filename.substring(index + 1, filename.length());
long fileSize = item.getSize();
if("".equals(filename) && fileSize == 0)
{
System.out.println("文件名为空 ...");
return;
}
File uploadFile = new File(filePath + "/" + filename);
item.write(uploadFile);
pw.println(filename + " 文件保存完毕 ...");
pw.println("文件大小为 :" + fileSize + "\r\n");
}
// doGet
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
doPost(req, res);
}
}
web.xml 文件
<?xml version="1.0" encoding="gb2312"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>UploadServlet</servlet-class>
<init-param>
<param-name>filepath</param-name>
<param-value>uploadFile</param-value>
</init-param>
<init-param>
<param-name>temppath</param-name>
<param-value>temp</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>
</web-app>