首页 > 学院 > 开发设计 > 正文

Servlet_03

2019-11-09 18:27:34
字体:
来源:转载
供稿:网友
过滤器步骤:/** * 过滤器 * 1、创建过滤器,实现javax.servlet.Filter接口 * 2、在过滤器的重写方法doFilter中写我们过滤操作:编码、登录拦截 * 3、配置web.xml中的过滤器 * <filter> <filter-name>myFilter</filter-name> <filter-class>com.blm.filter.MyFilter</filter-class> </filter> <filter-mapping> <filter-name>myFilter</filter-name> <!-- 表示所有访问guo下面的都要经过 过滤器 --> <!-- http://localhost/filterdemo/guo/FirstServlet要经过过滤器 --> <!-- http://localhost/filterdemo/SecondServlet 不经过过滤器 --> <url-pattern>/guo/*</url-pattern> </filter-mapping> * @author Administrator * */web.xml配置: <!-- 设置过滤器 --> <filter> <filter-name>myFilter</filter-name> <filter-class>com.blm.filter.MyFilter</filter-class> </filter> <filter-mapping> <filter-name>myFilter</filter-name> <!-- 表示所有访问guo下面的都要经过 过滤器 --> <!-- http://localhost/filterdemo/guo/FirstServlet要经过过滤器 --> <!-- http://localhost/filterdemo/SecondServlet 不经过过滤器 --> <url-pattern>/guo/*</url-pattern> </filter-mapping>java代码:package com.blm.filter;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class MyFilter implements Filter { static int number = 1; @Override public void destroy() { } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { System.out.PRintln("我在这等着你 doFilter"); // 每次指定路径的请求都会经过该过滤器 // 设置编码:方式一 // HttpServletRequest request = (HttpServletRequest)arg0; // HttpServletResponse resp = (HttpServletResponse)arg1; // request.setCharacterEncoding("UTF-8"); // resp.setContentType("text/html;charset=UTF-8"); // 设置编码:方式二 arg0.setCharacterEncoding("UTF-8"); arg1.setContentType("text/html;charset=UTF-8"); // 登录拦截:如果登录,就放行,如果没有登录,就返回页面 // 正常的登录信息,是从session中获取 number = number+1; if (number> 5) { // 登录成功 // 放行 arg2.doFilter(arg0, arg1); } else { //登录失败,返回登录页面 HttpServletResponse resp = (HttpServletResponse)arg1; resp.sendRedirect("/filterdemo/login.html"); } } @Override public void init(FilterConfig arg0) throws ServletException { }}上传文件html代码:<!-- 上传文件时,必须要有:enctype="multipart/form-data" method="post" --> <form action="/uppoadfiles/UploadFileServlet" enctype="multipart/form-data" method="post"> 上传头像:<input type="file" name="headImg"> <input type="submit" value="提交"> <img alt="" src=""> </form>java代码:servlet代码package com.blm.controller;import java.io.File;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.MultipartConfig;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.Part;import com.blm.utils.StringUtils;/** * 上传文件 * 1、表单中 form的属性必须要有: * enctype="multipart/form-data" method="post" * 2、input的类型必须是file * * 3、servlet类建议加上注解:@MultipartConfig * 4、req.getPart("headImg")中的headImg这个是input的name属性的值 * 5、StringUtils.uploadFile这个方法中指定了上传的路径: * String fileSavingFolder = "d://test"; * @author 沄莲 * */@MultipartConfigpublic class UploadFileServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //说明输入的请求信息采用UTF-8编码方式 req.setCharacterEncoding("utf-8"); resp.setContentType("text/html; charset=UTF-8"); PrintWriter out = resp.getWriter(); //Servlet3.0中新引入的方法,用来处理multipart/form-data类型编码的表单 Part part = req.getPart("headImg"); //name属性 //调用上传文件的工具方法 StringUtils.uploadFile(part); //输出上传成功信息 out.println("文件上传成功~!"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); }}工具类代码:/** * 上传文件 * @param part */ public static void uploadFile(Part part){ //获取HTTP头信息headerInfo=(form-data; name="file" filename="文件名") String headerInfo = part.getHeader("content-disposition"); //从HTTP头信息中获取文件名fileName=(文件名) String fileName = headerInfo.substring(headerInfo.lastIndexOf("=") + 2, headerInfo.length() - 1); //获得存储上传文件的文件夹路径 // String fileSavingFolder = this.getServletContext().getRealPath("/uploadImg"); String fileSavingFolder = "d://test"; //获得存储上传文件的完整路径(文件夹路径+文件名) //文件夹位置固定,文件夹采用与上传文件的原始名字相同 String fileSavingPath = fileSavingFolder + File.separator + fileName; //如果存储上传文件的文件夹不存在,则创建文件夹 File f = new File(fileSavingFolder + File.separator); if(!f.exists()){ f.mkdirs(); } System.out.println("~~~~~~~~~~~~~~~~~~~~~~"+fileSavingPath); //将上传的文件内容写入服务器文件中 System.out.println("文件上传路径:"+fileSavingPath); try { part.write(fileSavingPath); } catch (IOException e) { e.printStackTrace(); } }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表