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

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

Introduction

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

Prototype

byte[] getBytes() throws IOException;

Source Link

Document

Return the contents of the file as an array of bytes.

Usage

From source file:coral.reef.web.FileUploadController.java

@RequestMapping(value = "/admin/upload", method = RequestMethod.POST)
public String handleFileUpload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file)
        throws IOException {
    if (!file.isEmpty()) {
        byte[] bytes = file.getBytes();
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);

        name = name.replaceAll("[^a-zA-Z0-9_]", "");
        if (name.length() > 10) {
            name = name.substring(0, 10);
        }/*  ww w  .j  a va  2 s .  c o  m*/

        File target = new File("uploads", name);
        target.mkdirs();
        unzip(bis, target);

        return "redirect:/admin/edit?name=" + name + "&basepath=" + target.getAbsolutePath();
    } else {
        throw new RuntimeException("uplodaed file contains no data");
    }
}

From source file:julie.com.mikaelson.file.controller.FileInfoController.java

public @ResponseBody String handleFileUpload(@RequestParam("name") String name,
        @RequestParam("file") MultipartFile file) {
    if (!file.isEmpty()) {
        try {//from w  ww .j  a  va2  s.c o  m
            byte[] bytes = file.getBytes();
            BufferedOutputStream stream = new BufferedOutputStream(
                    new FileOutputStream(new File(name + "-uploaded")));
            stream.write(bytes);
            stream.close();
            return "You successfully uploaded " + name + " into " + name + "-uploaded !";
        } catch (Exception e) {
            return "You failed to upload " + name + " => " + e.getMessage();
        }
    } else {
        return "You failed to upload " + name + " because the file was empty.";
    }
}

From source file:com.zuoxiaolong.blog.web.controller.FileController.java

/**
 * //  w w w . ja va  2 s. c om
 *
 * @param imageFile ???
 */
@CheckLogin
@RequestMapping(value = "/Upload", method = RequestMethod.POST)
public void upload(@RequestParam MultipartFile imageFile) throws IOException {
    Attachment attachment = new Attachment();
    attachment.setFileName(imageFile.getOriginalFilename());
    attachment.setData(imageFile.getBytes());
    JsonResponse jsonResponse = invokeApi(Api.File_Upload, "file", new Attachment[] { attachment });
    if (jsonResponse.success()) {
        renderJson(ConfigurerPropertiesHolder.getProperty("api.url") + "/" + jsonResponse.getData());
    } else {
        renderJson("error");
    }
}

From source file:am.ik.categolj2.api.file.FileHelper.java

UploadFile multipartFileToUploadFile(MultipartFile multipartFile) {
    UploadFile uploadFile = new UploadFile();
    uploadFile.setFileName(multipartFile.getOriginalFilename());
    try {/*from ww  w  .  jav a 2s .  c  om*/
        uploadFile.setFileContent(multipartFile.getBytes());
    } catch (IOException e) {
        throw new SystemException("upload failed", e);
    }
    return uploadFile;
}

From source file:com.facerecog.rest.controller.RecognitionController.java

@RequestMapping(value = ApiUrls.URL_RECOG_UPLOAD_IMAGE, method = RequestMethod.POST)
public String handleFileUpload(@RequestParam("file") MultipartFile file) {
    logger.info("File upload");

    if (!file.isEmpty()) {
        try {// ww  w  .j av  a2  s  . co m
            byte[] bytes = file.getBytes();
            BufferedOutputStream stream = new BufferedOutputStream(
                    new FileOutputStream(new File(file.getOriginalFilename())));
            stream.write(bytes);
            stream.close();
            return "File uploaded: " + file.getOriginalFilename();
        } catch (Exception e) {
            return "Failed to upload image!";
        }
    } else {
        return "Failed to upload file because the file was empty.";
    }
}

From source file:de.appsolve.padelcampus.controller.ImagesController.java

