Revision 8304
Added by Matt Jones over 10 years ago
ResponseUtil.java | ||
---|---|---|
37 | 37 |
import javax.servlet.http.HttpServletRequest; |
38 | 38 |
import javax.servlet.http.HttpServletResponse; |
39 | 39 |
|
40 |
import org.apache.commons.io.IOUtils; |
|
40 | 41 |
import org.apache.log4j.Logger; |
41 | 42 |
|
42 | 43 |
import edu.ucsb.nceas.metacat.shared.BaseException; |
... | ... | |
113 | 114 |
public static void writeFileToOutput(HttpServletResponse response, String fileDir, String fileName, int bufferSize) |
114 | 115 |
throws MetacatUtilException { |
115 | 116 |
String filePath = ""; |
117 |
InputStream inputStream = null; |
|
118 |
OutputStream outputStream = null; |
|
116 | 119 |
try { |
117 | 120 |
filePath = fileDir + FileUtil.getFS() + fileName; |
118 | 121 |
|
... | ... | |
120 | 123 |
String shortFileName = fileName.substring(lastFileSep + 1, fileName.length()); |
121 | 124 |
response.setHeader("Content-Disposition", "attachment; filename=\"" + shortFileName + "\""); |
122 | 125 |
|
123 |
InputStream inputStream = new FileInputStream(filePath);
|
|
124 |
OutputStream outputStream = response.getOutputStream();
|
|
126 |
inputStream = new FileInputStream(filePath); |
|
127 |
outputStream = response.getOutputStream(); |
|
125 | 128 |
|
126 | 129 |
byte[] byteBuffer = new byte[bufferSize]; |
127 | 130 |
|
128 | 131 |
int b = 0; |
129 | 132 |
while ((b = inputStream.read(byteBuffer)) != -1) { |
130 | 133 |
outputStream.write(byteBuffer, 0, b); |
131 |
} |
|
134 |
} |
|
135 |
outputStream.close(); |
|
136 |
inputStream.close(); |
|
132 | 137 |
|
133 | 138 |
} catch (FileNotFoundException fnfe) { |
134 | 139 |
throw new MetacatUtilException("Error finding file: " + filePath |
... | ... | |
136 | 141 |
} catch (IOException ioe) { |
137 | 142 |
throw new MetacatUtilException("I/O Error when writing: " + filePath |
138 | 143 |
+ " to output"); |
144 |
} finally { |
|
145 |
IOUtils.closeQuietly(inputStream); |
|
146 |
IOUtils.closeQuietly(outputStream); |
|
139 | 147 |
} |
140 | 148 |
} |
141 | 149 |
|
Also available in: Unified diff
Reviewed code for all uses of FileInputStream, checking to see if the method should be closing the stream, and if so, closing it in the method as well as in the finally clause to ensure we don't leak file descriptors.