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:software.uncharted.rest.ImageResource.java

@RequestMapping(value = "/histogram", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ImageHistogramResponse getImageHistogram(@RequestParam("image") final MultipartFile file)
        throws IOException {
    String fileType = file.getContentType();
    if (fileType.startsWith("image")) {
        BufferedImage image = ImageIO.read(file.getInputStream());
        final String histogram = service.getHistogram(image);
        final String base64Str = ImageProcessing.toBase64(image);
        return new ImageHistogramResponse().setDataURI("data:image/png;base64," + base64Str)
                .setHistogram(histogram);
    }/* w w w. j a  v a  2 s  .c o m*/
    return null;
}

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

@PostMapping
@ResponseBody//from   ww  w . j  a v  a2s . c o 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:io.lavagna.web.api.ExportImportController.java

@RequestMapping(value = "/api/import/lavagna", method = RequestMethod.POST)
@ResponseBody// w w w  .  j a  v  a2s  .  co m
public void importFromLavagna(
        @RequestParam(value = "overrideConfiguration", defaultValue = "false") Boolean overrideConfiguration,
        @RequestParam("file") MultipartFile file) throws IOException {

    Path tempFile = Files.createTempFile(null, null);

    try {
        try (InputStream is = file.getInputStream(); OutputStream os = Files.newOutputStream(tempFile)) {
            StreamUtils.copy(is, os);
        }

        exportImportService.importData(overrideConfiguration, tempFile);
    } finally {
        Files.delete(tempFile);
    }

}

From source file:com.capstone.giveout.controllers.UsersController.java

private void saveImages(User user, MultipartFile image) throws IOException {
    if (image == null || image.isEmpty())
        return;/*from  w  w w.j ava 2s . c  o m*/

    BufferedImage img = ImageIO.read(image.getInputStream());
    ImageFileManager imgMan = ImageFileManager.get(USERS_ROOT_PATH);

    imgMan.saveImage(user.getId(), User.SIZE_FULL, img);
    user.setImageUrlFull(Routes.USERS_IMAGE_PATH.replace("{id}", String.valueOf(user.getId())).replace("{size}",
            User.SIZE_FULL));

    BufferedImage scaledImg = Scalr.resize(img, 640);
    imgMan.saveImage(user.getId(), User.SIZE_MEDIUM, scaledImg);
    user.setImageUrlMedium(Routes.USERS_IMAGE_PATH.replace("{id}", String.valueOf(user.getId()))
            .replace("{size}", User.SIZE_MEDIUM));

    users.save(user);
}

From source file:com.ace.erp.controller.sys.company.OrganizationController.java

@RequestMapping(value = "/company/upload", method = RequestMethod.POST)
@ResponseBody/*from   ww  w .j av  a2  s.  co m*/
public Response upload(@CurrentUser User user, String srcFile, MultipartHttpServletRequest request,
        HttpServletResponse response) {

    try {
        MultipartFile realPicFile = request.getFile(srcFile);
        InputStream in = realPicFile.getInputStream();
        String path = request.getSession().getServletContext().getRealPath("/");
        String fileName = user.getUsername() + "."
                + FilenameUtils.getExtension(realPicFile.getOriginalFilename());
        File f = new File(path + "/" + fileName);
        FileUtils.copyInputStreamToFile(in, f);
        logger.info("path : " + f.getAbsolutePath());
    } catch (Exception e) {
        logger.error("upload header picture error : ", e);
    }
    return null;
}

From source file:net.seedboxer.web.controller.rs.TorrentsAPI.java

@RequestMapping(value = "add", method = RequestMethod.POST)
public @ResponseBody APIResponse addTorrent(@RequestPart("file") MultipartFile file) {
    try {/*from  w  w  w  .j ava2 s.co  m*/
        String filename = file.getOriginalFilename();
        if (filename.endsWith(".torrent")) {
            controller.addTorrent(getUser(), filename, file.getInputStream());
            return APIResponse.createSuccessfulResponse();
        } else {
            return APIResponse.createErrorResponse("Wrong file type, only accept .torrent files");
        }
    } catch (Exception e) {
        LOGGER.error("Can not read list of downloads", e);
        return APIResponse.createErrorResponse("Can not put to download");
    }
}

From source file:org.coursera.cmbrehm.kewlvideo.server.VideoController.java

@RequestMapping(value = "/video/{id}/data", method = RequestMethod.POST)
public ResponseEntity<VideoStatus> saveVideoData(@PathVariable("id") long id,
        @RequestPart("data") MultipartFile videoData) {
    ResponseEntity<VideoStatus> response;
    try {//from ww w  . j av a2s  . c om
        InputStream videoDataStream = videoData.getInputStream();
        VideoFileManager vfmgr = VideoFileManager.get();
        Video video = videoList.get(id);
        if (video != null) {
            vfmgr.saveVideoData(video, videoDataStream);
            response = new ResponseEntity<VideoStatus>(new VideoStatus(VideoState.READY), HttpStatus.ACCEPTED);
        } else {
            response = new ResponseEntity<VideoStatus>(HttpStatus.NOT_FOUND);
        }
    } catch (IOException iox) {
        response = new ResponseEntity<VideoStatus>(HttpStatus.INTERNAL_SERVER_ERROR);
    }
    return response;
}

From source file:com.multiimages.insertmultiimg.InsertMultiImg.java

@Transactional(value = "EmployeesDBTransactionManager")
public void uploadFile(Integer relativePath, MultipartFile[] files, HttpServletRequest httpServletRequest) {
    /* Note: relativePath here maps to the id of the related Object to be saved in the transaction */
    File outputFile = null;/* w  w  w . j  a v a2  s .com*/
    Session session = sessionFactory.getObject().openSession();

    for (MultipartFile file : files) {
        try {
            Image image = new Image();
            image.setEmployee(employeeService.findById(relativePath));

            byte[] byteArray = IOUtils.toByteArray(file.getInputStream());
            Blob blob = Hibernate.getLobCreator(session).createBlob(new ByteArrayInputStream(byteArray),
                    new Long(byteArray.length));

            image.setImgdate(blob);

            imageService.create(image);
        } catch (Exception e) {
        }
    }
}

From source file:alfio.controller.api.SpecialPriceApiController.java

@RequestMapping("/events/{eventName}/categories/{categoryId}/link-codes")
public List<SendCodeModification> linkAssigneeToCodes(@PathVariable("eventName") String eventName,
        @PathVariable("categoryId") int categoryId, @RequestParam("file") MultipartFile file,
        Principal principal) throws IOException {

    Validate.isTrue(StringUtils.isNotEmpty(eventName));
    try (InputStreamReader isr = new InputStreamReader(file.getInputStream())) {
        CSVReader reader = new CSVReader(isr);
        List<SendCodeModification> content = reader.readAll().stream().map(line -> {
            Validate.isTrue(line.length >= 4);
            return new SendCodeModification(StringUtils.trimToNull(line[0]), line[1], line[2], line[3]);
        }).collect(Collectors.toList());
        return specialPriceManager.linkAssigneeToCode(content, eventName, categoryId, principal.getName());
    }/*  w w w .j av  a2 s . co  m*/
}

From source file:com.qubit.solution.fenixedu.bennu.webservices.ui.management.keystores.UploadKeyController.java

@RequestMapping(value = "/upload/{oid}", method = RequestMethod.POST)
public String uploadkeyToUpload(@PathVariable("oid") DomainKeyStore domainKeyStore,
        @RequestParam(value = "keyAlias", required = false) String alias,
        @RequestParam(value = "keyPassword", required = false) String keyPassword,
        @RequestParam(value = "keyFile", required = false) MultipartFile keyFile, Model model) {

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try {//ww  w.ja v  a2  s  .co  m
        StreamUtils.copy(keyFile.getInputStream(), byteArrayOutputStream);
    } catch (IOException e) {
        e.printStackTrace();
    }
    byte[] content = byteArrayOutputStream.toByteArray();
    addKeyToKeyStore(domainKeyStore, alias, keyPassword, content);

    return "redirect:/webservices/management/keystores/domainkeystore/read/" + domainKeyStore.getExternalId();
}