Пример #1
0
 @RequestMapping(value = "/photo/{id}", method = RequestMethod.GET)
 @ResponseBody
 public byte[] downloadPhoto(@PathVariable("id") Long id) {
   Hotel hotel = hotelService.findById(id);
   if (hotel.getPhoto() != null) {
     logger.info(
         "Downloading photo for id: {} with size: {}", hotel.getId(), hotel.getPhoto().length);
   }
   return hotel.getPhoto();
 }
Пример #2
0
  @RequestMapping(params = "form", method = RequestMethod.POST)
  public String create(
      Hotel hotel,
      BindingResult bindingResult,
      Model uiModel,
      HttpServletRequest httpServletRequest,
      RedirectAttributes redirectAttributes,
      Locale locale,
      @RequestParam(value = "file", required = false) Part file) {
    logger.info("Create Hotel");

    if (bindingResult.hasErrors()) {
      uiModel.addAttribute(
          "message",
          new Message(
              "error", messageSource.getMessage("hotel_save_fail", new Object[] {}, locale)));
      uiModel.addAttribute("hotel", hotel);
      return "hotels/create";
    }
    uiModel.asMap().clear();
    redirectAttributes.addFlashAttribute(
        "message",
        new Message(
            "success", messageSource.getMessage("hotel_save_success", new Object[] {}, locale)));
    // 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);
        hotel.setPhoto(fileContent);
      } catch (IOException ex) {
        logger.error("Error saving uploaded file");
      }
      hotel.setPhoto(fileContent);
    }
    hotelService.save(hotel);
    // logger.info("Hotel info: "+hotel.getId()+" "+hotel.getName()+" "+hotel.getPhone()+"
    // "+hotel.getAddress()+" "+hotel.getEmail()+" "+hotel.getPrice()+" "+hotel.getDescription());
    return "redirect:/hotels/"
        + UrlUtil.encodeUrlPathSegment(hotel.getId().toString(), httpServletRequest);
  }