Example usage for org.springframework.web.multipart MultipartFile getOriginalFilename

List of usage examples for org.springframework.web.multipart MultipartFile getOriginalFilename

Introduction

In this page you can find the example usage for org.springframework.web.multipart MultipartFile getOriginalFilename.

Prototype

@Nullable
String getOriginalFilename();

Source Link

Document

Return the original filename in the client's filesystem.

Usage

From source file:org.trpr.platform.batch.impl.spring.web.SynchronizationController.java

/**
 * Receiver methods start//from   w ww .ja  v  a 2  s .  c o m
 * These methods receive the job configuration files, dependency files and job loading requests.
 * This method is synchronized as 2 job requests cannot be processed simultaneously
 */
@RequestMapping(value = SynchronizationController.PUSH_URL, method = RequestMethod.POST)
public synchronized String jobReceiver(ModelMap model, @RequestParam String jobName,
        @RequestParam(value = "jobConfig") MultipartFile jobConfig,
        @RequestParam(value = "depFiles[]", required = false) MultipartFile[] depFiles) {
    jobName = jobName.trim();
    LOGGER.info("Push job request received for job: " + jobName);
    //Upload configuration file
    if (this.jobService.contains(jobName)) {
        LOGGER.info("Warning: " + jobName + " already exists. Modifying old file");
    }
    try {
        //Set XML File
        List<String> jobNames = new LinkedList<String>();
        jobNames.add(jobName);
        this.jobConfigService.setJobConfig(jobNames, new ByteArrayResource(jobConfig.getBytes()));
        LOGGER.info("Success in deploying configuration file for: " + jobName);
        model.addAttribute("Message", "success");
    } catch (Exception e) {
        model.addAttribute("Message", "Error while writing the job config file");
        LOGGER.warn("Unable to deploy the job. Please check that you have write permission. Nested exception: "
                + e.getMessage());
        return "sync/Message";
    }
    //Upload dependency Files
    if (depFiles != null && depFiles.length != 0) { //Dep files exist
        for (MultipartFile depFile : depFiles) {
            try {
                //Set dependencies
                LOGGER.info("Request to deploy file: " + jobName + " " + depFile.getOriginalFilename() + " "
                        + depFile.getSize());
                List<String> jobNames = new LinkedList<String>();
                jobNames.add(jobName);
                this.jobConfigService.addJobDependency(jobNames, depFile.getOriginalFilename(),
                        depFile.getBytes());
                LOGGER.info("Success in deploying dependency file for: " + jobName);
                model.addAttribute("Message", "success");
            } catch (Exception e) {
                LOGGER.error("Exception while deploying Dependency file: ", e);
                model.addAttribute("Message",
                        "Unexpected error while deploying dependencyFile: " + depFile.getOriginalFilename());
            }
        }
    }
    LOGGER.info("Deploy request");
    //Deploy request
    try {
        List<String> jobNames = new LinkedList<String>();
        jobNames.add(jobName);
        this.jobConfigService.deployJob(jobNames);
        LOGGER.info("Success in deploying: " + jobName);
        model.addAttribute("Message", "success");
    } catch (Exception e) {
        LOGGER.error("Error while deploying job: " + jobName, e);
        model.addAttribute("Message", "Unexpected error while loading: " + jobName);
    }
    return "sync/Message";
}

From source file:com.springsource.html5expense.controllers.ExpenseReportingApiController.java

@RequestMapping(value = "/receipts", method = RequestMethod.POST)
@ResponseBody//from w  ww  . j  a  v  a  2s  .  c o m
public String attachReceipt(@RequestParam("reportId") Long reportId,
        @RequestParam("expenseId") Integer expenseId, @RequestParam("file") MultipartFile file) {
    try {
        byte[] bytesForImage = file.getBytes();
        String ext = findExtensionFromFileName(file.getOriginalFilename());
        if (ext != null) {
            ext = ext.trim().toLowerCase();
        }
        String receiptKey = service.attachReceipt(reportId, expenseId, ext, bytesForImage);
        return receiptKey;
    } catch (Throwable th) {
        if (log.isErrorEnabled()) {
            log.error("Something went wrong trying to write the file out.", th);
        }
    }
    return null;
}

From source file:eea.eprtr.cms.controller.FileOpsController.java

/**
 * Upload file for transfer.// w  w  w  .j av a 2 s. co  m
 */
