<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>文件上传</title> </head> <body> <form action="FileUploadServlet" method="post" enctype="multipart/form-data"> <input type="file" size="30" /><br> <pre> <input type="submit" value="上传"> <input type="reset" value="重置"> </pre> </form> </body> </html>
3.配置web.xml文件中的的servlet和过滤器,过滤器解决上传文件名为中文时出现乱码。
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee "> <servlet> <display-name>FileUploadServlet</display-name> <servlet-name>FileUploadServlet</servlet-name> <servlet-class>FileUploadServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FileUploadServlet</servlet-name> <url-pattern>/FileUploadServlet</url-pattern> </servlet-mapping> <filter> <filter-name>setCharacterEncoding </filter-name> <filter-class>com.xulu.EncodingChange</filter-class> <init-param> <param-name>ignore </param-name> <param-value>true </param-value> </init-param> <init-param> <param-name>encoding </param-name> <param-value>UTF-8 </param-value> </init-param> </filter> <filter-mapping> <filter-name>setCharacterEncoding </filter-name> <url-pattern>/* </url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <login-config> <auth-method>BASIC</auth-method> </login-config> </web-app>
4.过滤器文件EncodingChange.java和setCharacterEncoding.java分别如下,并且在根目录下的WEB-INF\classes\com\xulu文件夹中放入他它们编译生成的.class文件
package com.xulu; import java.io.IOException; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class EncodingChange implements Filter { protected String encoding = null; protected FilterConfig filterconfig = null; protected boolean ignore = true; public void destroy() { this.encoding = null; this.filterconfig = null; } public void doFilter(ServletRequest requests, ServletResponse responses, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletRequest request = (HttpServletRequest) requests; HttpServletResponse response = (HttpServletResponse) responses; if (ignore || request.getCharacterEncoding() == null) { String encoding = selectEncoding(request); if (encoding != null) { request.setCharacterEncoding(encoding); request.getSession().getAttribute("Login_Student"); request.getSession().getAttribute("Login_Teacher"); request.getSession().getAttribute("Login_Admin"); } } chain.doFilter(request, response); } public void init(FilterConfig filterconfig) throws ServletException { // TODO Auto-generated method stub this.filterconfig = filterconfig; this.encoding = filterconfig.getInitParameter("encoding"); String value = filterconfig.getInitParameter("ignore"); if (value == null) { this.ignore = true; } else if (value.equalsIgnoreCase("true")) { this.ignore = true; } else if (value.equalsIgnoreCase("yes")) { this.ignore = true; } else { this.ignore = false; } } public String selectEncoding(ServletRequest request) { return this.encoding; } } 和 package com.xulu; import java.io.IOException; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class setCharacterEncoding implements Filter{ protected String encoding=null; protected FilterConfig filterconfig=null; protected boolean ignore=true; public void destroy() { this.encoding=null; this.filterconfig=null; } public void doFilter(ServletRequest requests, ServletResponse responses, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletRequest request=(HttpServletRequest)requests; HttpServletResponse response=(HttpServletResponse)responses; if(ignore||request.getCharacterEncoding()==null){ String encoding=selectEncoding(request); if(encoding!=null){ request.setCharacterEncoding(encoding); request.getSession().getAttribute("Login_Student"); request.getSession().getAttribute("Login_Teacher"); request.getSession().getAttribute("Login_Admin"); } } chain.doFilter(request, response); } public void init(FilterConfig filterconfig) throws ServletException { // TODO Auto-generated method stub this.filterconfig = filterconfig; this.encoding = filterconfig.getInitParameter("encoding"); String value = filterconfig.getInitParameter("ignore"); if (value == null) { this.ignore = true; } else if (value.equalsIgnoreCase("true")) { this.ignore = true; } else if (value.equalsIgnoreCase("yes")) { this.ignore = true; } else { this.ignore = false; } } public String selectEncoding(ServletRequest request){ return this.encoding; } }