public String create(@Valid Contact contact, BindingResult bindingResult, Model uiModel,
HttpServletRequest httpServletRequest, RedirectAttributes redirectAttributes, Locale locale,
@RequestParam(value="file", required=false) Part file) {
logger.info("Creating contact");
if (bindingResult.hasErrors()) {
uiModel.addAttribute("message", new Message("error",
messageSource.getMessage("contact_save_fail", new Object[]{}, locale)));
uiModel.addAttribute("contact", contact);
return "contacts/create";
}
uiModel.asMap().clear();
redirectAttributes.addFlashAttribute("message", new Message("success",
messageSource.getMessage("contact_save_success", new Object[]{}, locale)));
logger.info("Contact id: " + contact.getId());
// Process upload file
if (file != null) {
logger.info("File name: " + file.getName());
logger.info("File size: " + file.getSize());
logger.info("File content type: " + file.getContentType());
byte[] fileContent = null;
try {
InputStream inputStream = file.getInputStream();
if (inputStream == null) logger.info("File inputstream is null");
fileContent = IOUtils.toByteArray(inputStream);
contact.setPhoto(fileContent);
} catch (IOException ex) {
logger.error("Error saving uploaded file");
}
contact.setPhoto(fileContent);
}
contactService.save(contact);
return "redirect:/contacts/" +
UrlUtil.encodeUrlPathSegment(contact.getId().toString(), httpServletRequest);
}
@RequestMapping(value = "/photo/{id}", method = RequestMethod.GET)
@ResponseBody
public byte[] downloadPhoto(@PathVariable("id") Long id) {
Contact contact = contactService.findById(id);
if (contact.getPhoto() != null) {
logger.info("Downloading photo for id: {} with size: {}", contact.getId(),
contact.getPhoto().length);
}
return contact.getPhoto();
}
}
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home