本篇来源于:http://piggysnow.sinaapp.com/archives/29 我做下补充,记录一下
- package com.utils;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.catalina.Globals;
- import org.apache.catalina.servlets.DefaultServlet;
- /**
- * 此类是为了解决 下载tomcat中的中文文件时连接错误的问题
- * 解决办法1(也是此类):此类来源于网络:http://piggysnow.sinaapp.com/archives/29,需要注意的是在部署此项目的时候,需要把项目中的catalina.jar删除,否则会包冲突错误[tomcat启动正常,而页面却总是无内容显示的错误]
- * 解决办法2:在tomcat中server.xml 大概70行的位置添加:URIEncoding="utf-8"
- * @date:下午1:29:07 2012-12-28
- */
- public class DownloadCharacterEncodingServlet extends DefaultServlet {
- /**
- *
- */
- private static final long serialVersionUID = 2551697592701704614L;
- /**
- * Return the relative path associated with this servlet.
- *
- * @param request The servlet request we are processing
- */
- protected String getRelativePath(HttpServletRequest request) {
- // IMPORTANT: DefaultServlet can be mapped to ‘/’ or ‘/path/*’ but always
- // serves resources from the web app root with context rooted paths.
- // i.e. it can not be used to mount the web app root under a sub-path
- // This method must construct a complete context rooted path, although
- // subclasses can change this behaviour.
- // Are we being processed by a RequestDispatcher.include()?
- if (request.getAttribute(Globals.INCLUDE_REQUEST_URI_ATTR) != null) {
- String result = (String) request.getAttribute(
- Globals.INCLUDE_PATH_INFO_ATTR);
- if (result == null) {
- result = (String) request.getAttribute(
- Globals.INCLUDE_SERVLET_PATH_ATTR);
- } else {
- result = (String) request.getAttribute(
- Globals.INCLUDE_SERVLET_PATH_ATTR) + result;
- }
- if ((result == null) || (result.equals(""))) {
- result = "/";
- }
- return (result);
- }
- String result = request.getPathInfo();
- if (result == null) {
- result = request.getServletPath();
- } else {
- result = request.getServletPath() + result;
- }
- if ((result == null) || (result.equals(""))) {
- result = "/";
- }
- try{
- result = new String(result.getBytes("ISO-8859-1"), "UTF-8");
- }
- catch (Exception e) {
- }
- return (result);
- }
- @Override
- protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- super.doGet(req, resp);
- }
- @Override
- protected void doPost(HttpServletRequest request,
- HttpServletResponse response) throws IOException, ServletException {
- super.doGet(request, response);
- }
- @Override
- protected void doPut(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- // TODO Auto-generated method stub
- super.doGet(req, resp);
- }
- }
Web.xml配置一下servlet:
- <servlet>
- <servlet-name>chnURIServlet</servlet-name>
- <servlet-class>com.utils.DownloadCharacterEncodingServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>chnURIServlet</servlet-name>
- <url-pattern>/xls/*</url-pattern>
- </servlet-mapping>
需要注意的的是:要把那个jar包删除掉:不然会报如下错误:
- 2012-12-28 13:40:30 org.apache.catalina.core.ApplicationContext log
- 严重: StandardWrapper.Throwable
- java.lang.ClassCastException: org.apache.naming.resources.ProxyDirContext cannot be cast to org.apache.naming.resources.ProxyDirContext
- at org.apache.catalina.servlets.DefaultServlet.init(DefaultServlet.java:305)
- at javax.servlet.GenericServlet.init(GenericServlet.java:212)
- at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1206)
- at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:827)
- at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
- at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
- at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
- at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
- at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
- at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
- at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:877)
- at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:594)
- at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1675)
- at java.lang.Thread.run(Thread.java:619)
- 2012-12-28 13:40:30 org.apache.catalina.core.StandardWrapperValve invoke
- 严重: Allocate exception for servlet chnURIServlet
- java.lang.ClassCastException: org.apache.naming.resources.ProxyDirContext cannot be cast to org.apache.naming.resources.ProxyDirContext
- at org.apache.catalina.servlets.DefaultServlet.init(DefaultServlet.java:305)
- at javax.servlet.GenericServlet.init(GenericServlet.java:212)
- at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1206)
- at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:827)
- at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
- at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
- at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
- at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
- at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
- at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
- at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:877)
- at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:594)
- at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1675)
- at java.lang.Thread.run(Thread.java:619)
转载于:https://blog.51cto.com/programmer/1102909