From: William K. <kr...@3f...> - 2014-06-23 21:49:08
|
In my web application, I have code that generates a temporary file (based on user input) and streams it back to the user using a StreamingResolution. The initial implementation had a utility method that generated the file and passed back an InputStream... public Resolution exportCsv() throws IOException { InputStream is = getCsvData(); StreamingResolution sr = new StreamingResolution(Constants.CONTENT_TYPE_CSV, is); sr.setFilename("export.csv"); return sr; } However, I want to be able to clean up the temporary file after it has been streamed to the user. I thought that I could use the anonymous inner class method described in the stripes javadocs... public Resolution exportCsv() throws IOException { return new StreamingResolution(Constants.CONTENT_TYPE_CSV) { public void stream(HttpServletResponse response) throws Exception { File tempFile = null; InputStream is = null; try { tempFile = File.createTempFile(UUID.randomUUID().toString(), null); getCsvData(tempFile); is = new FileInputStream(tempFile); IOUtils.copy(is, response.getOutputStream()); is.close(); } catch(IOException e) { logger.error("Unable to export to csv file: " + e); throw new IOException(); } finally { IOUtils.closeQuietly(is); FileUtils.deleteQuietly(tempFile); } } }.setFilename("export.csv"); } I'm finding that if there's an exception, whatever error page my application generates gets streamed back to the user in a file called "export.csv". So this method isn't really viable, at least the way I'm using it. Is there a pattern for this sort of thing in Stripes? |