Servlet-文件上传@MultipartConfig,Part
2015年9月18日
参考:http://blog.csdn.net/xiazdong/article/details/7208316
设置请求方式:method=”post”
设置编码类型:enctype=”multipart/form-data”
指定servlet:action=” /testAMap/ts"。
示例:
<formmethod="post" enctype="multipart/form-data"action="/testAMap/ts">
<input type="file" name="file"/>
<button type="submit"> submit</button>
</form>
创建处理文件Servlet:使用@MultipartConfig标记。
处理part内容:读取文件名getSubmittedFileName(),文件大小getSize(),保存write()。
Part part=request.getPart("file");
pw.println("name="+part.getHeader("content-dispostion"));
byte[] buff=new byte[(int) part.getSize()];
part.getInputStream().read(buff);
//后台文件处理Servlet:UploadFile.java
package lee;
import java.io.IOException;
importjavax.servlet.ServletException;
importjavax.servlet.annotation.MultipartConfig;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.Part;
/**
* Servlet implementation class UploadFile
*/
@WebServlet("/UploadFile")
@MultipartConfig
public class UploadFileextends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UploadFile() {
super();
// TODO Auto-generated constructor stub
}
/**
* @seeHttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at:").append(request.getContextPath());
Part p=request.getPart("clientf");
StringfileName=p.getSubmittedFileName();
System.out.println("name="+fileName);
System.out.println("size="+p.getSize());
StringfilePath=getServletContext().getRealPath("/uploadfiles");
System.out.println(filePath);
p.write(filePath+"/"+fileName);
}
/**
* @seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
//前台文件上传页面:a.jsp
<%@ pagelanguage="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"session="false"%>
<!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Insert titlehere</title>
</head>
<body>
this is a.jsp
<%="aync return="+new java.util.Date() %>
<%=request.getAttribute("async") %>
<formmethod="post" enctype="multipart/form-data"action="UploadFile">
<inputtype="file" name="clientf"/>
<buttontype="submit"> submit</button>
</form>
</body>
</html>
//结果
//upload.htm
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
Hello World!
<form method="post"enctype="multipart/form-data" action="/testAMap/ts">
<inputtype="file" name="file"/>
<buttontype="submit"> submit</button>
</form>
</body>
</html>
//web.xml
<?xmlversion="1.0" encoding="UTF-8"?>
<web-appversion="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="false">
<description>
Servlet and JSP Examples.
</description>
<display-name>Servlet and JSPExamples</display-name>
</web-app>
//TestServlet.java
package com.thbd;
import java.io.IOException;
import java.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.annotation.MultipartConfig;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.Part;
/**
* Servlet implementation class TestServlet
*/
@WebServlet(urlPatterns="/ts")
@MultipartConfig
public class TestServletextends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public TestServlet() {
// TODO Auto-generated constructor stub
}
/**
* @seeHttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
pw.println("<h1>Hello,I amServlet.</h1>");
Partpart=request.getPart("file");
pw.println("name="+part.getHeader("content-dispostion"));
byte[] buff=newbyte[(int) part.getSize()];
part.getInputStream().read(buff);
pw.println(part.getSize()+new String(buff));
}
/**
* @seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request,response);
}
}