Skip to content

Commit 001fa2a

Browse files
authored
Merge pull request eugenp#3629 from Thoughtscript/master
Issue eugenp#3628 - BAEL-86: Unclosed Input Stream
2 parents 6eeff13 + 59cc02f commit 001fa2a

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

‎spring-dispatcher-servlet/src/main/java/com/baeldung/springdispatcherservlet/controller/MultipartController.java‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,20 @@ public ModelAndView FileuploadController(@RequestParam("file") MultipartFile fil
2525
try {
2626
InputStream in = file.getInputStream();
2727
String path = new File(".").getAbsolutePath();
28-
FileOutputStream f = new FileOutputStream(path.substring(0, path.length()-1)+ "/uploads/" + file.getOriginalFilename());
29-
int ch;
30-
while ((ch = in.read()) != -1) {
31-
f.write(ch);
28+
FileOutputStream f = new FileOutputStream(path.substring(0, path.length() - 1) + "/uploads/" + file.getOriginalFilename());
29+
try {
30+
int ch;
31+
while ((ch = in.read()) != -1) {
32+
f.write(ch);
33+
}
34+
modelAndView.getModel().put("message", "File uploaded successfully!");
35+
} catch (Exception e) {
36+
System.out.println("Exception uploading multipart: " + e);
37+
} finally {
38+
f.flush();
39+
f.close();
40+
in.close();
3241
}
33-
f.flush();
34-
f.close();
35-
modelAndView.getModel().put("message", "File uploaded successfully!");
3642
} catch (Exception e) {
3743
System.out.println("Exception uploading multipart: " + e);
3844
}

0 commit comments

Comments
 (0)