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

downloadFile to response

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.