@ResponseBody
@RequestMapping(value = "upload", method = POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.TEXT_PLAIN_VALUE)
public String postImage(@RequestParam("file") MultipartFile file, HttpServletRequest request)
        throws IOException, ImageProcessingException {
    Image image = imageUtil.saveImage(file.getContentType(), file.getBytes(), ImageCategory.cmsImage);
    return "/images/image/" + image.getSha256();
}

From source file:org.ow2.proactive.workflow_catalog.rest.controller.WorkflowRevisionController.java

@ApiOperation(value = "Creates a new workflow revision")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Bucket not found"),
        @ApiResponse(code = 422, message = "Invalid XML workflow content supplied") })
@RequestMapping(value = "/buckets/{bucketId}/workflows/{workflowId}/revisions", consumes = {
        MediaType.MULTIPART_FORM_DATA_VALUE }, method = POST)
@ResponseStatus(HttpStatus.CREATED)/* ww w . ja va2s  .  c  o  m*/
public WorkflowMetadata create(@PathVariable Long bucketId, @PathVariable Long workflowId,
        @ApiParam(value = "Layout describing the tasks position in the Workflow Revision") @RequestParam(required = false) Optional<String> layout,
        @RequestPart(value = "file") MultipartFile file) throws IOException {
    return workflowRevisionService.createWorkflowRevision(bucketId, Optional.of(workflowId), file.getBytes(),
            layout);
}

From source file:net.codejava.analyse.HomeController.java

/**
 * Controller method which respondes to a POST requst. It receives a MultipartFile, which in this case is a picture.
 * The controller writes the MultipartFile as an image to the disk. Afterwards it is executed with the analyse program.
 * After the analysis is performed the result is sent back to the client.
 * @throws InterruptedException //  w  w  w. j  a  v a  2  s . c o  m
 */
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public String handleFormUpload(@RequestParam("file") MultipartFile file)
        throws IOException, InterruptedException {
    Read r = new Read();
    if (!file.isEmpty()) {
        byte[] bytes = file.getBytes();//Put the bytes into a byte array
        FileOutputStream fos = new FileOutputStream("C:\\Users\\Martin\\Desktop\\IMG1.bmp");//A FileOutputStream with the path where the picture should be
        try {
            fos.write(bytes);//Writes the bytes to a file, in this case "creating" the picture as a .bmp
        } finally {
            fos.close();
            new CommandExecution("C:\\Users\\Martin\\SoftwareEng\\Analyse.bat");
        }
        Thread.sleep(3000);
        String result = r.reader("C:\\Users\\Martin\\Desktop\\Result.txt");
        return result;
    } else {
        return "doesn't work";//Returns this in case of empty file
    }
}

From source file:ru.mystamps.web.service.DatabaseImagePersistenceStrategy.java

@Override
public void save(MultipartFile file, ImageInfoDto image) {
    try {/* w  w w .  j  a va  2 s  .com*/
        AddImageDataDbDto imageData = new AddImageDataDbDto();
        imageData.setImageId(image.getId());
        imageData.setContent(file.getBytes());

        Integer id = imageDataDao.add(imageData);
        LOG.info("Image's data #{} for image #{} have been saved", id, image.getId());

    } catch (IOException e) {
        // throw RuntimeException for rolling back transaction
        throw new ImagePersistenceException(e);
    }
}

From source file:edu.mayo.xsltserver.controller.AdminController.java

/**
 * upload./*from   w w  w .ja  v a 2 s.c  o  m*/
 *
 * @param request the request
 * @param response the response
 * @return the redirect view
 * @throws Exception the exception
 */
@RequestMapping(value = "/admin/files", method = RequestMethod.POST)
public RedirectView upload(HttpServletRequest request, HttpServletResponse response) throws Exception {

    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    MultipartFile multipartFile = multipartRequest.getFile("file");

    String fileName = multipartFile.getOriginalFilename();
    fileService.store(fileName, multipartFile.getBytes());

    RedirectView view = new RedirectView("../admin");
    view.setExposeModelAttributes(false);

    return view;
}