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

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

Introduction

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

Prototype

@Override
InputStream getInputStream() throws IOException;

Source Link

Document

Return an InputStream to read the contents of the file from.

Usage

From source file:no.freecode.translator.web.AdminController.java

/**
 * Upload messages.//from  w  w  w  . j a  v  a  2s . c om
 * 
 * @throws IOException 
 */
@RequestMapping(method = RequestMethod.POST, value = "upload")
public @ResponseBody String upload(@RequestParam("file") MultipartFile file) throws IOException {

    if (!file.isEmpty()) {
        String filename = FilenameUtils.getName(file.getOriginalFilename());
        messageImporter.importData(file.getInputStream(), filename);
        return "Imported data from " + filename + "\n";

    } else {
        return "redirect:uploadFailure";
    }
}

From source file:org.kemri.wellcome.controller.ReportDefinitionController.java

@RequestMapping(value = Views.UPLOAD, method = RequestMethod.POST)
public @ResponseBody Map<String, ? extends Object> upload(@RequestParam("file") MultipartFile file, Model model)
        throws IOException {
    InputStream is = file.getInputStream();
    try {//from  ww w  . ja v  a 2 s .  c  o m
        service.unMarshallandSaveReportTemplates(is);
    } catch (Exception ex) {
        log.error(ex.getMessage());
        ex.printStackTrace();
    } finally {
        is.close();
    }
    log.info("User:" + service.getUsername() + " uploaded the report template :" + file.getOriginalFilename()
            + " on " + Calendar.getInstance().getTime());
    return Collections.singletonMap("u", "Saved");
}

From source file:de.uni_koeln.spinfo.maalr.services.admin.shared.AdminService.java

public void importDatabase(HttpServletRequest request) throws IOException, InvalidEntryException,
        NoDatabaseAvailableException, JAXBException, XMLStreamException {
    DefaultMultipartHttpServletRequest dmhsRequest = (DefaultMultipartHttpServletRequest) request;
    MultipartFile multipartFile = (MultipartFile) dmhsRequest.getFile("file");
    InputStream in = multipartFile.getInputStream();
    Database.getInstance().importData(in);
}

From source file:com.yqboots.initializer.web.controller.ProjectInitializerController.java

@RequestMapping(value = "/", method = RequestMethod.POST)
public Object startup(@ModelAttribute(WebKeys.MODEL) @Valid final ProjectInitializerForm form,
        final BindingResult bindingResult, final ModelMap model) throws IOException {
    if (bindingResult.hasErrors()) {
        return FORM_URL;
    }/*ww w  .  ja  va2  s  .c  om*/

    Path path;

    final MultipartFile file = form.getFile();
    if (StringUtils.isNotBlank(file.getName())) {
        try (InputStream inputStream = file.getInputStream()) {
            path = initializer.startup(form.getMetadata(), form.getTheme(), inputStream);
        }
    } else {
        path = initializer.startup(form.getMetadata(), form.getTheme());
    }

    final HttpEntity<byte[]> result = FileWebUtils.downloadFile(path,
            new MediaType("application", "zip", StandardCharsets.UTF_8));

    // clear temporary folder
    final Path parent = path.getParent();
    FileUtils.deleteDirectory(parent.toFile());

    // clear model
    model.clear();

    return result;
}

From source file:com.qcadoo.mes.integration.cfcSimple.IntegrationController.java

protected final String handleUpload(final HttpServletRequest request, final MultipartFile file,
        final Locale locale) {
    try {/*www .j  av a2  s  .c  om*/
        String redirect = getIntegrationPerformer().performImport(file.getInputStream());
        String context = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
        return redirect.replaceAll("\\$\\{root\\}", context);
    } catch (IOException e) {
        throw new IllegalStateException("Error while reading file", e);
    }
}

From source file:fr.univrouen.poste.web.admin.GalaxieExcelController.java

