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:app.controller.PhotoController.java

@RequestMapping(method = RequestMethod.POST)
public String saveImage(@RequestParam("file") MultipartFile file) throws IOException {
    FileObject fo = new FileObject();
    fo.setContent(file.getBytes());

    imageRepository.save(fo);/*from   w  ww .ja v a 2 s. com*/

    return "redirect:/photocan";
}

From source file:com.portal.service.SimplePortalService.java

@Override
public void saveFile(String fileName, MultipartFile file) throws Exception {
    try {/*  w  w  w  .ja va 2 s. c  o  m*/
        FileUtils.writeByteArrayToFile(new File(fileName), file.getBytes());
    } catch (IOException ex) {
        throw new PortalException("? ? ? ");
    }
}

From source file:com.springsecurity.plugin.util.ImageResizer.java

public File createFile(MultipartFile file, ServletContext context, String fileName) {
    try {//  w ww  . jav  a 2s . c  om
        byte[] bytes = file.getBytes();
        // Creating the directory to store file
        String rootPath = context.getRealPath("");
        File dir = new File(rootPath + File.separator + MENU_IMG_FOLDER + File.separator + "Temp");
        if (!dir.exists())
            dir.mkdirs();
        String filePath = dir.getAbsolutePath() + File.separator + fileName;
        // Create the file on server
        File serverFile = new File(filePath);
        BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
        stream.write(bytes);
        stream.close();
        return serverFile;
    } catch (Exception e) {
        return null;
    }
}

From source file:feign.form.spring.SpringMultipartEncodedDataProcessor.java

@Override
protected void writeByteOrFile(OutputStream output, PrintWriter writer, String name, Object value) {
    if (value instanceof MultipartFile) {
        try {//from www .j  a v a  2s.c  om
            MultipartFile mpf = (MultipartFile) value;
            writeByteArray(output, writer, name, mpf.getOriginalFilename(), mpf.getContentType(),
                    mpf.getBytes());
        } catch (IOException e) {
            throw new EncodeException("Can't encode MultipartFile", e);
        }
        return;
    }

    super.writeByteOrFile(output, writer, name, value);
}

From source file:io.isoft.reg.controller.FileUploadController.java

/**
 * Accepts a POST request with multipart/form-data content
 * @param description the name of the file being uploaded
 * @param file the binary file//from   w  w  w.ja  v a  2 s  .c  o  m
 * @return response message indicating success or failure
 */
@RequestMapping(value = "postformdata", method = RequestMethod.POST, headers = "Content-Type=multipart/form-data")
public @ResponseBody String handleFormUpload(@RequestParam("description") String description,
        @RequestParam("file") MultipartFile file) {

    if (!file.isEmpty()) {
        byte[] bytes = null;
        try {
            bytes = file.getBytes();
        } catch (IOException e) {
            logger.info("error processing uploaded file", e);
        }
        return "file upload received! Name:[" + description + "] Size:[" + bytes.length + "]";
    } else {
        return "file upload failed!";
    }
}

From source file:com.redhat.rhtracking.web.controller.UploadController.java

@ResponseBody
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String handleFileUpload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file) {
    String r = "--- upload ---\n";
    try {/* w ww  .j a  va 2s .co  m*/
        if (!file.isEmpty()) {
            byte[] bytes = file.getBytes();
            r += "file uploaded\n";
            r += "name: " + name + "\n";
            r += new String(bytes) + "\n";
        } else {
            r += "got empty file";
        }
    } catch (IOException | NullPointerException ex) {
        r += "--IOException|NullPointerException--\n" + ex + "\n";
    }
    return r + "---end---";
}

From source file:gg.server.MailController.java

@RequestMapping(method = RequestMethod.POST)
public ModelAndView handleEmailUpload(@RequestParam("email") MultipartFile email) {
    if (!email.isEmpty()) {
        try {//from   w ww .ja  va 2s .c  o m
            byte[] bytes = email.getBytes();
            log.debug("Got POST {} bytes", bytes.length);
            mailer.sendMail(bytes);
        } catch (IOException e) {
            return errorPage("Failed receiving email data", e);
        } catch (RuntimeException e) {
            return errorPage("Failed sending mail", e);
        }
    }
    return success;
}

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

@RequestMapping(method = RequestMethod.POST)
public Map<String, Object> post(MultipartFile file) {
    try {//ww  w  .  j  a v  a 2s.c  om
        String metainfo = Base64.encodeBase64String(file.getBytes());
        if (client.torrentAdd(metainfo))
            return ResponseMsgFactory.createSuccessResponse();
        return ResponseMsgFactory.createFailedResponse("transmission");
    } catch (Exception e) {
        logger.error("transmission ?");
        return ResponseMsgFactory.createFailedResponse("transmission?");
    }
}

From source file:se.omegapoint.facepalm.client.controllers.ImageController.java

@RequestMapping(value = "/postImage", method = RequestMethod.POST)
public String postImage(final @RequestParam("title") String title,
        final @RequestParam("file") MultipartFile file) {
    if (!file.isEmpty()) {
        try {/*w  w w .  j  ava 2 s.  c  o  m*/
            final byte[] data = notNull(file.getBytes());
            imageAdapter.addImage(new ImageUpload(title, data));
        } catch (IOException e) {
            throw new IllegalArgumentException("Illegal file upload!");
        }
    }
    return "redirect:/";
}

From source file:org.shareok.data.webserv.PlosWebController.java

@RequestMapping(value = "/plos/upload", method = RequestMethod.POST)
public ModelAndView plosUpload(@RequestParam("file") MultipartFile file) {

    if (!file.isEmpty()) {
        try {//  w w w . ja va 2  s .co  m
            byte[] bytes = file.getBytes();

            // Creating the directory to store file
            //                String rootPath = System.getProperty("catalina.home");
            //                File dir = new File(rootPath + File.separator + "tmpFiles");
            //                if (!dir.exists())
            //                    dir.mkdirs();
            // 
            //                // Create the file on server
            //                File serverFile = new File(dir.getAbsolutePath()
            //                        + File.separator + name);
            //                BufferedOutputStream stream = new BufferedOutputStream(
            //                        new FileOutputStream(serverFile));
            //                stream.write(bytes);
            //                stream.close();

            //                logger.info("Server File Location="
            //                        + serverFile.getAbsolutePath());
            System.out.println("The uploaded file name is " + file.getName() + "\n");
            ModelAndView model = new ModelAndView();
            model.setViewName("plosDataUpload");
            model.addObject("message", file.getOriginalFilename());

            return model;
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        return null;
    }
    return null;
}