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.openmeetings.servlet.outputhandler.AbstractUploadController.java

protected UploadInfo validate(HttpServletRequest request, boolean admin) throws ServletException {
    UploadInfo info = new UploadInfo();
    log.debug("Starting validate");
    try {/*from  w  ww.j a va 2  s. co m*/
        String sid = request.getParameter("sid");
        if (sid == null) {
            throw new ServletException("SID Missing");
        }
        info.sid = sid;
        log.debug("sid: " + sid);

        Long userId = sessionManagement.checkSession(sid);
        Long userLevel = userManagement.getUserLevelByID(userId);
        log.debug("userId = " + userId + ", userLevel = " + userLevel);
        info.userId = userId;

        if ((admin && !authLevelManagement.checkAdminLevel(userLevel)) || (!admin && userLevel <= 0)) {
            throw new ServletException("Insufficient permissions " + userLevel);
        }

        String publicSID = request.getParameter("publicSID");
        if (publicSID == null) {
            // Always ask for Public SID
            throw new ServletException("Missing publicSID");
        }
        log.debug("publicSID: " + publicSID);
        info.publicSID = publicSID;

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartRequest.getFile("Filedata");
        //FIXME encoding HACK
        info.filename = new String(multipartFile.getOriginalFilename().getBytes("iso-8859-1"), "UTF-8");
        long fileSize = multipartFile.getSize();
        long maxSize = ImportHelper.getMaxUploadSize(cfgManagement);
        log.debug("uploading " + fileSize + " bytes");
        if (fileSize > maxSize) {
            throw new ServletException("Maximum upload size: " + maxSize + " exceeded: " + fileSize);
        }
        info.file = multipartFile;
    } catch (ServletException e) {
        throw e;
    } catch (Exception e) {
        log.error("Exception during upload: ", e);
        throw new ServletException(e);
    }
    return info;
}

From source file:com.epam.catgenome.controller.AbstractRESTController.java

/**
 * Transfers content of the given {@code Multipart} entity to a temporary file.
 *
 * @param multipart {@code Multipart} used to handle file uploads
 * @return {@code File} represents a reference on a file that has been created
 * from the given {@code Multipart}/*from w w  w  . j a  v  a  2s. com*/
 * @throws IOException
 */
protected File transferToTempFile(final MultipartFile multipart) throws IOException {
    Assert.notNull(multipart);
    final File tmp = File.createTempFile(UUID.randomUUID().toString(), multipart.getOriginalFilename(),
            fileManager.getTempDir());
    multipart.transferTo(tmp);
    FileUtils.forceDeleteOnExit(tmp);
    return tmp;
}

From source file:io.restassured.examples.springmvc.controller.FileUploadController.java

@RequestMapping(value = "/fileUploadWithControlNameEqualToSomething", method = POST, consumes = MULTIPART_FORM_DATA_VALUE, produces = APPLICATION_JSON_VALUE)
public @ResponseBody String fileUploadWithControlNameEqualToSomething(
        @RequestParam(value = "something") MultipartFile file) {
    return "{ \"size\" : " + file.getSize() + ", \"name\" : \"" + file.getName() + "\", \"originalName\" : \""
            + file.getOriginalFilename() + "\", \"mimeType\" : \"" + file.getContentType() + "\" }";
}

From source file:gov.nih.nci.ncicb.tcga.dcc.datareports.web.SubmissionReportController.java

@RequestMapping(value = SubmissionReportConstants.SUBMISSION_REPORT_URL, method = RequestMethod.POST)
public String handleSubmission(final ModelMap model, final Submission submission, final BindingResult result) {
    try {//from  ww  w. j  a v  a  2 s. co m
        if (result.hasErrors()) {
            for (ObjectError error : (List<ObjectError>) result.getAllErrors()) {
                logger.info("Error: " + error.getCode() + " - " + error.getDefaultMessage());
            }
            model.addAttribute("response", buildManualJsonResponse(false, "Upload failed"));
        } else {
            final MultipartFile file = submission.getFileData();
            parseUploadedFile(file.getInputStream());
            model.addAttribute("response", buildManualJsonResponse(true, file.getOriginalFilename()));
        }
    } catch (IOException iox) {
        logger.info(iox.getMessage());
    }
    return SubmissionReportConstants.SUBMISSION_REPORT_VIEW;
}

From source file:org.wso2.security.tools.am.webapp.service.StaticScannerService.java

private void validateRequest(boolean sourceCodeUploadAsZip, MultipartFile zipFile, String gitUrl)
        throws AutomationManagerWebException {
    if (sourceCodeUploadAsZip) {
        if (zipFile == null || !zipFile.getOriginalFilename().endsWith(".zip")) {
            throw new AutomationManagerWebException("Zip file required");
        }/*from w w  w .ja  v a 2s.c o  m*/
    } else {
        if (gitUrl == null) {
            throw new AutomationManagerWebException("Please enter a URL to clone");
        }
    }
}

From source file:net.shopxx.controller.shop.PaymentController.java

/**
 * //from  w w  w. j  a v  a 2  s. c om
 */
