return showForm(request, response, errors);
}
MultipartHttpServletRequest multipartRequest =
(MultipartHttpServletRequest) request;
CommonsMultipartFile file =
(CommonsMultipartFile) multipartRequest.getFile("file");
// the directory to upload to
String uploadDir =
getServletContext().getRealPath("/resources") + "/" +
request.getRemoteUser() + "/";
// Create the directory if it doesn't exist
File dirPath = new File(uploadDir);
if (!dirPath.exists()) {
dirPath.mkdirs();
}
//retrieve the file data
InputStream stream = file.getInputStream();
//write the file to the file specified
OutputStream bos =
new FileOutputStream(uploadDir + file.getOriginalFilename());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
//close the stream
stream.close();
// place the data into the request for retrieval on next page
request.setAttribute("friendlyName", fileUpload.getName());
request.setAttribute("fileName", file.getOriginalFilename());
request.setAttribute("contentType", file.getContentType());
request.setAttribute("size", file.getSize() + " bytes");
request.setAttribute("location",
dirPath.getAbsolutePath() + Constants.FILE_SEP +
file.getOriginalFilename());
String link =
request.getContextPath() + "/resources" + "/" +
request.getRemoteUser() + "/";
request.setAttribute("link", link + file.getOriginalFilename());
return new ModelAndView(getSuccessView());
}