Writes a temporary file into the default temporary directory, form an InputStream a unique ID is generated and associated automaticaly with the file name...
(InputStream contentStream)
| 190 | * with the file name... |
| 191 | */ |
| 192 | public static File writeTempFile(InputStream contentStream) |
| 193 | throws IOException { |
| 194 | |
| 195 | File resourceFile = null; |
| 196 | FileOutputStream resourceFileOutputStream = null; |
| 197 | |
| 198 | try { |
| 199 | // create a temporary file name |
| 200 | resourceFile = File.createTempFile("gateResource", ".tmp"); |
| 201 | resourceFileOutputStream = new FileOutputStream(resourceFile); |
| 202 | resourceFile.deleteOnExit(); |
| 203 | |
| 204 | if(contentStream == null) return resourceFile; |
| 205 | |
| 206 | int bytesRead = 0; |
| 207 | final int readSize = 1024; |
| 208 | byte[] bytes = new byte[readSize]; |
| 209 | while((bytesRead = contentStream.read(bytes, 0, readSize)) != -1) |
| 210 | resourceFileOutputStream.write(bytes, 0, bytesRead); |
| 211 | } finally { |
| 212 | IOUtils.closeQuietly(resourceFileOutputStream); |
| 213 | IOUtils.closeQuietly(contentStream); |
| 214 | } |
| 215 | |
| 216 | return resourceFile; |
| 217 | }// writeTempFile() |
| 218 | |
| 219 | /** |
| 220 | * Writes aString into a temporary file located inside |