@RequestMapping(value = "/addFile", method = RequestMethod.POST, produces = "text/html")
public String addFile(@Valid GalaxieExcel galaxieExcel, BindingResult bindingResult, Model uiModel,
        HttpServletRequest httpServletRequest) throws IOException, SQLException {
    if (bindingResult.hasErrors()) {
        logger.warn(bindingResult.getAllErrors());
        return "redirect:/admin/galaxieexcels";
    }/*from w w  w.j  a v a 2  s  .  c  o  m*/
    uiModel.asMap().clear();

    // upload file
    MultipartFile file = galaxieExcel.getFile();
    String filename = file.getOriginalFilename();
    InputStream inputStream = file.getInputStream();
    byte[] bytes = IOUtils.toByteArray(inputStream);

    galaxieExcel.setFilename(filename);
    galaxieExcel.getBigFile().setBinaryFile(new SerialBlob(bytes));
    galaxieExcel.getBigFile().persist();

    // set current date 
    Calendar cal = Calendar.getInstance();
    galaxieExcel.setCreation(cal.getTime());

    // persist
    galaxieExcel.persist();

    // process : generate GalaxieEntries
    galaxieExcelParser.process(galaxieExcel);

    return "redirect:/admin/galaxieexcels";
}

From source file:fr.univrouen.poste.web.admin.CommissionExcelController.java

@RequestMapping(value = "/addFile", method = RequestMethod.POST, produces = "text/html")
public String addFile(@Valid CommissionExcel commissionExcel, BindingResult bindingResult, Model uiModel,
        HttpServletRequest httpServletRequest) throws IOException, SQLException {
    if (bindingResult.hasErrors()) {
        logger.warn(bindingResult.getAllErrors());
        return "redirect:/admin/commissionexcels";
    }/*from  w  w w. j av  a 2  s.  c o m*/
    uiModel.asMap().clear();

    // upload file
    MultipartFile file = commissionExcel.getFile();
    String filename = file.getOriginalFilename();
    InputStream inputStream = file.getInputStream();
    byte[] bytes = IOUtils.toByteArray(inputStream);

    commissionExcel.setFilename(filename);
    commissionExcel.getBigFile().setBinaryFile(new SerialBlob(bytes));
    commissionExcel.getBigFile().persist();

    // set current date 
    Calendar cal = Calendar.getInstance();
    commissionExcel.setCreation(cal.getTime());

    // persist
    commissionExcel.persist();

    // process : generate CommissionEntries
    commissionExcelParser.process(commissionExcel);

    return "redirect:/admin/commissionexcels";
}

From source file:com.yunmel.syncretic.core.BaseController.java

protected void uploadFile(HttpServletRequest request, String field, File file) {
    try {//w ww  .  ja  v a2  s  .  c om
        if (request instanceof MultipartHttpServletRequest) {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            MultiValueMap<String, MultipartFile> map = multipartRequest.getMultiFileMap();
            List<MultipartFile> files = map.get(field);
            MultipartFile _file = files.get(0);
            IOUtils.copyInputStreamToFile(_file.getInputStream(), file);
        }
    } catch (IOException e) {
        LOG.error("upload file error.", e);
    }
}

From source file:com.greglturnquist.springagram.fileservice.mongodb.ApplicationController.java

@RequestMapping(method = RequestMethod.POST, value = "/files")
public ResponseEntity<?> newFile(@RequestParam("name") String filename,
        @RequestParam("file") MultipartFile file) {

    if (!file.isEmpty()) {
        try {// w  w  w.j av  a2 s .c  o  m
            this.fileService.saveFile(file.getInputStream(), filename);

            Link link = linkTo(methodOn(ApplicationController.class).getFile(filename)).withRel(filename);
            return ResponseEntity.created(new URI(link.getHref())).build();

        } catch (IOException | URISyntaxException e) {
            return ResponseEntity.badRequest().body("Couldn't process the request");
        }
    } else {
        return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body("File is empty");
    }
}

From source file:net.mindengine.oculus.frontend.web.controllers.project.ProjectEditController.java

private BufferedImage readImageFromMultipartFile(MultipartFile multipartFile) {
    try {/*from w ww.  j a  va2  s. com*/
        return ImageIO.read(multipartFile.getInputStream());
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}