【JavaEE企业应用实战学习记录】servlet3.0上传文件

 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: Administrator
 4   Date: 2016/10/6
 5   Time: 14:20
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <html>
10 <head>
11     <title>upload</title>
12 </head>
13 <body>
14 <form method="post" action="/upload" enctype="multipart/form-data">
15     文件名:<input type="text" id="name" name="name"><br/>
16     选择文件:<input type="file" id="file" name="file"><br/>
17     <input type="submit" value="上传">
18 </form>
19 </body>
20 </html>
 1 package sanglp.servlet;
 2
 3 import javax.servlet.ServletException;
 4 import javax.servlet.annotation.MultipartConfig;
 5 import javax.servlet.annotation.WebServlet;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 import javax.servlet.http.Part;
10 import java.io.IOException;
11 import java.io.PrintWriter;
12 import java.util.Collection;
13
14 /**
15  * Created by Administrator on 2016/10/6.
16  */
17 @WebServlet(name = "upload",urlPatterns = {"/upload"})
18 @MultipartConfig
19 public class UploadServlet extends HttpServlet {
20     @Override
21     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
22         //super.service(req, resp);
23         resp.setContentType("text/html;charset=GBK");
24         PrintWriter out=resp.getWriter();
25         //获取普通请求参数
26         String fileName=req.getParameter("name");
27         //获取上传文件的上传域
28         Part part=req.getPart("file");
29         out.println("上传文件的类型为"+part.getContentType()+"<br/>");
30         out.println("上传文件的大小为"+part.getSize()+"<br/>");
31         //获取文件上传的Header Name
32         Collection<String> headerNames=part.getHeaderNames();
33         for(String headerName:headerNames){
34             out.println(headerName+"-->"+part.getHeader(headerName)+"<br/>");
35         }
36         //将上传的文件写入服务器
37         part.write(getServletContext().getRealPath("/uploadFiles")+"/"+fileName);
38     }
39 }

时间: 2024-10-11 04:49:58

【JavaEE企业应用实战学习记录】servlet3.0上传文件的相关文章

【JavaEE企业应用实战学习记录】optiontransferselect实现两个列表选择框

1 <%@ page contentType="text/html; charset=GBK" language="java"%> 2 <%@taglib prefix="s" uri="/struts-tags"%> 3 <html> 4 <head> 5 <meta http-equiv="Content-Type" content="te

【JavaEE企业应用实战学习记录】struts配置文件详细解析

1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <!DOCTYPE struts PUBLIC 4 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 5 "http://struts.apache.org/dtds/struts-2.3.dtd"> 6 7 <!--strut

【JavaEE企业应用实战学习记录】Filter

1 package sanglp.servlet; 2 3 import javax.servlet.*; 4 import javax.servlet.annotation.WebFilter; 5 import javax.servlet.http.HttpServletRequest; 6 import java.io.IOException; 7 8 /** 9 * Created by Administrator on 2016/10/5. 10 */ 11 @WebFilter(fi

【JavaEE企业应用实战学习记录】struts国际化

1 <%-- 2 Created by IntelliJ IDEA. 3 User: Administrator 4 Date: 2016/10/6 5 Time: 16:26 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %

【JavaEE企业应用实战学习记录】struts2登录

1 <%-- login.jsp 2 Created by IntelliJ IDEA. 3 User: Administrator 4 Date: 2016/10/6 5 Time: 16:26 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="ja

【JavaEE企业应用实战学习记录】requestListener

1 package sanglp.servlet; 2 3 import javax.servlet.*; 4 import javax.servlet.annotation.WebListener; 5 import javax.servlet.http.HttpServletRequest; 6 7 /** 8 * Created by Administrator on 2016/10/5. 9 */ 10 @WebListener 11 public class RequestListen

【JavaEE企业应用实战学习记录】getConnListener

Listener:当Web应用在Web容器中运行时,Web应用内部会不断地发生各种事件,如Web应用被启动.Web应用被停止,用户Session开始,用户session结束.用户请求到达等,这些对Web事件对开发者是透明的. ServletAPI提供了大量的监听器来监听Web应用的内部事件,从而允许当Web内部事件发生时回调事件监听器内的方法. 使用步骤:定义Listener实现类 通过Annotation或在web.xml文件中配置Listener. 常用Listener类: ServletC

【JavaEE企业应用实战学习记录】authorityFilter

1 package sanglp.servlet; 2 3 import javax.servlet.*; 4 import javax.servlet.annotation.WebFilter; 5 import javax.servlet.annotation.WebInitParam; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpSession; 8 import java.

【JavaEE企业应用实战学习记录】MyGetAttributeListener

1 package sanglp.servlet; 2 3 import javax.servlet.ServletContext; 4 import javax.servlet.ServletContextAttributeEvent; 5 import javax.servlet.ServletContextAttributeListener; 6 import javax.servlet.annotation.WebListener; 7 8 /** 9 * Created by Admi