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.jahia.modules.webflow.showcase.JobApplication.java

private UploadedFile copy(MultipartFile multipartFile) throws IOException {
    UploadedFile uploaded = null;//from w  w  w  .j  a v  a2s . c o  m
    if (multipartFile != null && !multipartFile.isEmpty()) {
        String originalFilename = multipartFile.getOriginalFilename();
        File tmp = File.createTempFile(FilenameUtils.getBaseName(originalFilename),
                "." + FilenameUtils.getExtension(originalFilename));
        multipartFile.transferTo(tmp);
        uploaded = new UploadedFile(originalFilename, multipartFile.getContentType(), tmp);
    }

    return uploaded;
}

From source file:com.funtl.framework.smoke.core.modules.act.service.ActProcessService.java

/**
 * ? - ?//w w w  .  j ava2  s. c  o m
 *
 * @param file
 * @return
 */
@Transactional(readOnly = false)
public String deploy(String exportDir, String category, MultipartFile file) {

    String message = "";

    String fileName = file.getOriginalFilename();

    try {
        InputStream fileInputStream = file.getInputStream();
        Deployment deployment = null;
        String extension = FilenameUtils.getExtension(fileName);
        if (extension.equals("zip") || extension.equals("bar")) {
            ZipInputStream zip = new ZipInputStream(fileInputStream);
            deployment = repositoryService.createDeployment().addZipInputStream(zip).deploy();
        } else if (extension.equals("png")) {
            deployment = repositoryService.createDeployment().addInputStream(fileName, fileInputStream)
                    .deploy();
        } else if (fileName.indexOf("bpmn20.xml") != -1) {
            deployment = repositoryService.createDeployment().addInputStream(fileName, fileInputStream)
                    .deploy();
        } else if (extension.equals("bpmn")) { // bpmn????bpmn20.xml
            String baseName = FilenameUtils.getBaseName(fileName);
            deployment = repositoryService.createDeployment()
                    .addInputStream(baseName + ".bpmn20.xml", fileInputStream).deploy();
        } else {
            message = "??" + extension;
        }

        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deployment.getId()).list();

        // ?
        for (ProcessDefinition processDefinition : list) {
            //               ActUtils.exportDiagramToFile(repositoryService, processDefinition, exportDir);
            repositoryService.setProcessDefinitionCategory(processDefinition.getId(), category);
            message += "??ID=" + processDefinition.getId() + "<br/>";
        }

        if (list.size() == 0) {
            message = "?";
        }

    } catch (Exception e) {
        throw new ActivitiException("?", e);
    }
    return message;
}

From source file:com.mum.controller.ProductController.java

@RequestMapping(value = "products/add", method = RequestMethod.POST)
public String processAddNewProductForm(@ModelAttribute("newProduct") Product product, BindingResult result,
        HttpServletRequest request, HttpServletResponse response) {
    MultipartFile productImage = product.getProductImage();
    String rootDirectory = request.getSession().getServletContext().getRealPath("/../../../");

    if (productImage != null && !productImage.isEmpty()) {
        try {/* www  .j  a v  a2  s  .co m*/
            productImage.transferTo(
                    new File(rootDirectory + "\\images\\" + productImage.getOriginalFilename() + ".png"));
        } catch (Exception e) {
            throw new RuntimeException("Product Image saving failed", e);
        }
    }

    String[] suppressedFields = result.getSuppressedFields();
    if (suppressedFields.length > 0) {
        throw new RuntimeException("Attempting to bind disallowed fields:"
                + StringUtils.arrayToCommaDelimitedString(suppressedFields));
    }

    productService.addProduct(product);
    //return "redirect:/products/add";
    //return "redirect:/products";
    return "";
}

From source file:org.pdfgal.pdfgalweb.services.impl.UnProtectServiceImpl.java

