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();