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

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

Introduction

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

Prototype

default void transferTo(Path dest) throws IOException, IllegalStateException 

Source Link

Document

Transfer the received file to the given destination file.

Usage

From source file:com.bookselling.util.FileUtilities.java

public static File multipartToFile(MultipartFile multipart) {
    try {/*from ww w  .  j ava 2s .  c  o  m*/
        File convFile = new File(multipart.getOriginalFilename());
        multipart.transferTo(convFile);
        return convFile;
    } catch (Exception ex) {
        return null;
    }
}

From source file:il.co.brandis.utils.FileUploaderUtil.java

public static void saveFile(MultipartFile file, String saveDir) throws IllegalStateException, IOException {

    String fileName = file.getOriginalFilename();

    System.out.println("Saving file: " + file.getOriginalFilename());

    if (!fileName.equals("")) {
        file.transferTo(new File(saveDir + fileName));
    }/*  ww w  .ja  v a 2s  .co m*/

}

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

/**
 * // ww w  .j  av  a2  s .c  o  m
 * 
 * @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:com.glaf.core.util.UploadUtils.java

/**
 * /*  w w  w  .j  a va 2  s .c om*/
 * 
 * @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  ww w  .  j  a  v a  2s.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:cn.guoyukun.spring.web.upload.FileUploadUtils.java

/**
 * //from  w  ww. j a v a  2s .  com
 *
 * @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:eionet.transfer.dao.StorageServiceFiles.java

@Override
public void save(MultipartFile myFile, String uuidName) throws IOException {
    assert storageDir != null;
    File destination = new File(storageDir, uuidName);
    myFile.transferTo(destination);
}

From source file:sample.SpringMultipartParser.java

public void parse(HttpServletRequest request, String saveDir) throws IOException {
    multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);

    if (multipartRequest == null) {
        LOG.warn("Unable to MultipartHttpServletRequest");
        errors.add("Unable to MultipartHttpServletRequest");
        return;//from  w w  w. ja  va 2  s. c  o m
    }
    multipartMap = multipartRequest.getMultiFileMap();
    for (Entry<String, List<MultipartFile>> fileEntry : multipartMap.entrySet()) {
        String fieldName = fileEntry.getKey();
        for (MultipartFile file : fileEntry.getValue()) {
            File temp = File.createTempFile("upload", ".dat");
            file.transferTo(temp);
            multiFileMap.add(fieldName, temp);
        }
    }
}

From source file:bookpub.util.FileIO.java

public void save(File file, MultipartFile uploadedFile) {
    String fileId = file.getId().toString();
    java.io.File diskFile = new java.io.File(fileDir, fileId);

    try {/*from www  .ja v  a  2s.  c o m*/
        uploadedFile.transferTo(diskFile);

    } catch (Exception e) {
        logger.error("Failed to save uploaded file", e);
    }
}

From source file:csns.util.FileIO.java

public void save(File file, MultipartFile uploadedFile) {
    java.io.File diskFile = getDiskFile(file, false);
    try {//from ww w  .j  av a 2  s  .co m
        uploadedFile.transferTo(diskFile);
    } catch (Exception e) {
        logger.error("Failed to save uploaded file", e);
    }
}