@Override
public DownloadForm unProtect(final MultipartFile file, final String password,
        final HttpServletResponse response)
        throws COSVisitorException, IOException, BadSecurityHandlerException, CryptographyException {

    DownloadForm result = new DownloadForm();

    if (!file.isEmpty() && StringUtils.isNotBlank(password)) {
        final String originalName = file.getOriginalFilename();
        final String inputUri = this.fileUtils.saveFile(file);
        final String outputUri = this.fileUtils.getAutogeneratedName(originalName);

        try {/*w  ww .j a v a  2 s. c o  m*/
            // File is unprotected
            this.pdfGal.unProtect(inputUri, outputUri, password);
        } catch (COSVisitorException | IOException | BadSecurityHandlerException | CryptographyException e) {
            // Temporal files are deleted from system
            this.fileUtils.delete(inputUri);
            this.fileUtils.delete(outputUri);
            throw e;
        }

        // Temporal files are deleted from system
        this.fileUtils.delete(inputUri);

        result = new DownloadForm(outputUri, originalName);
    }

    return result;
}

From source file:de.tobiasbruns.content.storage.ContentController.java

private Content<InputStream> buildContent(MultipartFile file) {
    try {/*  w  w  w  .j  av  a2  s. c o  m*/
        Content<InputStream> content = new Content<>();
        content.setContent(file.getInputStream());
        content.getHeader().setContentType(file.getContentType());
        content.getHeader().setName(file.getOriginalFilename());
        return content;
    } catch (IOException e) {
        throw new RuntimeException("Error reading incoming file", e);
    }
}

From source file:org.freeeed.ep.web.controller.ProcessingController.java

public ModelAndView execute() {
    log.info("request received... ");
    if (!(request instanceof MultipartHttpServletRequest)) {
        valueStack.put("status", "error");

        return new ModelAndView(WebConstants.PROCESS_REQUEST_RESPONSE);
    }//from w ww.ja  v a  2s . c o  m

    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    MultipartFile file = multipartRequest.getFile("file");
    String taskId = multipartRequest.getParameter("taskId");

    ProcessingResult result = null;

    try {
        File uploadDirFile = new File(uploadDir);
        uploadDirFile.mkdirs();

        String fileName = uploadDir + File.separator + file.getOriginalFilename();
        File destination = new File(fileName);

        log.info("File transfer started... ");

        file.transferTo(destination);

        NSFTask task = new NSFTask(destination.getAbsolutePath(), workDir);
        result = processor.submitTask(taskId, task);
    } catch (Exception e) {
        log.error("Problem uploading file: ", e);
        result = new ProcessingResult();
        result.setStatus(ProcessingStatus.ERROR);
    }

    Gson gson = new Gson();
    String data = gson.toJson(result);

    valueStack.put("status", data);

    log.info("File submitted for processing!");

    return new ModelAndView(WebConstants.PROCESS_REQUEST_RESPONSE);
}

From source file:com.chinalbs.service.impl.FileServiceImpl.java

