从Java库更改提示错误消息

时间:2015-10-23 02:53:16

标签: java file-upload zk apache-commons-fileupload

目前我正在使用zkoss,java和maven。我的应用程序中的一个功能是上传文件。

当我通过上传一个非常大的文件对此功能进行一些测试时,我收到了一条提示错误消息,其中说 “请求被拒绝,因为其大小(8356577)超出了配置的最大值(1024000)”

在我做了几次调试之后,我发现提示消息来自org.apache.commons.fileupload.FileUploadBase,它来自我的一个maven依赖项。

现在,我想将错误消息更改为其他内容。有没有解决方法怎么做?我更喜欢不重新编译库的方式。非常感谢。

- 更新 -

这是我的web.xml。我做错什么了吗?再次感谢。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  id="WebApp_ID" version="2.5">
  <display-name>Sistem Informasi Jabatan Fungsional</display-name>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      classpath:spring-ctx.xml
    </param-value>
  </context-param>

  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
  </context-param>


  <session-config>
    <session-timeout>15</session-timeout> <!-- in minutes -->
  </session-config>


  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>


  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- For using session scoped bean -->
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>

  <listener>
    <listener-class>org.zkoss.zk.ui.http.HttpSessionListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>uploader</servlet-name>
    <servlet-class>org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class>
    <init-param>
      <param-name>extention0</param-name>
      <param-value>/upload=uploader.UploadUtil</param-value>
    </init-param>
  </servlet>

  <servlet>
    <servlet-name>zkLoader</servlet-name>
    <servlet-class>org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class>
    <!-- Must. Specifies URI of the update engine (DHtmlUpdateServlet). It must be the same as <url-pattern> 
      for the update engine. -->
    <init-param>
      <param-name>update-uri</param-name>
      <param-value>/zkau</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup><!-- MUST -->
  </servlet>
  <servlet>
    <servlet-name>auEngine</servlet-name>
    <servlet-class>org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>zkLoader</servlet-name>
    <url-pattern>*.zul</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>zkLoader</servlet-name>
    <url-pattern>*.zhtml</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>auEngine</servlet-name>
    <url-pattern>/zkau/*</url-pattern>
  </servlet-mapping>
  <!-- Spring Security Filter Start Here -->
  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- Spring Security Filter End Here -->

  <welcome-file-list>
    <welcome-file>index.zul</welcome-file>
  </welcome-file-list>
</web-app>

1 个答案:

答案 0 :(得分:0)

您可以从org.zkoss.zk.au.http.AuUploader延伸并覆盖handleError(Throwable)方法:

public class MyUploader extends AuUploader {
    @Override
    protected String handleError(Throwable ex)
    {
        return "mymessage";
    }        
}

然后你必须在你的web.xml中配置这个扩展:

配置

<servlet>
    <servlet-class>org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class>
    <!-- extentions here -->
</servlet>

添加:

<init-param>            
    <param-name>processor0</param-name>             
    <param-value>/upload=uploader.MyUploader</param-value>
</init-param>

可能processor0必须更改为extention0,具体取决于您的ZK版本。 processor0被证实与ZK 7合作。

可以找到有关此问题的ZK文档here