Guest User

Untitled

a guest
Sep 8th, 2015
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1. @RequestMapping(value = "/add", method = RequestMethod.POST)
  2.     public String processAddNewProductForm(@ModelAttribute("newProduct") Product productToBeAdded, BindingResult result,
  3.             HttpServletRequest request) {
  4.         String[] suppressedFields = result.getSuppressedFields();
  5.         if (suppressedFields.length > 0) {
  6.             throw new RuntimeException("Attempting to bind disallowed fields: "
  7.                     + StringUtils.arrayToCommaDelimitedString(suppressedFields));
  8.         }
  9.         MultipartFile productImage = productToBeAdded.getProductImage();
  10.         String rootDirectory = request.getSession().getServletContext().getRealPath("/");
  11.         if (productImage != null && !productImage.isEmpty()) {
  12.             try {
  13.                 productImage.transferTo(
  14.                         new File(rootDirectory + "/resources/images/" + productToBeAdded.getProductId() + ".png"));
  15.             } catch (Exception e) {
  16.                 throw new RuntimeException("Product Image saving failed", e);
  17.             }
  18.         }
  19.  
  20.         MultipartFile pdfManual = productToBeAdded.getPdfManual();
  21.         if (pdfManual != null && !pdfManual.isEmpty()) {
  22.             try {
  23.                 pdfManual.transferTo(
  24.                         new File(rootDirectory + "/resources/pdfManuals/" + productToBeAdded.getPdfManual() + ".pdf"));
  25.             } catch (Exception e) {
  26.                 throw new RuntimeException("Product manual saving failed", e);
  27.             }
  28.         }
  29.  
  30.         productService.addProduct(productToBeAdded);
  31.         return "redirect:/products";
  32.     }
Advertisement
Add Comment
Please, Sign In to add comment