Example usage for org.springframework.web.multipart.support DefaultMultipartHttpServletRequest getFileMap

List of usage examples for org.springframework.web.multipart.support DefaultMultipartHttpServletRequest getFileMap

Introduction

In this page you can find the example usage for org.springframework.web.multipart.support DefaultMultipartHttpServletRequest getFileMap.

Prototype

@Override
    public Map<String, MultipartFile> getFileMap() 

Source Link

Usage

From source file:org.tangram.components.spring.SpringRequestParameterAccess.java

/**
 * Weak visibility to avoid direct instanciation.
 *//*from   w ww  .  j  av  a  2  s  .co m*/
@SuppressWarnings("unchecked")
SpringRequestParameterAccess(HttpServletRequest request) throws Exception {
    if (request instanceof DefaultMultipartHttpServletRequest) {
        DefaultMultipartHttpServletRequest r = (DefaultMultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = r.getFileMap();

        for (Entry<String, MultipartFile> entry : fileMap.entrySet()) {
            if (entry.getValue().getContentType().equals(StreamingMultipartResolver.ERROR)) {
                throw new Exception(entry.getValue().getOriginalFilename());
            } // if
            String key = entry.getKey();
            String filename = entry.getValue().getName();
            LOG.info("() name {}", filename);
            LOG.info("() size {}", entry.getValue().getSize());
            final String originalFilename = entry.getValue().getOriginalFilename();
            LOG.debug("() key {} original filename {}", key, originalFilename);
            if (filename.length() > 0) {
                LOG.info("multipart file {}", key);
                try {
                    originalNames.put(key, originalFilename);
                    blobs.put(key, entry.getValue().getBytes());
                } catch (IOException ex) {
                    LOG.error("()", ex);
                } // try/catch
            } // if
        } // for
    } // if
    parameterMap = request.getParameterMap();
}

From source file:org.wise.vle.web.AssetManager.java

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 *///  w  ww. jav a 2  s  .c o  m
@RequestMapping(method = RequestMethod.POST)
protected ModelAndView doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String command = request.getParameter("command");
    String type = request.getParameter("type");

    if ("studentAssetManager".equals(type)) {
        // the user is a student

        if ("remove".equals(command)) {
            // the student is removing an asset

            User user = ControllerUtil.getSignedInUser();

            // get the run
            String runId = request.getParameter("runId");
            Run run = null;
            try {
                run = runService.retrieveById(new Long(runId));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (ObjectNotFoundException e) {
                e.printStackTrace();
            }

            // get the workgroup id
            List<Workgroup> workgroupListByOfferingAndUser = workgroupService
                    .getWorkgroupListByOfferingAndUser(run, user);
            Workgroup workgroup = workgroupListByOfferingAndUser.get(0);
            Long workgroupId = workgroup.getId();

            // get the directory name for the workgroup for this run
            String dirName = run.getId() + "/" + workgroupId + "/unreferenced"; // looks like /studentuploads/[runId]/[workgroupId]/unreferenced

            // get the student uploads base directory path
            String path = wiseProperties.getProperty("studentuploads_base_dir");

            // get the file name the student wants to remove
            String assetFileName = request.getParameter("asset");

            // remove the file from the student asset folder
            String result = removeAsset(path, dirName, assetFileName);

            response.getWriter().write(result);
        } else if ("studentAssetCopyForReference".equals(command)) {
            User user = ControllerUtil.getSignedInUser();

            // get the run
            String runId = request.getParameter("runId");
            Run run = null;
            try {
                run = runService.retrieveById(new Long(runId));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (ObjectNotFoundException e) {
                e.printStackTrace();
            }

            // get the workgroup id
            List<Workgroup> workgroupListByOfferingAndUser = workgroupService
                    .getWorkgroupListByOfferingAndUser(run, user);
            Workgroup workgroup = workgroupListByOfferingAndUser.get(0);
            Long workgroupId = workgroup.getId();

            // looks like /studentuploads/[runId]/[workgroupId]/unreferenced
            String dirName = run.getId() + "/" + workgroupId + "/unreferenced";

            String referencedDirName = "";
            String commandParameter = request.getParameter("command");
            if (commandParameter != null && "studentAssetCopyForReference".equals(commandParameter)) {
                // if we're copying student asset for reference, also pass along the referenced dir. looks like /studentuploads/[runId]/[workgroupId]/referenced
                referencedDirName = run.getId() + "/" + workgroupId + "/referenced";
            }

            // get the file name to copy
            String fileName = request.getParameter("assetFilename");

            String result = copyAssetForReference(dirName, referencedDirName, fileName);

            response.getWriter().write(result);
        } else if ("uploadAsset".equals(command)) {
            // the student is uploading an asset

            User user = ControllerUtil.getSignedInUser();

            // get the run
            String runId = request.getParameter("runId");
            Run run = null;
            try {
                run = runService.retrieveById(new Long(runId));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (ObjectNotFoundException e) {
                e.printStackTrace();
            }

            // get the workgroup id
            List<Workgroup> workgroupListByOfferingAndUser = workgroupService
                    .getWorkgroupListByOfferingAndUser(run, user);
            Workgroup workgroup = workgroupListByOfferingAndUser.get(0);
            Long workgroupId = workgroup.getId();

            // get the directory name for the workgroup for this run
            String dirName = run.getId() + "/" + workgroupId + "/unreferenced";

            // get the student uploads base directory path
            String path = wiseProperties.getProperty("studentuploads_base_dir");
            Long studentMaxTotalAssetsSize = new Long(
                    wiseProperties.getProperty("student_max_total_assets_size", "5242880"));
            String pathToCheckSize = path + "/" + dirName;

            DefaultMultipartHttpServletRequest multiRequest = (DefaultMultipartHttpServletRequest) request;
            Map<String, MultipartFile> fileMap = multiRequest.getFileMap();

            // upload the files
            String result = uploadAsset(fileMap, path, dirName, pathToCheckSize, studentMaxTotalAssetsSize);

            response.getWriter().write(result);
        }
    }
    return null;
}