@RequestMapping(value = "/filecatalogue", method = RequestMethod.POST)
public String importFile(@RequestParam("file") MultipartFile myFile,
        final RedirectAttributes redirectAttributes, final HttpServletRequest request) throws IOException {

    if (myFile == null || myFile.getOriginalFilename() == null) {
        redirectAttributes.addFlashAttribute("message", "Select a file to upload");
        return "redirect:filecatalogue";
    }
    String fileName = storeFile(myFile);
    redirectAttributes.addFlashAttribute("filename", fileName);
    StringBuffer requestUrl = request.getRequestURL();
    redirectAttributes.addFlashAttribute("url",
            requestUrl.substring(0, requestUrl.length() - "/filecatalogue".length()));
    return "redirect:filecatalogue";
}

From source file:org.craftercms.search.controller.SearchRestController.java

@RequestMapping(value = URL_UPDATE_DOCUMENT, method = RequestMethod.POST)
@ResponseBody//  w  w w.  ja v  a 2s .  c o m
public String updateDocument(@RequestPart(REQUEST_PARAM_SITE) String site,
        @RequestPart(REQUEST_PARAM_ID) String id, @RequestPart(REQUEST_PARAM_DOCUMENT) MultipartFile document,
        HttpServletRequest request) throws SearchException {

    try {
        File tmpFile = File.createTempFile("crafter" + document.getOriginalFilename(), "");
        Map<String, String> additionalFields = getAdditionalFields(request);

        document.transferTo(tmpFile);

        String result = searchService.updateDocument(site, id, tmpFile, additionalFields);

        FileUtils.forceDelete(tmpFile);

        return result;
    } catch (IOException e) {
        throw new SearchException(e);
    }
}

From source file:org.craftercms.search.controller.SearchRestController.java

@RequestMapping(value = URL_PARTIAL_DOCUMENT_UPDATE, method = RequestMethod.POST)
@ResponseBody//from  ww w . ja  va2  s.  c  o m
public String partialDocumentUpdate(@RequestPart(REQUEST_PARAM_SITE) String site,
        @RequestPart(REQUEST_PARAM_ID) String id, @RequestPart(REQUEST_PARAM_DOCUMENT) MultipartFile document,
        HttpServletRequest request) throws SearchException {

    try {
        File tmpFile = File.createTempFile("crafter" + document.getOriginalFilename(), "");
        Map<String, String> additionalFields = getAdditionalFields(request);

        document.transferTo(tmpFile);

        String result = searchService.updateDocument(site, id, tmpFile, additionalFields);

        FileUtils.forceDelete(tmpFile);

        return result;
    } catch (IOException e) {
        throw new SearchException(e);
    }
}

From source file:org.openlmis.fulfillment.service.TemplateServiceTest.java

@Test
public void shouldValidateFileAndSetData() throws Exception {
    MultipartFile file = mock(MultipartFile.class);
    when(file.getOriginalFilename()).thenReturn(NAME_OF_FILE);

    mockStatic(JasperCompileManager.class);
    JasperReport report = mock(JasperReport.class);
    InputStream inputStream = mock(InputStream.class);
    when(file.getInputStream()).thenReturn(inputStream);

    JRParameter param1 = mock(JRParameter.class);
    JRParameter param2 = mock(JRParameter.class);
    JRPropertiesMap propertiesMap = mock(JRPropertiesMap.class);
    JRExpression jrExpression = mock(JRExpression.class);

    String[] propertyNames = { DISPLAY_NAME };
    when(report.getParameters()).thenReturn(new JRParameter[] { param1, param2 });
    when(JasperCompileManager.compileReport(inputStream)).thenReturn(report);
    when(propertiesMap.getPropertyNames()).thenReturn(propertyNames);
    when(propertiesMap.getProperty(DISPLAY_NAME)).thenReturn(PARAM_DISPLAY_NAME);

    when(param1.getPropertiesMap()).thenReturn(propertiesMap);
    when(param1.getValueClassName()).thenReturn("String");
    when(param1.getName()).thenReturn("name");
    when(param1.getDescription()).thenReturn("desc");
    when(param1.getDefaultValueExpression()).thenReturn(jrExpression);
    when(jrExpression.getText()).thenReturn("text");

    when(param2.getPropertiesMap()).thenReturn(propertiesMap);
    when(param2.getValueClassName()).thenReturn("Integer");
    when(param2.getName()).thenReturn("name");
    when(param2.getDescription()).thenReturn("desc");
    when(param2.getDefaultValueExpression()).thenReturn(jrExpression);

    ByteArrayOutputStream byteOutputStream = mock(ByteArrayOutputStream.class);
    whenNew(ByteArrayOutputStream.class).withAnyArguments().thenReturn(byteOutputStream);
    ObjectOutputStream objectOutputStream = spy(new ObjectOutputStream(byteOutputStream));
    whenNew(ObjectOutputStream.class).withArguments(byteOutputStream).thenReturn(objectOutputStream);
    doNothing().when(objectOutputStream).writeObject(report);
    byte[] byteData = new byte[1];
    when(byteOutputStream.toByteArray()).thenReturn(byteData);
    Template template = new Template();

    templateService.validateFileAndInsertTemplate(template, file);

    verify(templateRepository).save(template);

    assertThat(template.getTemplateParameters().get(0).getDisplayName(), is(PARAM_DISPLAY_NAME));
    assertThat(template.getTemplateParameters().get(0).getDescription(), is("desc"));
    assertThat(template.getTemplateParameters().get(0).getName(), is("name"));
}

