jp.terasoluna.fw.web.struts.actions
Class FileDownloadUtil

java.lang.Object
  extended by jp.terasoluna.fw.web.struts.actions.FileDownloadUtil

public class FileDownloadUtil
extends java.lang.Object

Utility class which downloads the file.

Changing the encoding of download file name
Only Internet Explorer supports by default, encoding of the file name (File name displayed when storing the file by using the browser) specified at the time of download. To change the encoding, create DownloadFileNameEncoder implementation class and do the settings in Bean definition file.
Implementation example of DownloadFileNameEncoder
In this example, User-Agent detects the browser. If the browser is FireFox, encoding is performed by using commons-codec class.

 public class MyEncoder implements DownloadFileNameEncoder {

     public String encode(String original, HttpServletRequest request,
             HttpServletResponse response) {
         String userAgent = request.getHeader("User-Agent");
         if (StringUtils.contains(userAgent, "MSIE")) {
             return encodeForIE(original);
         } else if (StringUtils.contains(userAgent, "Gecko")) {
             return encodeForGecko(original);
         }
         return encodeForIE(original);
     }

     protected String encodeForGecko(String original) {
         try {
             return new BCodec().encode(original);
         } catch (EncoderException e) {
             return original;
         }
     }

     protected String encodeForIE(String original) {
         try {
             return URLEncoder.encode(original,
                     AbstractDownloadObject.DEFAULT_CHARSET);
         } catch (UnsupportedEncodingException e) {
             return original;
         }
     }
 }
 
Configuration example of Bean definition file

 <bean class="jp.terasoluna.fw.web.struts.actions.FileDownloadUtil">
   <property name="encoder" ref="encoder"/>
 </bean>
 <bean name="encoder" class="sample.MyEncoder"/>
 


Field Summary
protected static DownloadFileNameEncoder encoder
          Encoder of specified file name.
static java.lang.String HEADER_CONTENT_DISPOSITION
          CONTENT-DISPOSITION header name of response.
private static org.apache.commons.logging.Log log
          Log class.
static java.lang.String TOO_MANY_DOWNLOAD_ERROR
          Error code indicating that there are more than 1 instances for downloading.
 
Constructor Summary
FileDownloadUtil()
           
 
Method Summary
static void download(AbstractDownloadObject downloadObject, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, boolean forceDownload)
          Download through browser.
static void download(java.lang.Object result, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Download through browser.
 void setEncoder(DownloadFileNameEncoder encoder)
          Sets the encoder of specified file name.
protected static void setFileName(javax.servlet.http.HttpServletResponse response, java.lang.String name, boolean forceDownload)
          Sets file name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static final org.apache.commons.logging.Log log
Log class.


HEADER_CONTENT_DISPOSITION

public static final java.lang.String HEADER_CONTENT_DISPOSITION
CONTENT-DISPOSITION header name of response.

See Also:
Constant Field Values

TOO_MANY_DOWNLOAD_ERROR

public static final java.lang.String TOO_MANY_DOWNLOAD_ERROR
Error code indicating that there are more than 1 instances for downloading.

See Also:
Constant Field Values

encoder

protected static DownloadFileNameEncoder encoder
Encoder of specified file name.

Constructor Detail

FileDownloadUtil

public FileDownloadUtil()
Method Detail

setEncoder

public void setEncoder(DownloadFileNameEncoder encoder)
Sets the encoder of specified file name.

Parameters:
encoder - Encoder of specified file name.

download

public static void download(java.lang.Object result,
                            javax.servlet.http.HttpServletRequest request,
                            javax.servlet.http.HttpServletResponse response)
Download through browser.

Parameters:
result - Instance that retains the download data.
request - Request.
response - Response.

download

public static void download(AbstractDownloadObject downloadObject,
                            javax.servlet.http.HttpServletRequest request,
                            javax.servlet.http.HttpServletResponse response,
                            boolean forceDownload)
                     throws java.io.IOException
Download through browser.

Parameters:
downloadObject - Download target.
request - Request.
response - Response.
Throws:
java.io.IOException - When the I/O exception has occurred at the time of download.

setFileName

protected static void setFileName(javax.servlet.http.HttpServletResponse response,
                                  java.lang.String name,
                                  boolean forceDownload)
Sets file name.

Parameters:
response - Response.
name - Download name.
forceDownload - Check if it is forcible download. true when forcible download.