Search code examples
javafile-uploadzkapache-commons-fileupload

Change Prompt Error Message From Java Library


currently I am working with zkoss, java, and maven. One of functionalities in my applications is uploading file.

When I did some testing regarding this functionality by uploading a very large file, I got a prompt error message, which said "the request was rejected because its size (8356577) exceeds the configured maximum (1024000)"

After I did several debugging, I found out that the prompt message is came from org.apache.commons.fileupload.FileUploadBase, which is file from one of my maven dependencies.

Now, I would like to change the error message into something else. Is there any solution about how to do it? I prefer way without recompiling the library. Thanks a lot.

-- UPDATED --

Here is my web.xml. Did I do something wrong? Thanks again.

<?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>

Solution

  • You can extend from org.zkoss.zk.au.http.AuUploader and override the handleError(Throwable) method:

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

    Then you have to configure this extention in your web.xml:

    In configuration of

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

    add:

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

    May be processor0 has to be changed to extention0, depending on your ZK version. processor0 was testesd working with ZK 7.

    The ZK documentation on this matter can be found here.