Changeset 449
- Timestamp:
- 04/02/12 18:22:23 (12 years ago)
- Location:
- tapas/web/src/com/ether
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
tapas/web/src/com/ether/ControllerEther.java
r446 r449 23 23 import javax.servlet.http.HttpServletRequest; 24 24 import javax.servlet.http.HttpServletResponse; 25 import java.io.BufferedInputStream;26 import java.io.File;27 import java.io.FileInputStream;28 25 import java.io.IOException; 29 import java.io.InputStream;30 import java.io.OutputStream;31 26 import java.io.UnsupportedEncodingException; 32 27 import java.lang.annotation.Annotation; … … 186 181 final String fileName = methodDescription.getDownloadFile(); 187 182 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 ); 216 184 return null; 217 185 } -
tapas/web/src/com/ether/WebHelper.java
r446 r449 12 12 import javax.servlet.http.HttpServletRequest; 13 13 import javax.servlet.http.HttpServletResponse; 14 import java.io.BufferedInputStream; 15 import java.io.File; 14 16 import java.io.FileInputStream; 15 17 import java.io.IOException; 18 import java.io.InputStream; 19 import java.io.OutputStream; 16 20 import java.io.PrintWriter; 17 21 import java.security.InvalidParameterException; … … 130 134 } 131 135 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 132 176 public static final int STATUS_CODE_SERVICE_EXCEPTION = 500; 133 177 public static final String PROPERTIES_FILE = "tapas.properties";
Note: See TracChangeset
for help on using the changeset viewer.