From source file:com.gothiaforum.validator.actorsform.ImageValidatorTest.java

/**
 * Test watch should work for jpg file/*  www  .ja va  2 s  . c o m*/
 * 
 * @throws Exception
 *             the exception
 */
@Test
public void test2() throws Exception {

    ImageValidator imageValidator = new ImageValidator();

    List<String> errors = new ArrayList<String>();
    MultipartFile multipartFile = Mockito.mock(MultipartFile.class);

    Mockito.when(multipartFile.getSize()).thenReturn((long) (1 * 1024 * 1024));
    Mockito.when(multipartFile.getOriginalFilename()).thenReturn("my-image.jpg");
    Mockito.when(multipartFile.getContentType()).thenReturn("image/jpeg");

    imageValidator.validate(multipartFile, errors);

    assertEquals(0, errors.size());

}

From source file:com.gothiaforum.validator.actorsform.ImageValidatorTest.java

/**
 * Test watch should work for png file.// w w  w . j a  v a  2s  .com
 * 
 * @throws Exception
 *             the exception
 */
@Test
public void test3() throws Exception {

    ImageValidator imageValidator = new ImageValidator();

    List<String> errors = new ArrayList<String>();
    MultipartFile multipartFile = Mockito.mock(MultipartFile.class);

    Mockito.when(multipartFile.getSize()).thenReturn((long) (1 * 1024 * 1024));
    Mockito.when(multipartFile.getOriginalFilename()).thenReturn("my-image.png");
    Mockito.when(multipartFile.getContentType()).thenReturn("image/png");

    imageValidator.validate(multipartFile, errors);

    assertEquals(0, errors.size());

}

From source file:com.gothiaforum.validator.actorsform.ImageValidatorTest.java

/**
 * Test watch should work for gif file./*from ww  w  .  ja  v  a  2 s. c o  m*/
 * 
 * @throws Exception
 *             the exception
 */
@Test
public void test4() throws Exception {

    ImageValidator imageValidator = new ImageValidator();

    List<String> errors = new ArrayList<String>();
    MultipartFile multipartFile = Mockito.mock(MultipartFile.class);

    Mockito.when(multipartFile.getSize()).thenReturn((long) (1 * 1024 * 1024));
    Mockito.when(multipartFile.getOriginalFilename()).thenReturn("my-image.gif");
    Mockito.when(multipartFile.getContentType()).thenReturn("image/gif");

    imageValidator.validate(multipartFile, errors);

    assertEquals(0, errors.size());

}

From source file:com.gothiaforum.validator.actorsform.ImageValidatorTest.java

/**
 * Test watch should work for x-png file
 * /*from w ww  .j ava2s  .  co m*/
 * @throws Exception
 *             the exception
 */
@Test
public void test5() throws Exception {

    ImageValidator imageValidator = new ImageValidator();

    List<String> errors = new ArrayList<String>();
    MultipartFile multipartFile = Mockito.mock(MultipartFile.class);

    Mockito.when(multipartFile.getSize()).thenReturn((long) (1 * 1024 * 1024));
    Mockito.when(multipartFile.getOriginalFilename()).thenReturn("my-image.png");
    Mockito.when(multipartFile.getContentType()).thenReturn("image/x-png");

    imageValidator.validate(multipartFile, errors);

    assertEquals(0, errors.size());

}