1.multipart/form-data基础方式是post,也就是说通过post组合方式来实现的。
2,form 表单中用enctype="application/x-www-form-urlencoded"会报错这个错: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded
3在WebContent下建NewFile.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="UserServlet" enctype="multipart/form-data" method="post">
<input type="file" name="file">
<input type="submit" value="提交">
</form>
</body>
</html>
4.在WebContent下建index.jsp,代码如下
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<img alt="" src="img/${img}">
</body>
</html>
5.在src下测量
1 设置编码格式
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
.2.getPart是上传文件
3.multipart/form-data :form-data; name="file"; filename="项目需求文档.doc"
4.Part part=request.getPart("file");
System.out.println(part);打印类存的地址:org.apache.catalina.core.ApplicationPart@30213f9a
5.String head=part.getHeader("content-disposition");
System.out.println(head);打印是 :form-data; name="file"; filename="Penguins.jpg"
6 int begin=head.lastIndexOf("=\"");
String name=head.substring(begin+2, head.length()-1);
System.out.println(name);;打印是 Penguins.jpg
7
package com.orcla.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
/**
* Servlet implementation class UserServlet
*/
@WebServlet("/UserServlet")
@MultipartConfig //声明支持文件上传
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UserServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 设置编码格式
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//从request对象中获取前台传过来的文件对象。
Part part=request.getPart("file");
//从part对象中获取文件的头信息:form-data; name="file"; filename="项目需求文档.doc"
String head=part.getHeader("content-disposition");
System.out.println(head);
/**
* 从获取的头信息字符串中截取文件名称
*/
//获取文件名的开始位置
int begin=head.lastIndexOf("=\"");
//截取文件名
String name=head.substring(begin+2, head.length()-1);
//获取存放文件的根路径
String parent=request.getServletContext().getRealPath("/img/");
//将上传的图片名称保存在session中
HttpSession session=request.getSession();
session.setAttribute("img", name);
//将part对象中的文件上传到指定位置
part.write(parent+name);
//重定向至显示图片的页面
response.sendRedirect("index.jsp");
//转发
//request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
7 img是文件
8.@MultipartConfig //声明支持文件上传,没有@MultipartConfig 这个会报错,错误是 Unable to process parts as no multi-part configuration has been provided