Changeset 449


Ignore:
Timestamp:
04/02/12 18:22:23 (12 years ago)
Author:
vmipsl
Message:

downloadFile to response

Location:
tapas/web/src/com/ether
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • tapas/web/src/com/ether/ControllerEther.java

    r446 r449  
    2323import javax.servlet.http.HttpServletRequest; 
    2424import javax.servlet.http.HttpServletResponse; 
    25 import java.io.BufferedInputStream; 
    26 import java.io.File; 
    27 import java.io.FileInputStream; 
    2825import java.io.IOException; 
    29 import java.io.InputStream; 
    30 import java.io.OutputStream; 
    3126import java.io.UnsupportedEncodingException; 
    3227import java.lang.annotation.Annotation; 
     
    186181                final String fileName = methodDescription.getDownloadFile(); 
    187182                final String downloadPath = getProperty( "downloadPath" ); 
    188  
    189                 response.setContentType( "multipart/zip" ); 
    190                 response.setHeader( "Content-Disposition", "attachment; filename=\"" + fileName.trim() + "\";" ); 
    191  
    192                 final File file = new File( downloadPath + fileName ); 
    193                 if( !file.exists() ) 
    194                     throw new WebException( WebException.WebCode.ERROR_TO_DOWNLOAD_FILE, file.getPath() ); 
    195  
    196                 response.setContentLength( (int) file.length() ); 
    197                 try 
    198                 { 
    199                     final OutputStream outputStream = response.getOutputStream(); 
    200                     final FileInputStream fileInputStream = new FileInputStream( file ); 
    201  
    202                     final BufferedInputStream bufferedInputStream = new BufferedInputStream( fileInputStream ); 
    203                     final InputStream inputStream = new BufferedInputStream( bufferedInputStream ); 
    204  
    205                     int count; 
    206                     final byte[] buf = new byte[4096]; 
    207                     while( ( count = inputStream.read( buf ) ) > -1 ) 
    208                         outputStream.write( buf, 0, count ); 
    209                     inputStream.close(); 
    210                     outputStream.close(); 
    211                 } 
    212                 catch( Exception ex ) 
    213                 { 
    214                     throw new WebException( WebException.WebCode.ERROR_TO_DOWNLOAD_FILE, ex ); 
    215                 } 
     183                WebHelper.downloadFileToResponse( fileName, downloadPath, response ); 
    216184                return null; 
    217185            } 
  • tapas/web/src/com/ether/WebHelper.java

    r446 r449  
    1212import javax.servlet.http.HttpServletRequest; 
    1313import javax.servlet.http.HttpServletResponse; 
     14import java.io.BufferedInputStream; 
     15import java.io.File; 
    1416import java.io.FileInputStream; 
    1517import java.io.IOException; 
     18import java.io.InputStream; 
     19import java.io.OutputStream; 
    1620import java.io.PrintWriter; 
    1721import java.security.InvalidParameterException; 
     
    130134    } 
    131135 
     136    /** 
     137     * This method send to the response (to download) the file named fileName localized in the path downloadPath 
     138     * 
     139     * @param fileName 
     140     * @param downloadPath 
     141     * @param response 
     142     * @throws WebException 
     143     */ 
     144    public static void downloadFileToResponse( @NotNull final String fileName, @NotNull final String downloadPath, @NotNull final HttpServletResponse response ) 
     145            throws WebException 
     146    { 
     147        response.setContentType( "multipart/zip" ); 
     148        response.setHeader( "Content-Disposition", "attachment; filename=\"" + fileName.trim() + "\";" ); 
     149 
     150        final File file = new File( downloadPath + fileName ); 
     151        if( !file.exists() ) 
     152            throw new WebException( WebException.WebCode.ERROR_TO_DOWNLOAD_FILE, file.getPath() ); 
     153 
     154        response.setContentLength( (int) file.length() ); 
     155        try 
     156        { 
     157            final OutputStream outputStream = response.getOutputStream(); 
     158            final FileInputStream fileInputStream = new FileInputStream( file ); 
     159 
     160            final BufferedInputStream bufferedInputStream = new BufferedInputStream( fileInputStream ); 
     161            final InputStream inputStream = new BufferedInputStream( bufferedInputStream ); 
     162 
     163            int count; 
     164            final byte[] buf = new byte[4096]; 
     165            while( ( count = inputStream.read( buf ) ) > -1 ) 
     166                outputStream.write( buf, 0, count ); 
     167            inputStream.close(); 
     168            outputStream.close(); 
     169        } 
     170        catch( Exception ex ) 
     171        { 
     172            throw new WebException( WebException.WebCode.ERROR_TO_DOWNLOAD_FILE, ex ); 
     173        } 
     174    } 
     175 
    132176    public static final int STATUS_CODE_SERVICE_EXCEPTION = 500; 
    133177    public static final String PROPERTIES_FILE = "tapas.properties"; 
Note: See TracChangeset for help on using the changeset viewer.