List of usage examples for org.springframework.web.multipart.commons CommonsMultipartFile getOriginalFilename
@Override
public String getOriginalFilename()
From source file:org.springframework.web.multipart.commons.CommonsFileUploadSupport.java
/** * Cleanup the Spring MultipartFiles created during multipart parsing, * potentially holding temporary data on disk. * <p>Deletes the underlying Commons FileItem instances. * @param multipartFiles Collection of MultipartFile instances * @see org.apache.commons.fileupload.FileItem#delete() *//*from ww w . j av a 2s. co m*/ protected void cleanupFileItems(MultiValueMap<String, MultipartFile> multipartFiles) { for (List<MultipartFile> files : multipartFiles.values()) { for (MultipartFile file : files) { if (file instanceof CommonsMultipartFile) { CommonsMultipartFile cmf = (CommonsMultipartFile) file; cmf.getFileItem().delete(); if (logger.isDebugEnabled()) { logger.debug("Cleaning up multipart file [" + cmf.getName() + "] with original filename [" + cmf.getOriginalFilename() + "], stored " + cmf.getStorageDescription()); } } } } }
From source file:org.springframework.web.multipart.commons.CommonsMultipartResolverTests.java
private void doTestFiles(MultipartHttpServletRequest request) throws IOException { Set<String> fileNames = new HashSet<>(); Iterator<String> fileIter = request.getFileNames(); while (fileIter.hasNext()) { fileNames.add(fileIter.next());/*from w w w . java2 s. co m*/ } assertEquals(3, fileNames.size()); assertTrue(fileNames.contains("field1")); assertTrue(fileNames.contains("field2")); assertTrue(fileNames.contains("field2x")); CommonsMultipartFile file1 = (CommonsMultipartFile) request.getFile("field1"); CommonsMultipartFile file2 = (CommonsMultipartFile) request.getFile("field2"); CommonsMultipartFile file2x = (CommonsMultipartFile) request.getFile("field2x"); Map<String, MultipartFile> fileMap = request.getFileMap(); assertEquals(3, fileMap.size()); assertTrue(fileMap.containsKey("field1")); assertTrue(fileMap.containsKey("field2")); assertTrue(fileMap.containsKey("field2x")); assertEquals(file1, fileMap.get("field1")); assertEquals(file2, fileMap.get("field2")); assertEquals(file2x, fileMap.get("field2x")); MultiValueMap<String, MultipartFile> multiFileMap = request.getMultiFileMap(); assertEquals(3, multiFileMap.size()); assertTrue(multiFileMap.containsKey("field1")); assertTrue(multiFileMap.containsKey("field2")); assertTrue(multiFileMap.containsKey("field2x")); List<MultipartFile> field1Files = multiFileMap.get("field1"); assertEquals(2, field1Files.size()); assertTrue(field1Files.contains(file1)); assertEquals(file1, multiFileMap.getFirst("field1")); assertEquals(file2, multiFileMap.getFirst("field2")); assertEquals(file2x, multiFileMap.getFirst("field2x")); assertEquals("type1", file1.getContentType()); assertEquals("type2", file2.getContentType()); assertEquals("type2", file2x.getContentType()); assertEquals("field1.txt", file1.getOriginalFilename()); assertEquals("field2.txt", file2.getOriginalFilename()); assertEquals("field2x.txt", file2x.getOriginalFilename()); assertEquals("text1", new String(file1.getBytes())); assertEquals("text2", new String(file2.getBytes())); assertEquals(5, file1.getSize()); assertEquals(5, file2.getSize()); assertTrue(file1.getInputStream() instanceof ByteArrayInputStream); assertTrue(file2.getInputStream() instanceof ByteArrayInputStream); File transfer1 = new File("C:/transfer1"); file1.transferTo(transfer1); File transfer2 = new File("C:/transfer2"); file2.transferTo(transfer2); assertEquals(transfer1, ((MockFileItem) file1.getFileItem()).writtenFile); assertEquals(transfer2, ((MockFileItem) file2.getFileItem()).writtenFile); }
From source file:org.webcurator.ui.target.controller.TargetSeedsHandler.java
public ModelAndView processOther(TabbedController tc, Tab currentTab, HttpServletRequest req, HttpServletResponse res, Object comm, BindException errors) { SeedsCommand command = (SeedsCommand) comm; TargetEditorContext ctx = getEditorContext(req); if (authorityManager.hasAtLeastOnePrivilege(ctx.getTarget(), Privilege.MODIFY_TARGET, Privilege.CREATE_TARGET)) {/*from w w w .j ava 2s. co m*/ if (command.isAction(SeedsCommand.ACTION_ADD)) { if (errors.hasErrors()) { // Go to the Seeds tab. TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.addObject(Constants.GBL_CMD_DATA, command); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } else { // Create the seed business object. Seed seed = businessObjectFactory.newSeed(ctx.getTarget()); seed.setSeed(command.getSeed().trim()); //remove any trailing spaces // Set the first seed to be primary, others to be secondary. seed.setPrimary(ctx.getTarget().getSeeds().size() == 0); // Associate with the correct permissions. long option = command.getPermissionMappingOption().longValue(); boolean addedExclusions = false; try { addedExclusions = mapSeed(ctx, seed, option); ctx.putObject(seed); ctx.getTarget().addSeed(seed); } // The seed we've tried to link to is from a different // agency. catch (SeedLinkWrongAgencyException ex) { errors.reject("target.seeds.link.wrong_agency", new Object[] {}, "One of the selected seeds cannot be linked because it belongs to another agency."); } // Go to the Seeds tab. TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); if (addedExclusions) { tmav.addObject(Constants.GBL_MESSAGES, messageSource .getMessage("target.linkseeds.exclusions", new Object[] {}, Locale.getDefault())); } return tmav; } } if (command.isAction(SeedsCommand.ACTION_REMOVE)) { // Remove the URL. Seed seedToRemove = (Seed) ctx.getObject(Seed.class, command.getSelectedSeed()); ctx.getTarget().removeSeed(seedToRemove); // Go back to the URLs tab. TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } if (command.isAction(SeedsCommand.ACTION_REMOVE_SELECTED)) { // Remove the selected URLs. Set<Seed> seeds = ctx.getTarget().getSeeds(); Set<Seed> seedsToRemove = new HashSet<Seed>(); Iterator<Seed> it = seeds.iterator(); while (it.hasNext()) { Seed seed = it.next(); if (isSelected(req, seed)) { seedsToRemove.add(seed); } } it = seedsToRemove.iterator(); while (it.hasNext()) { Seed seedToRemove = it.next(); ctx.getTarget().removeSeed(seedToRemove); } // Go back to the URLs tab. TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } if (command.isAction(SeedsCommand.ACTION_TOGGLE_PRIMARY)) { Seed seed = (Seed) ctx.getObject(Seed.class, command.getSelectedSeed()); if (targetManager.getAllowMultiplePrimarySeeds()) { //toggle the selected seed seed.setPrimary(!seed.isPrimary()); } else { //Reset all seeds to not primary List<Seed> seeds = ctx.getSortedSeeds(); Iterator<Seed> it = seeds.iterator(); while (it.hasNext()) { Seed listItem = it.next(); listItem.setPrimary(false); } //Set the selected seed to primary seed.setPrimary(true); } // Go back to the URLs tab. TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } if (command.isAction(SeedsCommand.ACTION_UNLINK)) { Seed seed = (Seed) ctx.getObject(Seed.class, command.getSelectedSeed()); Permission permission = (Permission) ctx.getObject(Permission.class, command.getSelectedPermission()); seed.removePermission(permission); // Go back to the URLs tab. TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } if (command.isAction(SeedsCommand.ACTION_UNLINK_SELECTED)) { //Unlink all permissions from the selected seeds Set<Seed> seeds = ctx.getTarget().getSeeds(); Iterator<Seed> it = seeds.iterator(); while (it.hasNext()) { Seed seed = it.next(); if (isSelected(req, seed)) { Set<Permission> permissions = seed.getPermissions(); permissions.clear(); seed.getTarget().setDirty(true); } } // Go back to the URLs tab. TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } if (command.isAction(SeedsCommand.ACTION_LINK_NEW)) { SeedsCommand newCommand = new SeedsCommand(); Seed selectedSeed = (Seed) ctx.getObject(Seed.class, command.getSelectedSeed()); newCommand.setSelectedSeed(command.getSelectedSeed()); newCommand.setSearchType(SeedsCommand.SEARCH_URL); newCommand.setUrlSearchCriteria(selectedSeed.getSeed()); boolean doSearch = validator.validateLinkSearch(command, errors); return processLinkSearch(tc, ctx.getTarget(), newCommand, doSearch, errors); } if (command.isAction(SeedsCommand.ACTION_LINK_SELECTED)) { //Unlink all permissions from the selected seeds Set<Seed> seeds = ctx.getTarget().getSeeds(); Set<Seed> selectedSeeds = new HashSet<Seed>(); String seedList = ""; Iterator<Seed> it = seeds.iterator(); while (it.hasNext()) { Seed seed = it.next(); if (isSelected(req, seed)) { selectedSeeds.add(seed); if (seedList.isEmpty()) { seedList = seed.getIdentity(); } else { seedList += "," + seed.getIdentity(); } } } if (selectedSeeds.size() > 0) { SeedsCommand newCommand = new SeedsCommand(); newCommand.setSelectedSeed(seedList); newCommand.setSearchType(SeedsCommand.SEARCH_SITES); newCommand.setSiteSearchCriteria(""); ctx.putAllObjects(selectedSeeds); return processLinkSearch(tc, ctx.getTarget(), newCommand, false, errors); } else { // Go back to the URLs tab. TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } } if (command.isAction(SeedsCommand.ACTION_LINK_NEW_SEARCH)) { if (errors.hasErrors()) { return processLinkSearch(tc, ctx.getTarget(), command, false, errors); } else { SeedsCommand newCommand = new SeedsCommand(); newCommand.setSelectedSeed(command.getSelectedSeed()); newCommand.setSearchType(command.getSearchType()); newCommand.setSiteSearchCriteria(command.getSiteSearchCriteria()); newCommand.setUrlSearchCriteria(command.getUrlSearchCriteria()); newCommand.setPageNumber(command.getPageNumber()); return processLinkSearch(tc, ctx.getTarget(), newCommand, true, errors); } } if (command.isAction(SeedsCommand.ACTION_LINK_NEW_CONFIRM)) { if (errors.hasErrors()) { boolean doSearch = validator.validateLinkSearch(command, errors); return processLinkSearch(tc, ctx.getTarget(), command, doSearch, errors); } else { // Go back to the URLs tab. TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); // Get the selected seed(s). String[] seedList = command.getSelectedSeed().split(","); for (int s = 0; s < seedList.length; s++) { Seed theSeed = (Seed) ctx.getObject(Seed.class, seedList[s]); // Link all the selected permissions. String[] perms = command.getLinkPermIdentity(); Set<Permission> toLink = new HashSet<Permission>(); boolean wrongAgencyPermission = false; for (int i = 0; i < perms.length && !wrongAgencyPermission; i++) { Permission linkPerm = targetManager.loadPermission(ctx, perms[i]); toLink.add(linkPerm); if (!linkPerm.getOwningAgency().equals(ctx.getTarget().getOwner().getAgency())) { wrongAgencyPermission = true; } } try { boolean addedExclusions = linkSeed(ctx.getTarget(), theSeed, toLink); if (addedExclusions) { tmav.addObject(Constants.GBL_MESSAGES, messageSource.getMessage( "target.linkseeds.exclusions", new Object[] {}, Locale.getDefault())); } } catch (SeedLinkWrongAgencyException ex) { errors.reject("target.seeds.link.wrong_agency", new Object[] {}, "One of the selected seeds cannot be linked because it belongs to another agency."); boolean doSearch = validator.validateLinkSearch(command, errors); return processLinkSearch(tc, ctx.getTarget(), command, doSearch, errors); } } return tmav; } } if (command.isAction(SeedsCommand.ACTION_LINK_NEW_CANCEL)) { // Go back to the URLs tab. TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } if (command.isAction(SeedsCommand.ACTION_START_IMPORT)) { return getImportSeedsTabModel(tc, ctx); } if (command.isAction(SeedsCommand.ACTION_DO_IMPORT)) { BufferedReader reader = null; MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req; CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("seedsFile"); if (command.getSeedsFile().length == 0 || file.getOriginalFilename() == null || "".equals(file.getOriginalFilename().trim())) { errors.reject("target.seeds.import.nofile"); TabbedModelAndView tmav = getImportSeedsTabModel(tc, ctx); tmav.addObject(Constants.GBL_ERRORS, errors); return tmav; } if (!file.getOriginalFilename().endsWith(".txt") && !"text/plain".equals(file.getContentType())) { errors.reject("target.seeds.import.filetype"); TabbedModelAndView tmav = getImportSeedsTabModel(tc, ctx); tmav.addObject(Constants.GBL_ERRORS, errors); return tmav; } else { try { reader = new BufferedReader( new InputStreamReader(new ByteArrayInputStream(command.getSeedsFile()))); List<Seed> validSeeds = new LinkedList<Seed>(); boolean success = true; int lineNumber = 1; String line = reader.readLine(); SeedsCommand importCommand = new SeedsCommand(); while (success && line != null) { if (!line.startsWith("#") && !"".equals(line.trim())) { Seed seed = businessObjectFactory.newSeed(ctx.getTarget()); importCommand.setSeed(line); seed.setSeed(importCommand.getSeed()); if (UrlUtils.isUrl(seed.getSeed())) { mapSeed(ctx, seed, command.getPermissionMappingOption()); // Update the target. ctx.putObject(seed); validSeeds.add(seed); } else { success = false; } } if (success) { line = reader.readLine(); lineNumber++; } } if (success) { for (Seed seed : validSeeds) { ctx.getTarget().addSeed(seed); } } else { errors.reject("target.seeds.import.badline", new Object[] { lineNumber }, "Bad seed detected on line: " + lineNumber); } } catch (SeedLinkWrongAgencyException ex) { errors.reject("target.seeds.link.wrong_agency", new Object[] {}, "One of the selected seeds cannot be linked because it belongs to another agency."); } catch (IOException ex) { errors.reject("target.seeds.import.ioerror"); log.error("Failed to import seeds", ex); } finally { try { reader.close(); } catch (Exception ex) { log.debug("Failed to close uploaded seeds file", ex); } } if (errors.hasErrors()) { TabbedModelAndView tmav = getImportSeedsTabModel(tc, ctx); tmav.addObject(Constants.GBL_ERRORS, errors); return tmav; } else { // Go back to the URLs tab. TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } } } } if (command.isAction(SeedsCommand.ACTION_SET_NAME)) { String id = command.getUpdatedNameSeedId(); if (id != null) { for (Seed seed : ctx.getSortedSeeds()) { if (seed.getOid().equals(Long.valueOf(id))) { String value = command.getUpdatedNameSeedValue(); if (value.trim().equals("")) { errors.reject("target.seeds.name.edit.required"); TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.addObject(Constants.GBL_CMD_DATA, command); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } if (UrlUtils.isUrl(seed.getSeed())) { seed.setSeed(UrlUtils.fixUrl(value)); ctx.putObject(seed); } else { errors.reject("target.seeds.name.edit.invalid"); TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.addObject(Constants.GBL_CMD_DATA, command); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } System.out.println("Found seed " + id); } } } } TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; }
From source file:ubic.gemma.web.util.upload.CommonsMultipartMonitoredResolver.java
@Override public void cleanupMultipart(MultipartHttpServletRequest request) { if (request instanceof FailedMultipartHttpServletRequest) return;//from ww w .j a v a 2s. c o m Map<String, MultipartFile> multipartFiles = request.getFileMap(); for (MultipartFile multipartFile : multipartFiles.values()) { CommonsMultipartFile file = (CommonsMultipartFile) multipartFile; if (logger.isDebugEnabled()) { logger.debug("Cleaning up multipart file [" + file.getName() + "] with original filename [" + file.getOriginalFilename() + "], stored " + file.getStorageDescription()); } file.getFileItem().delete(); } }