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:cn.guoyukun.spring.web.upload.FileUploadUtils.java

/**
 * ??//  w ww. ja va2s .  c o  m
 *
 * @param file             
 * @param allowedExtension   null ?
 * @param maxSize          ? ?? -1??
 * @return
 * @throws InvalidExtensionException      MIME??
 * @throws FileSizeLimitExceededException ?
 */
public static final void assertAllowed(MultipartFile file, String[] allowedExtension, long maxSize)
        throws InvalidExtensionException, FileSizeLimitExceededException {

    String filename = file.getOriginalFilename();
    String extension = FilenameUtils.getExtension(file.getOriginalFilename());

    if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension)) {
        if (allowedExtension == IMAGE_EXTENSION) {
            throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
                    filename);
        } else if (allowedExtension == FLASH_EXTENSION) {
            throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
                    filename);
        } else if (allowedExtension == MEDIA_EXTENSION) {
            throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
                    filename);
        } else {
            throw new InvalidExtensionException(allowedExtension, extension, filename);
        }
    }

    long size = file.getSize();
    if (maxSize != -1 && size > maxSize) {
        throw new FileSizeLimitExceededException("not allowed upload upload", size, maxSize);
    }
}

From source file:com.glaf.core.util.UploadUtils.java

/**
 * //from w  w w.j  a v a  2  s.  c  om
 * 
 * @param request
 * @param fileParam
 * @param uploadDir
 *            ?
 * @return 
 */
public static String upload(HttpServletRequest request, String fileParam, String uploadDir) {
    MultipartFile mFile = getMultipartFile(request, fileParam);
    String filePath = "";
    try {
        String pathDir = mkdirs(getUploadAbsolutePath(uploadDir));
        if (!mFile.isEmpty()) {
            String fileName = mFile.getOriginalFilename();
            String saveName = rename(fileName);
            mFile.transferTo(new File(getUploadAbsolutePath(uploadDir) + pathDir + saveName));
            filePath = getUploadRelativePath(uploadDir) + pathDir + saveName;
        }
    } catch (Exception ex) {
        logger.error(ex.getMessage());
        ex.printStackTrace();
    }
    return filePath;
}

From source file:de.ingrid.iplug.excel.service.SheetsService.java

/**
 * Create sheets./*from   w  w w . j a  v  a 2 s. c om*/
 * 
 * @param multipartFile
 * @return Created sheets.
 * @throws IOException
 */
public static Sheets createSheets(final MultipartFile multipartFile) throws IOException {
    final byte[] bytes = multipartFile.getBytes();
    final Sheets sheets = createSheets(new ByteArrayInputStream(bytes));
    for (final Sheet sheet : sheets) {
        sheet.setFileName(multipartFile.getOriginalFilename());
        sheet.setWorkbook(bytes);
    }
    return sheets;
}

From source file:cn.guoyukun.spring.web.upload.FileUploadUtils.java

/**
 * // ww  w.j a  v  a2 s . co  m
 *
 * @param request          ? ??? 
 * @param baseDir          
 * @param file             
 * @param allowedExtension ? null ?
 * @param maxSize          ? -1 ??
 *@param needDatePathAndRandomName ??????
 * @return ???
 * @throws InvalidExtensionException      MIME??
 * @throws FileSizeLimitExceededException ?
 * @throws FileNameLengthLimitExceededException
 *                                        ??
 * @throws IOException                    
 */
public static final String upload(HttpServletRequest request, String baseDir, MultipartFile file,
        String[] allowedExtension, long maxSize, boolean needDatePathAndRandomName)
        throws InvalidExtensionException, FileSizeLimitExceededException, IOException,
        FileNameLengthLimitExceededException {

    int fileNamelength = file.getOriginalFilename().length();
    if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) {
        throw new FileNameLengthLimitExceededException(file.getOriginalFilename(), fileNamelength,
                FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
    }

    assertAllowed(file, allowedExtension, maxSize);
    String filename = extractFilename(file, baseDir, needDatePathAndRandomName);

    File desc = getAbsoluteFile(extractUploadDir(request), filename);

    file.transferTo(desc);
    return filename;
}

From source file:org.shareok.data.documentProcessor.DocumentProcessorUtil.java

public static String saveMultipartFileByTimePath(MultipartFile file, String uploadPath) {
    String uploadedFilePath = null;
    try {//from ww  w  .  j av a  2 s .c  om
        String oldFileName = file.getOriginalFilename();
        String extension = DocumentProcessorUtil.getFileExtension(oldFileName);
        oldFileName = DocumentProcessorUtil.getFileNameWithoutExtension(oldFileName);
        //In the future the new file name will also has the user name
        String time = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
        String newFileName = oldFileName + "." + extension;

        if (null != uploadPath) {
            File uploadFolder = new File(uploadPath);
            if (!uploadFolder.exists()) {
                uploadFolder.mkdir();
            }
            File uploadTimeFolder = new File(uploadPath + File.separator + time);
            if (!uploadTimeFolder.exists()) {
                uploadTimeFolder.mkdir();
            }
        }
        uploadedFilePath = uploadPath + File.separator + time + File.separator + newFileName;
        File uploadedFile = new File(uploadedFilePath);
        file.transferTo(uploadedFile);
    } catch (Exception ex) {
        Logger.getLogger(DocumentProcessorUtil.class.getName()).log(Level.SEVERE, null, ex);
    }
    return uploadedFilePath;
}