@RequestMapping(value = "/upload", method = RequestMethod.POST, produces = "text/html; charset=UTF-8")
public @ResponseBody String upload(HttpServletRequest request, HttpServletResponse response) {
    Map<String, Object> result = new HashMap<String, Object>();
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
    for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
        MultipartFile mf = entity.getValue();
        String fileName = mf.getOriginalFilename();
        try {
            String fullPath = fileService.upload(FileType.image, mf, false);
            Setting setting = SettingUtils.get();
            String domain = setting.getSiteUrl();
            String path = fullPath.substring(domain.length());

            result.put("path", path);
            result.put("fullPath", fullPath);
            result.put("fileName", fileName);

            result.put("rtnKey", "1");
            result.put("rtnMsg", "??");
        } catch (Exception e) {
            result.put("rtnKey", "0");
            result.put("rtnMsg", e.toString());
        }
    }
    return JSONObject.toJSON(result).toString();
}

From source file:com.mycompany.rent.controllers.ListingController.java

@RequestMapping(value = "/savefiles", method = RequestMethod.POST)
public String crunchifySave(@ModelAttribute("uploadForm") UploadForm uploadForm)
        throws IllegalStateException, IOException {

    String saveDirectory = "/home/brennan/_repos/rent/src/main/webapp/uploads/";

    List<MultipartFile> crunchifyFiles = uploadForm.getFiles();

    List<String> fileNames = new ArrayList<String>();

    ForRent fr = forRentDao.get(uploadForm.getProp_id());

    if (null != crunchifyFiles && crunchifyFiles.size() > 0) {
        for (MultipartFile multipartFile : crunchifyFiles) {

            String fileName = saveDirectory + uploadForm.getProp_id() + multipartFile.getOriginalFilename();
            if (!"".equalsIgnoreCase(fileName)) {
                // Handle file content - multipartFile.getInputStream()
                multipartFile.transferTo(new File(fileName));
                fileNames.add("/rent/uploads/" + uploadForm.getProp_id() + multipartFile.getOriginalFilename());
            }/*from w w  w  .  j a  v  a2  s.  c o m*/
        }
    }

    fr.setImagePaths(fileNames);

    //Iterate through ForRent imagePath and add each path to db
    for (String s : fr.getImagePaths()) {
        forRentDao.addPhotos(fr.getId(), s);
    }

    //        map.addAttribute("files", fileNames);
    return "pricing";
}

From source file:com.opendesign.utils.CmnUtil.java

/**
 * ?  //  www  .  j ava 2 s .c  o m
 * 
 * @param request
 * @param fileParamName
 * @param subDomain
 * @return dbPath
 * @throws IOException
 */
public static List<UpFileInfo> handleMultiFileUpload(MultipartHttpServletRequest request, String fileParamName,
        String subDomain) throws IOException {
    List<MultipartFile> reqFileList = request.getFiles(fileParamName);
    List<UpFileInfo> resultList = new ArrayList<UpFileInfo>();
    for (MultipartFile reqFile : reqFileList) {
        UpFileInfo upInfo = new UpFileInfo();
        if (reqFile != null) {
            String fileUploadDir = CmnUtil.getFileUploadDir(request, subDomain);
            String saveFileName = UUID.randomUUID().toString();
            File file = CmnUtil.saveFile(reqFile, fileUploadDir, saveFileName);
            String fileUploadDbPath = CmnUtil.getFileUploadDbPath(request, file);
            upInfo.setUpfile(file);
            upInfo.setDbPath(fileUploadDbPath);
            upInfo.setFilename(reqFile.getOriginalFilename());
        }
        resultList.add(upInfo);
    }
    return resultList;
}

From source file:org.apache.ambari.view.registry.web.controller.ApplicationController.java

@PostMapping
@ResponseBody/* w  w w. j  a v  a2  s  . co m*/
public ResponseEntity addApplication(@RequestParam("file") MultipartFile file) {
    ApplicationConfig config = null;
    try {
        config = service.parseApplicationConfig(file.getInputStream(), file.getOriginalFilename());
    } catch (IOException e) {
        log.error("Failed to get the stream from multipart file", e);
        throw new ConfigFileParseException(file.getOriginalFilename(), e);
    }

    ApplicationVersion version = service.saveApplicationConfig(config);
    Map<String, Object> map = new LinkedHashMap<>();
    Map<String, Object> map1 = new LinkedHashMap<>();
    map1.put("id", version.getApplication().getId());
    map1.put("name", version.getApplication().getName());
    map.put("id", version.getId());
    map.put("version", version.getVersion());
    map.put("application", map1);
    return ResponseEntity.ok(map);
}

From source file:controller.UploadFileController.java

@RequestMapping(method = RequestMethod.POST)
public @ResponseBody String upload(@RequestParam(value = "file") MultipartFile mfile, HttpServletResponse res,
        HttpServletRequest req) {//from w w w  .java2s . c om
    long time = Calendar.getInstance().getTimeInMillis();
    String fileName = "";
    try {
        fileName = time + mfile.getOriginalFilename();
        File f = new File(req.getServletContext().getRealPath("/images") + "/" + fileName);
        if (!f.exists()) {
            f.createNewFile();
        }
        FileOutputStream out = new FileOutputStream(f);
        FileCopyUtils.copy(mfile.getBytes(), out);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return "<img src='" + req.getContextPath() + "/images/" + fileName + "'/>";

}