http html form表单 post上传文件
DEMO文件
新建一个servlet.java并在web.xml配置url
package top.lishuoboy.servlet;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ServletTest
*/
public class ServletTest extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public ServletTest() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
res.setCharacterEncoding("UTF-8");
System.out.println("get来啦=================================================");
// System.out.println(req.getAuthType());
// System.out.println(req.getCharacterEncoding());
// System.out.println(req.getContentLength());
// System.out.println(req.getContentLengthLong());
// System.out.println(req.getContentType());
// System.out.println(req.getContextPath());
// System.out.println(req.getLocalAddr());
// System.out.println(req.getLocalName());
// System.out.println(req.getLocalPort());
// System.out.println(req.getMethod());
// System.out.println(req.getPathInfo());
// System.out.println(req.getPathTranslated());
// System.out.println(req.getProtocol());
// System.out.println(req.getQueryString());
// System.out.println(req.getRemoteAddr());
// System.out.println(req.getRemoteHost());
// System.out.println(req.getRemotePort());
// System.out.println(req.getRemoteUser());
// System.out.println(req.getRequestedSessionId());
// System.out.println(req.getRequestURL());
// System.out.println(req.getScheme());
// System.out.println(req.getServerName());
// System.out.println(req.getServerPort());
// System.out.println(req.getServletPath());
// System.out.println(req.getServletContext());
System.out.println("获取headers=================================");
Enumeration<String> headerNames = req.getHeaderNames();
while (headerNames.hasMoreElements()) {
String header = headerNames.nextElement();
System.out.println(header + "=" + req.getHeader(header));
}
System.out.println("获取请求头 parameters =================================");
Enumeration<String> parameterNames = req.getParameterNames();
while (parameterNames.hasMoreElements()) {
String parameterName = parameterNames.nextElement();
System.out.println(parameterName + "=" + req.getParameter(parameterName));
}
System.out.println("获取请求头 attributeNames =================================");
Enumeration<String> attributeNames = req.getAttributeNames();
while (attributeNames.hasMoreElements()) {
String attributeName = attributeNames.nextElement();
System.out.println(attributeName + "=" + req.getAttribute(attributeName));
}
/*
* System.out.println("获取请求头 body(字符流) =================================");
* BufferedReader br = null; StringBuilder sb = new StringBuilder(""); try { br
* = req.getReader(); String str; while ((str = br.readLine()) != null) {
* sb.append(str+"\n"); } br.close(); } catch (IOException e) {
* e.printStackTrace(); } finally { if (null != br) { try { br.close(); } catch
* (IOException e) { e.printStackTrace(); } } }
* System.out.println(sb.toString());
*/
System.out.println("获取请求头 body(字节流) =================================");
File file = new File("D:\\222.png");
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
BufferedInputStream bis = null;
try {
InputStream is = req.getInputStream();
bis = new BufferedInputStream(is);
int length;
byte[] b = new byte[8 * 1024];
while ((length = bis.read(b)) != -1) {
bos.write(b, 0, length);
}
bos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
}
res.sendRedirect("get.html");
// res.getWriter().append("<a href=\"http://www.baidu.com\">baidu</a>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
res.setCharacterEncoding("UTF-8");
System.out.println("post来啦=================================================");
System.out.println("获取headers=================================");
Enumeration<String> headerNames = req.getHeaderNames();
while (headerNames.hasMoreElements()) {
String header = headerNames.nextElement();
System.out.println(header + "=" + req.getHeader(header));
}
System.out.println("获取请求头 parameters =================================");
Enumeration<String> parameterNames = req.getParameterNames();
while (parameterNames.hasMoreElements()) {
String parameterName = parameterNames.nextElement();
System.out.println(parameterName + "=" + req.getParameter(parameterName));
}
System.out.println("获取请求头 attributeNames =================================");
Enumeration<String> attributeNames = req.getAttributeNames();
while (attributeNames.hasMoreElements()) {
String attributeName = attributeNames.nextElement();
System.out.println(attributeName + "=" + req.getAttribute(attributeName));
}
/*
* System.out.println("获取请求头 body(字符流) =================================");
* BufferedReader br = null; StringBuilder sb = new StringBuilder(""); try { br
* = req.getReader(); String str; while ((str = br.readLine()) != null) {
* sb.append(str+"\n"); } br.close(); } catch (IOException e) {
* e.printStackTrace(); } finally { if (null != br) { try { br.close(); } catch
* (IOException e) { e.printStackTrace(); } } }
* System.out.println(sb.toString());
*/
System.out.println("获取请求头 body(字节流) =================================");
File file = new File("D:\\222.png");
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
BufferedInputStream bis = null;
try {
InputStream is = req.getInputStream();
bis = new BufferedInputStream(is);
int length;
byte[] b = new byte[10 * 1024 * 1024];
while ((length = bis.read(b)) != -1) {
System.out.println(length);
bos.write(b, 0, length);
}
bos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
}
res.sendRedirect("post.html");
// res.getWriter().append("<a href=\"http://www.baidu.com\">baidu</a>");
}
}
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<servlet>
<servlet-name>ServletTest</servlet-name>
<display-name>ServletTest</display-name>
<description></description>
<servlet-class>top.lishuoboy.servlet.ServletTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletTest</servlet-name>
<url-pattern>/ServletTest</url-pattern>
</servlet-mapping>
</web-app>
get方法form表单文件get.html
<html>
<head>
<meta charset="UTF-8">
<title>get</title>
</head>
<body>
<h1>get</h1>
<form name="formGet" method="get" action="ServletTest" enctype="multipart/form-data">
<div>
用户名: <input name="name" type="text" value="张 三" />
</div>
<div>
密码: <input name="pwd" type="password" value="000 000" />
</div>
<div>
文件: <input name="file" type="file"/>
</div>
<div>
<input type="submit">
</div>
</form>
</body>
</html>
post方法form表单文件post.html
<html>
<head>
<meta charset="UTF-8">
<title>post</title>
</head>
<body>
<h1>post</h1>
<form name="formPost" method="post" action="ServletTest" enctype="multipart/form-data">
<div>
用户名: <input name="name" type="text" value="张 三" />
</div>
<div>
密码: <input name="pwd" type="password" value="000 000" />
</div>
<div>
文件: <input name="file" type="file" />
</div>
<div>
<input type="submit">
</div>
</form>
</body>
</html>
结论
client | method | enctype | 结论 |
chrome | get | application/x-www-form-urlencoded | 无文件流,file作为parameter传入 |
post | application/x-www-form-urlencoded | 无文件流,file作为parameter传入 | |
multipart/form-data | parameter作为body传入,header的content-type和body里还夹杂着boundary | ||
text/plain | parameter作为body传入,file只是键值对 | ||
POSTMAN | get | application/x-www-form-urlencoded | 传不了文件,但是可以传文本body |
multipart/form-data | header的content-type和body里还夹杂着boundary | ||
text/plain(raw) | 传不了文件,但是可以传文本body | ||
binary | 正常 | ||
post | application/x-www-form-urlencoded | 传不了文件,body文本作为parameter传入 | |
multipart/form-data | header的content-type和body里还夹杂着boundary | ||
text/plain(raw) | 传不了文件,但是可以传文本body | ||
binary | 正常 |