From source file:com.eryansky.core.web.upload.FileUploadUtils.java

/**
 * ?????//from ww w  .  j  a v  a 2 s.c om
 * @param file
 * @param baseDir
 * @param needDatePathAndRandomName
 * @param _prefix
 * @return
 * @throws java.io.UnsupportedEncodingException
 */
public static final String extractFilename(MultipartFile file, String baseDir,
        boolean needDatePathAndRandomName, String _prefix) throws UnsupportedEncodingException {
    String fileAllName = file.getOriginalFilename();

    return extractFilename(fileAllName, baseDir, needDatePathAndRandomName, _prefix);
}

From source file:com.glaf.core.util.UploadUtils.java

/**
 * /*  ww w  . j a  v a2 s.co  m*/
 * 
 * @param request
 * @param fileParam
 * @param uploadDir
 *            ?
 * @return 
 */
public static String upload(HttpServletRequest request, String fileParam, String uploadDir,
        String fileNameParam) {
    MultipartFile mFile = getMultipartFile(request, fileParam);
    String filePath = "";
    if (StringUtils.isEmpty(uploadDir)) {
        uploadDir = UPLOAD_DIR;
    }
    try {
        FileUtils.mkdirs(getUploadAbsolutePath(uploadDir));
        if (!mFile.isEmpty()) {
            String fileName = mFile.getOriginalFilename();
            String saveName = "";
            if (StringUtils.isEmpty(fileNameParam)) {
                saveName = fileNameParam + getSuffix(fileName);
            } else {
                saveName = rename(fileName);
            }
            mFile.transferTo(new File(getUploadAbsolutePath(uploadDir) + saveName));
            filePath = uploadDir + saveName;
        }
    } catch (Exception ex) {
        logger.error(ex.getMessage());
        ex.printStackTrace();
    }
    return filePath;
}

From source file:org.shareok.data.kernel.api.services.ServiceUtil.java

public static String saveUploadedFile(MultipartFile file, String jobFilePath) {
    String uploadedFilePath = null;
    try {/*from  w  w w  . jav  a2 s . c  o m*/
        String fileName = file.getOriginalFilename();
        File path = new File(jobFilePath);
        if (!path.exists()) {
            path.mkdir();
        }
        uploadedFilePath = jobFilePath + File.separator + fileName;
        File uploadedFile = new File(uploadedFilePath);
        file.transferTo(uploadedFile);
    } catch (IOException | IllegalStateException ex) {
        logger.error("Cannot save the uploaded file", ex);
    }
    return uploadedFilePath;
}

From source file:com.eryansky.core.web.upload.FileUploadUtils.java

/**
 * /*from ww  w .  j  av a  2 s  . com*/
 *
 * @param request                   ? ??? 
 * @param dir                   request?,?;,??
 * @param file                      
 * @param allowedExtension          ? null ?
 * @param maxSize                   ? -1 ??
 * @param needDatePathAndRandomName ??????
 * @param _prefix ??? needDatePathAndRandomNamefalse
 * @return ???
 * @throws com.eryansky.core.web.upload.exception.InvalidExtensionException            MIME??
 * @throws org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException       ?
 * @throws com.eryansky.core.web.upload.exception.FileNameLengthLimitExceededException ??
 * @throws java.io.IOException                          
 */
public static final String upload(HttpServletRequest request, String dir, MultipartFile file,
        String[] allowedExtension, long maxSize, boolean needDatePathAndRandomName, String _prefix)
        throws InvalidExtensionException, FileSizeLimitExceededException, IOException,
        FileNameLengthLimitExceededException {

    int fileNamelength = file.getOriginalFilename().length();
    if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) {
        throw new FileNameLengthLimitExceededException(file.getOriginalFilename(), fileNamelength,
                FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
    }
    File desc = null;
    String filename = null;
    assertAllowed(file, allowedExtension, maxSize);
    if (request != null) {
        filename = extractFilename(file, dir, needDatePathAndRandomName, _prefix);
        desc = getAbsoluteFile(extractUploadDir(request), filename);
    } else {
        filename = extractFilename(file, dir, needDatePathAndRandomName, _prefix);
        String fileBasePath = getBasePath(filename);
        desc = getAbsoluteFile(fileBasePath);
    }

    file.transferTo(desc);
    return filename;
}

From source file:com.ylife.goods.model.UploadUtil.java

/**
 * ????,SpringMVC/*ww  w  . ja v a  2  s  .  co m*/
 * 
 * @param muFile
 *            MultipartFile
 * @return ??
 */
private static boolean checkFileForSpringUpload(MultipartFile muFile) {
    boolean bool = true;
    // ?
    if (muFile.getSize() > maxSize) {
        //LOGGER.error("=============>" + muFile.getOriginalFilename() + "??");
        bool = false;
    }
    String fileName = muFile.getOriginalFilename();
    // ??
    String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
    if (!Arrays.<String>asList(extMap.get(IMAGE).split(",")).contains(fileExt)) {
        //LOGGER.error("" + muFile.getOriginalFilename() + "??????\n??" + extMap.get(IMAGE) + "?");
        bool = false;
    }
    return bool;
}