Servlet3.0新特性之批量上传
2016-10-19 11:20
405 查看
package com.xxx.action; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Collection; 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.Part; /** * 批量上传文件 * @author Administrator * */ @WebServlet(name="uploadPart",urlPatterns="/uploadPart") @MultipartConfig public class UploadPartServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("GBK"); resp.setCharacterEncoding("GBK"); resp.setContentType("text/html;charset=GBK"); //上传文件存储位置 String path=this.getServletContext().getRealPath("/WEB-INF/upload"); Collection<Part> parts=req.getParts(); for(Part part:parts){ //只处理上传文件区段 if (part.getName().startsWith("file")) { String header=part.getHeader("content-disposition"); //System.out.println(header); //form-data; name="file2"; filename="11.jpg" //String[] strArr=header.split(";"); //System.out.println(strArr.length); //String[] tempName=strArr[2].split("="); //String fileNmae=tempName[1].replaceAll("\"", ""); //System.out.println(fileNmae); String fileName=getFileName(part); part.write(path+File.separator+fileName); } } } private String getFileName(Part part) { String header = part.getHeader("Content-Disposition"); String fileName = header.substring(header.indexOf("filename=\"") + 10, header.lastIndexOf("\"")); header.lastIndexOf("\""); return fileName; } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% 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>My JSP 'upload.jsp' starting page</title> </head> <body> <form action="${pageContext.request.contextPath}/uploadPart" method="post" enctype="multipart/form-data"> <input type="file" name="file1"><br> <input type="file" name="file2"><br> <input type="submit"> </form> </body> </html>
相关文章推荐
- [Servlet3.0新特性]Serlvet文件上传
- Servlet3.0 新特性——HttpServletRequest 对文件上传的支持
- [Servlet3.0新特性]Serlvet文件上传
- Servlet3.0新特性之文件上传
- Servlet 3.0新特性——文件上传接口
- servlet3.0新特性测试,文件上传(1)
- Servlet 3.0新特性——文件上传接口
- servlet3.0新特性测试,文件上传(1)
- 【Servlet3.0新特性】第03节_文件上传
- Servlet 3.0 新特性详解(五)ServletContext 的性能增强及HttpServletRequest 对文件上传的支持
- 【Servlet3.0新特性】第03节_文件上传
- 【Servlet 3.0】Servelet 3.0 新特性 文件上传 path对象
- Servlet 3.0 新特性概述
- servlet3.0 新特性之Annotation
- 在Spring MVC框架下利用Servlet3.0 API实现文件上传
- Servlet3.0上传图片示例
- Servlet 3.0笔记之超方便的文件上传支持
- [转]Servlet 3.0 新特性详解
- Servlet3.0实现文件上传的方法
- Servlet 3.0 新特性详解