Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
k-tamura committed Sep 8, 2017
1 parent 2e0af17 commit 4a228da
Showing 1 changed file with 31 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,37 +129,41 @@ protected void doPost(HttpServletRequest req, HttpServletResponse res) throws Se
private List<File> saveUploadedFiles(HttpServletRequest request)
throws IOException, ServletException {
List<File> listFiles = new ArrayList<File>();
byte[] buffer = new byte[4096];
int bytesRead;
Collection<Part> multiparts = request.getParts();
if (!multiparts.isEmpty()) {
for (Part part : request.getParts()) {
// creates a file to be saved
String fileName = extractFileName(part);
if (StringUtils.isBlank(fileName)) {
// not attachment part, continue
continue;
}
try {
byte[] buffer = new byte[4096];
int bytesRead;
Collection<Part> multiparts = request.getParts();
if (!multiparts.isEmpty()) {
for (Part part : request.getParts()) {
// creates a file to be saved
String fileName = extractFileName(part);
if (StringUtils.isBlank(fileName)) {
// not attachment part, continue
continue;
}

File saveFile = new File(fileName);
log.debug("Uploaded file is saved on: " + saveFile.getAbsolutePath());
FileOutputStream outputStream = null;
InputStream inputStream = null;
try {
outputStream = new FileOutputStream(saveFile);
// saves uploaded file
inputStream = part.getInputStream();
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
File saveFile = new File(fileName);
log.debug("Uploaded file is saved on: " + saveFile.getAbsolutePath());
FileOutputStream outputStream = null;
InputStream inputStream = null;
try {
outputStream = new FileOutputStream(saveFile);
// saves uploaded file
inputStream = part.getInputStream();
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
} catch (Exception e) {
log.error("Exception occurs: ", e);
} finally {
Closer.close(outputStream);
Closer.close(inputStream);
}
} catch (Exception e) {
log.error("Exception occurs: ", e);
} finally {
Closer.close(outputStream);
Closer.close(inputStream);
listFiles.add(saveFile);
}
listFiles.add(saveFile);
}
} catch (Exception e) {
log.error("Exception occurs: ", e);
}
return listFiles;
}
Expand Down

0 comments on commit 4a228da

Please sign in to comment.