- 使用Servlet3.0实现文件上传,不需要借助第三方的jar包。
1、在index.jsp文件中编写文件上传的表单:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Insert title here</title> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"> </head> <body> <form action="${pageContext.request.contextPath }/uploadServlet" method="POST" enctype="multipart/form-data"> 文件:<input type="file" name="photo" /> <br> <input type="submit" value="上传" /> </form> </body> </html>
2、需要在对应的自定义Servlet类中添加相应代码:
package com.geeklicreed.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.Part; @WebServlet("/uploadServlet") @MultipartConfig // 表明当前Servlet可以处理Multipart请求 public class UploadServlet extends HttpServlet { private static final long serialVersionUID = 1L; public UploadServlet() { super(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取服务器保存上传文件的目录路径 String path = this.getServletContext().getRealPath("images"); System.out.println(path); // 从请求中获取Multipart请求中的上传文件“部分” Part part = request.getPart("photo"); //获取指定的头部属性 String header = part.getHeader("Content-Disposition"); System.out.println(header); int index = header.lastIndexOf("="); String fileName = header.substring(index + 2, header.length() - 1); System.out.println(fileName); fileName = fileName.substring(fileName.lastIndexOf("\\") + 1); System.out.println(fileName); // 完成文件上传 part.write(path + "/" + fileName); /* 我电脑中的输出结果为: C:\Users\Administrator\Workspaces\MyEclipse 2016 CI\.metadata\.me_tcat7\webapps\fileUpload\images form-data; name="photo"; filename="C:\Users\Administrator\Desktop\魔术格言.txt" C:\Users\Administrator\Desktop\魔术格言.txt 魔术格言.txt */ } }
- 代码解析:
- javax.servlet.http.HttpServletRequest接口中的getPart(java.lang.String name)方法用于获取给定名称的Part对象:(即用<input type="file" name="photo"/>表单组件中的name属性来获取Part对象)
Part | getPart(java.lang.String name) Gets the Part with the given name. |
- javax.servlet.http.Part接口:
- 这个接口代表用于接收multipart/form-data POST请求的部分或者单项。(只用于Serlvet3.0)
This class represents a part or form item that was received within a multipart/form-data POST request. Since: Servlet 3.0
- 该接口中主要用到的方法为:
- getHeader(java.lang.String name)方法:获取指定名称的请求头部信息,作为字符串返回。
java.lang.String | getHeader(java.lang.String name) Returns the value of the specified request header as a String . |
- write(java.lang.String fileName)方法:一个便捷的方法用于将上传的文件写入硬盘中。
void | write(java.lang.String fileName) A convenience method to write this uploaded item to disk. |
3、在myEclipse自带的浏览器中访问index.jsp页面。可以看到,在运行的tomcat对应的硬盘目录中已经上传:
- 附加说明:
- 在myEclipse中,默认会把Web项目放到Eclipse的工作空间下的.metadata\.me_tcat7\webapps。(在myEclipse中指定服务器为tomcat7)
本文出自 “12392717” 博客,请务必保留此出处http://12402717.blog.51cto.com/12392717/1979155
Servlet编程专题11之Servlet3.0特性实现文件上传
原文地址:http://12402717.blog.51cto.com/12392717/1979155