@Override
public String saveImage(MultipartFile[] multipartFile) {
    String webPath = null;/*from w  w  w .  j av a2  s  .c om*/
    if (multipartFile == null || multipartFile.length == 0) {
        return null;
    }
    try {
        for (MultipartFile multiFile : multipartFile) {
            if (multiFile.getSize() > ImageMaxSize) {
                continue;
            }
            String uuid = UUID.randomUUID().toString();
            String sourcePath = uploadPath + File.separator + "src_" + uuid + "."
                    + FilenameUtils.getExtension(multiFile.getOriginalFilename());
            //            webPath = uuid + "." + DEST_EXTENSION;
            webPath = File.separator + "src_" + uuid + "."
                    + FilenameUtils.getExtension(multiFile.getOriginalFilename());
            String storePath = uploadPath + File.separator + uuid + "." + DEST_EXTENSION;
            ;

            File tempFile = new File(System.getProperty("java.io.tmpdir") + File.separator + "upload_"
                    + UUID.randomUUID() + ".tmp");
            if (!tempFile.getParentFile().exists()) {
                tempFile.getParentFile().mkdirs();
            }

            multiFile.transferTo(tempFile);
            proccessImage(tempFile, sourcePath, storePath, licenseImageWidth, licenseImageHeight, true);
        }
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return webPath;
}

From source file:net.risesoft.soa.asf.web.controller.SystemController.java

@RequestMapping({ "uploadLicense.do" })
@ResponseBody/*from  w ww  . ja va 2s .c  om*/
public String uploadLicense(MultipartRequest multipartRequest) {
    if (!(this.bundleHelper.isDevMode())) {
        MultipartFile multipartFile = multipartRequest.getFile("licenseFile");

        File licenseFile = new File(
                System.getProperty("user.dir") + "/../license/" + multipartFile.getOriginalFilename());
        try {
            multipartFile.transferTo(licenseFile);
            InputStream istream = null;
            try {
                istream = new FileInputStream(licenseFile);
                if (!(this.checkLicense.check(istream)))
                    throw new RuntimeException("License .");
            } finally {
                IOUtils.closeQuietly(istream);
            }
            IOUtils.closeQuietly(istream);

            File licensePath = licenseFile.getParentFile();
            if (licensePath.isDirectory()) {
                File[] files = licensePath.listFiles();
                for (File f : files) {
                    if ((!(f.isFile())) || (f.equals(licenseFile)))
                        continue;
                    f.delete();
                }
            }

            if (!(this.checkLicense.refresh()))
                throw new RuntimeException("License .");
        } catch (Exception ex) {
            log.error(" License : " + ex.getMessage(), ex);
            if (licenseFile.exists()) {
                licenseFile.delete();
            }
            return "{success:false, msg:'" + ex.getMessage() + "'}";
        }
        return "{success:true, msg:' License ?.'}";
    }
    return "{success:false, msg:' License ???.'}";
}

From source file:com.jayway.restassured.examples.springmvc.controller.FileUploadController.java

@RequestMapping(value = "/fileUploadWithParam", method = POST, consumes = MULTIPART_FORM_DATA_VALUE, produces = APPLICATION_JSON_VALUE)
public @ResponseBody FileWithParam fileUploadWithParam(@RequestParam(value = "controlName") MultipartFile file,
        @RequestParam(value = "param", required = false) String param) throws IOException {
    FileDescription fd1 = new FileDescription();
    fd1.setContent(new String(file.getBytes()));
    fd1.setName(file.getName());/*from   ww  w  .  j  a v a2 s.  co m*/
    fd1.setMimeType(file.getContentType());
    fd1.setOriginalName(file.getOriginalFilename());
    fd1.setSize(file.getSize());

    FileWithParam fileWithParam = new FileWithParam();
    fileWithParam.setFile(fd1);
    fileWithParam.setParam(param);

    return fileWithParam;
}

From source file:com.jayway.restassured.examples.springmvc.controller.FileUploadController.java

@RequestMapping(value = "/multiFileUpload", method = POST, consumes = MULTIPART_FORM_DATA_VALUE, produces = APPLICATION_JSON_VALUE)
public @ResponseBody List<FileDescription> multiFileUpload(
        @RequestParam(value = "controlName1") MultipartFile file1,
        @RequestParam(value = "controlName2") MultipartFile file2) throws IOException {
    FileDescription fd1 = new FileDescription();
    fd1.setContent(new String(file1.getBytes()));
    fd1.setName(file1.getName());//from  www  . ja v a2 s  .  co m
    fd1.setMimeType(file1.getContentType());
    fd1.setOriginalName(file1.getOriginalFilename());
    fd1.setSize(file1.getSize());

    FileDescription fd2 = new FileDescription();
    fd2.setContent(new String(file2.getBytes()));
    fd2.setName(file2.getName());
    fd2.setMimeType(file2.getContentType());
    fd2.setOriginalName(file2.getOriginalFilename());
    fd2.setSize(file2.getSize());
    return asList(fd1, fd2);
}