@RequestMapping(value = Paths.UPLOAD, method = RequestMethod.POST)
public @ResponseBody
String upload(MultipartHttpServletRequest request, HttpServletResponse response) {
UserDetails userDetails = getCurrentUserDetails();
LinkedList<FileMeta> files = new LinkedList<FileMeta>();
FileMeta fileMeta = null;
JSONObject results = new JSONObject();
// 1. build an iterator
Iterator<String> itr = request.getFileNames();
MultipartFile mpf = null;
// 2. get each file
while (itr.hasNext()) {
// 2.1 get next MultipartFile
mpf = request.getFile(itr.next());
System.out.println(mpf.getOriginalFilename() + " uploaded! " + files.size());
// 2.2 if files > 10 remove the first from the list
if (files.size() >= 10) files.pop();
// 2.3 create new fileMeta
fileMeta = new FileMeta();
fileMeta.setFileName(mpf.getOriginalFilename());
fileMeta.setFileSize(mpf.getSize() / 1024 + " Kb");
fileMeta.setFileType(mpf.getContentType());
try {
fileMeta.setBytes(mpf.getBytes());
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 2.4 add to files
files.add(fileMeta);
FileMeta savedFileMeta = writeFileToDisk(fileMeta);
if (savedFileMeta != null) {
nl.oneday.data.domain.uploads.File fileEntity = new nl.oneday.data.domain.uploads.File();
fileEntity.setFilename(fileMeta.getFileName());
fileEntity.setFilePath(uploadDirectory + userDetails.getUsername() + java.io.File.separator + fileMeta.getFileName());
fileEntity.setType(fileMeta.getFileType());