private void writeFilesAndAddToMap(HttpServletRequest request, Map<String, Object> variableMap, List<File> tempFileList) { // get multipart request StandardMultipartHttpServletRequest multipartHttpServletRequest = (StandardMultipartHttpServletRequest) request; // get file map from request Map<String, MultipartFile> fileMap = multipartHttpServletRequest.getFileMap(); for (Entry<String, MultipartFile> fileMapEntry : fileMap.entrySet()) { // write file uploads to disk File file = writeMultipartFile(fileMapEntry.getValue()); if (file != null) { // add to temp file list tempFileList.add(file); // add file paths to variable map variableMap.put(fileMapEntry.getKey(), file.getAbsolutePath()); } } }
/** * ## todo : add proper mapping. * * @param request {@link HttpServletRequest} */ @PostMapping("add/product") public Map<String, Boolean> addAndPersistProduct(final HttpServletRequest request) { final MultipartFile product = ((StandardMultipartHttpServletRequest) request).getMultiFileMap().getFirst("products"); final MultipartFile image = ((StandardMultipartHttpServletRequest) request).getMultiFileMap().getFirst("images"); // manually replace quote char final String commaSeparatedKeywords = request.getParameter("keywords").replaceAll("\"", ""); final String userName = request.getParameter("username"); final UserEntity userEntity = this.service.getUserByUserName(userName); final List<KeywordEntity> keywordEntityList = this.service.getKeywordEntityListByAlias(commaSeparatedKeywords); final ProductEntity productEntity = new ProductEntity(); // manually replace quote char productEntity.setName(request.getParameter("name").replaceAll("\"", "")); productEntity.setDescription(request.getParameter("description").replaceAll("\"", "")); try (final InputStream imgInput = image.getInputStream(); final InputStream cntInput = product.getInputStream()) { final byte[] imgArray = IOUtils.toByteArray(imgInput); // Apache commons IO. productEntity.setPreviewImage(imgArray); final byte[] cntArray = IOUtils.toByteArray(cntInput); // Apache commons IO. productEntity.setProductItem(cntArray); } catch (IOException e) { LOG.error(e.getMessage(), e); } productEntity.setUserEntity(userEntity); productEntity.setKeywordEntityList(keywordEntityList); this.service.persistProductEntity(productEntity); final Map<String, Boolean> returnMap = new HashMap<>(); returnMap.put("success", productEntity.getProductUuid() != null); return returnMap; }