// are we expecting file upload?
if (Uploadable.class.isAssignableFrom(command.getClass())
&& request instanceof MultipartHttpServletRequest) {
List<FileInfo> files = null;
Uploadable upload = (Uploadable) command;
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
List<MultipartFile> multipartFiles = multipartRequest
.getFiles(htmlId);
// check if there is anything to process
if (multipartFiles != null && !multipartFiles.isEmpty())
// loop through all the attachments
for (MultipartFile multipartFile : multipartFiles) {
// check if there is anything to process
if (multipartFile != null && !multipartFile.isEmpty()) {
FileInfo fileInfo = new FileInfo();
fileInfo.setFilename(multipartFile.getOriginalFilename());
fileInfo.setFileSize(multipartFile.getSize());
fileInfo.setFullPath(uploadPath);
try {
// setup the directory
File directory;
directory = FileUtil.createDirectory(uploadPath);
String subdir = directory.getAbsoluteFile()
+ File.separator
+ DateUtil.convertShortDate(new Date());
File subDirectory = FileUtil.createDirectory(subdir);
String filePath = subDirectory.getAbsoluteFile()
+ File.separator
+ multipartFile.getOriginalFilename();
// change filename if already existing in the given path
// to avoid overriding of files
if (fileInfoService.getFileInfoByFullPath(filePath) != null) {
Long fileCnt = 1L;
String newFilePath = subDirectory.getAbsoluteFile()
+ File.separator + fileCnt.toString() + "_"
+ multipartFile.getOriginalFilename();
while (fileInfoService
.getFileInfoByFullPath(newFilePath) != null) {
fileCnt++;
newFilePath = subDirectory.getAbsoluteFile()
+ File.separator + fileCnt.toString()
+ "_"
+ multipartFile.getOriginalFilename();
}
filePath = newFilePath;
fileInfo.setOriginalFileName(multipartFile
.getOriginalFilename());
}
File uploadFile = new File(filePath);
// update file path information
fileInfo.setFullPath(uploadFile.getAbsolutePath());
_log.debug("Uploading file to "
+ fileInfo.getFullPath());
// now copy the file
FileUtil.copyMultipartFile(multipartFile, uploadFile);
// add fileinfo
if (isMultipleUpload())
files = upload.getFiles();
if (files == null)
files = new ArrayList<FileInfo>();
files.add(fileInfo);
upload.setFiles(files);
} catch (IOException e) {
_log.error("Failed to upload file.", e);
throw e;
}