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:org.chos.fs.FileStorage.java

public String write(MultipartFile file, File dest) {
    if (dest.exists()) {
        throw new IllegalArgumentException("The file to write is already exists");
    }/* w  w w.  ja  va  2s  .c o m*/
    try {
        file.transferTo(dest);
        return dest.getName();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.lixiaocong.rest.FileController.java

@RolesAllowed("ROLE_USER")
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String post(MultipartFile imageFile) {
    try {/*from  w  w  w.  j a va2  s  .  co m*/
        File newFile = new File(imageFolder + UUID.randomUUID() + imageFile.getOriginalFilename());
        imageFile.transferTo(newFile);
        return imageServer + newFile.getName();
    } catch (Exception e) {
        logger.error(e);
    }
    return imageServer + "error.jpg";
}

From source file:ch.systemsx.cisd.openbis.generic.client.web.server.UploadedFilesBean.java

public final void addMultipartFile(final MultipartFile multipartFile) {
    assert multipartFile != null : "Unspecified multipart file.";
    try {/*from w w  w  .  j ava2s.  c  o  m*/
        final File tempFile = createTempFile();
        multipartFile.transferTo(tempFile);
        final FileMultipartFileAdapter multipartFileAdapter = new FileMultipartFileAdapter(multipartFile,
                tempFile);
        multipartFiles.add(multipartFileAdapter);
    } catch (final IOException ex) {
        throw new IOExceptionUnchecked(ex);
    }
}

From source file:io.pivotal.PaasappApplication.java

@RequestMapping("upload")
public String uploadFiles(HttpServletRequest request) throws IllegalStateException, IOException {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
            request.getSession().getServletContext());
    if (multipartResolver.isMultipart(request)) {
        MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
        //?multiRequest ??
        Iterator iter = multiRequest.getFileNames();

        while (iter.hasNext()) {
            //??/*from w  w w  . ja  va 2 s . c om*/
            MultipartFile file = multiRequest.getFile(iter.next().toString());
            if (file != null) {
                String path = file.getOriginalFilename();
                //
                file.transferTo(new File(path));
            }

        }
    }

    return "/success";
}

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

/**
 * ? //w w  w . j a  v  a2s  .c  om
 * 
 * @param multipartFile
 * @param fileUploadDir
 * @param saveFileName
 * @return
 * @throws IllegalStateException
 * @throws IOException
 */
public static File saveFile(MultipartFile multipartFile, String fileUploadDir, String saveFileName)
        throws IllegalStateException, IOException {
    String originalFilename = multipartFile.getOriginalFilename();
    String ext = FilenameUtils.getExtension(originalFilename);
    saveFileName = saveFileName + "." + ext;

    // System.out.println(saveFileName);
    // System.out.println(fileUploadDir);

    File directory = new File(fileUploadDir);
    if (!directory.exists()) {
        directory.mkdirs();
    }

    File file = new File(directory, saveFileName);
    multipartFile.transferTo(file);

    return file;
}

From source file:com.vermeg.convertisseur.service.ConvServiceImpl.java

@Override
public List<String> getSheets(MultipartFile file)
        throws FileNotFoundException, InvalidFormatException, IOException {
    xlsxFile = File.createTempFile("fichier", "xslx");
    file.transferTo(xlsxFile);
    FileInputStream inp = new FileInputStream(xlsxFile);
    Workbook workbook = WorkbookFactory.create(inp);
    List<String> list = new ArrayList<>();
    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
        list.add(workbook.getSheetName(i));
    }/*from  www  .  j  a v  a 2 s.c  o m*/
    return list;
}

From source file:com.kalai.controller.FileUploadController.java

@RequestMapping(value = "/savefiles", method = RequestMethod.POST)
public String fileupload(@ModelAttribute("uploadForm") filesupload fileup, Model map)
        throws IllegalStateException, IOException {
    String saveDirectory = "D:/spring_file_upload/";
    List<MultipartFile> files = fileup.getFiles();
    List<String> filenames = new ArrayList<String>();
    try {/*from ww  w. j  av a 2 s. c om*/
        if (null != files && !files.isEmpty()) {
            for (MultipartFile multipartfile : files) {
                String fileName = multipartfile.getOriginalFilename();
                if (!"".equalsIgnoreCase(fileName)) {
                    multipartfile.transferTo(new File(saveDirectory + fileName));
                    filenames.add(fileName);
                }
            }
            map.addAttribute("uploadoption", "uploaded");
            map.addAttribute("files", filenames);
        } else {
            map.addAttribute("uploadoption", "emptyfiles");
            map.addAttribute("files", filenames);
        }

    } catch (Exception e) {
        return e.getMessage();
    }
    return "fileupload";
}

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 {/*from   ww w .  j a  va2 s  . c  o  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: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  ww .j  a  va 2  s.  c  om
 * @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:org.openmrs.module.filemanager.api.impl.FileManagerServiceImpl.java

public File multipartToFile(MultipartFile multipart) throws IllegalStateException, IOException {
    File tmpFile = new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator")
            + multipart.getOriginalFilename());
    multipart.transferTo(tmpFile);
    return tmpFile;
}