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:de.metas.ui.web.upload.ImageRestController.java

@PostMapping
public int uploadImage(@RequestParam("file") final MultipartFile file) throws IOException {
    userSession.assertLoggedIn();//from  ww  w  .  java 2 s  . c  o  m

    final String name = file.getOriginalFilename();
    final byte[] data = file.getBytes();

    final MImage adImage = new MImage(userSession.getCtx(), 0, ITrx.TRXNAME_None);
    adImage.setName(name);
    adImage.setBinaryData(data);
    InterfaceWrapperHelper.save(adImage);

    return adImage.getAD_Image_ID();
}

From source file:org.dineth.shooter.app.view.ImageController.java

@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody/*from  www  .ja  v a2s.  c o  m*/
public String handleFormUpload(@RequestParam("file") MultipartFile file) {

    File destination = null;
    if (!file.isEmpty()) {

        try {
            BufferedImage src = ImageIO.read(new ByteArrayInputStream(file.getBytes()));

            destination = new File(homefilefolder + file.getOriginalFilename());
            destination.mkdirs();

            ImageIO.write(src, FilenameUtils.getExtension(file.getOriginalFilename()), destination);

            //Save the id you have used to create the file name in the DB. You can retrieve the image in future with the ID.
        } catch (IOException ex) {
            Logger.getLogger(ImageController.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {
        return "bad thing";
    }
    return destination.getName();
}

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

@RequestMapping(value = "/fileupload", method = RequestMethod.POST)
public @ResponseBody String fileuploadstore(@RequestParam("name") String name,
        @RequestParam("file") MultipartFile file, ModelMap map) {
    if (!file.isEmpty()) {
        try {//w  w w .  j ava 2 s  .  c  om
            byte[] bytes = file.getBytes();
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name)));
            stream.write(bytes);
            stream.close();
            map.addAttribute("uploadoption", "uploaded");
            map.addAttribute("Status", "uploaded Successfully" + name);

        } catch (Exception e) {
            map.addAttribute("uploadoption", "uploaded");
            map.addAttribute("Status", "uploaded UnSuccessfully" + name);
        }
    } else {
        map.addAttribute("Status", "uploaded  is Empty" + name);
    }
    return "fileupload";
}

From source file:net.anthonychaves.bookmarks.web.BookmarkFileUploadController.java

@RequestMapping(method = RequestMethod.POST)
public String uploadBookmarkFile(@RequestParam(value = "file") MultipartFile file, HttpSession session,
        ModelMap model) throws IOException {

    HtmlCleaner cleaner = new HtmlCleaner();
    TagNode root = cleaner.clean(new String(file.getBytes()));
    List<TagNode> nodes = (List<TagNode>) root.getElementListHavingAttribute("href", true);

    List<Bookmark> bookmarks = new ArrayList<Bookmark>();
    for (TagNode node : nodes) {
        bookmarks.add(bookmarkService.makeBookmark(node));
    }//w w  w .  j a v  a2 s  .c  om

    User user = (User) session.getAttribute("user");
    user = userService.addBookmarks(user, bookmarks);
    session.setAttribute("user", user);

    model.addAttribute(user.getBookmarksDetail());

    return "redirect:/b/user";
}

From source file:com.springsource.html5expense.controllers.ExpenseReportingApiController.java

@RequestMapping(value = "/receipts", method = RequestMethod.POST)
@ResponseBody//  ww  w.  ja  v a 2s .co  m
public String attachReceipt(@RequestParam("reportId") Long reportId,
        @RequestParam("expenseId") Integer expenseId, @RequestParam("file") MultipartFile file) {
    try {
        byte[] bytesForImage = file.getBytes();
        String ext = findExtensionFromFileName(file.getOriginalFilename());
        if (ext != null) {
            ext = ext.trim().toLowerCase();
        }
        String receiptKey = service.attachReceipt(reportId, expenseId, ext, bytesForImage);
        return receiptKey;
    } catch (Throwable th) {
        if (log.isErrorEnabled()) {
            log.error("Something went wrong trying to write the file out.", th);
        }
    }
    return null;
}

From source file:de.iteratec.iteraplan.presentation.dialog.ExcelImport.ImportFrontendServiceImpl.java

private void setFileContentToMemBean(MassdataMemBean memBean, MultipartFile file) {
    try {/*w  ww . j av  a 2 s .  co m*/
        memBean.setFileContent(file.getBytes());
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("File length: {0}", Integer.valueOf(file.getBytes().length));
        }
    } catch (IOException e) {
        LOGGER.error("Error reading file \"{0}\"", file.getOriginalFilename());
        throw new IteraplanTechnicalException(IteraplanErrorMessages.GENERAL_TECHNICAL_ERROR, e);
    }
}

From source file:com.card.loop.xyz.pageControllers.LOIDEController.java

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void uploadLearningElement(@RequestParam("title") String title, @RequestParam("author") String authorID,
        @RequestParam("description") String description, @RequestParam("file") MultipartFile file) {
    if (!file.isEmpty()) {
        try {// ww  w. j  a  v  a2 s .co m
            byte[] bytes = file.getBytes();
            File fil = new File(AppConfig.UPLOAD_BASE_PATH + title);

            if (!fil.getParentFile().exists())
                fil.getParentFile().mkdirs();

            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(fil));
            stream.write(bytes);
            stream.close();

            LearningElement le = new LearningElement();
            le.setTitle(title);
            le.setUploadedBy(authorID);
            le.setDescription(description);
            le.setDownloads(0);
            le.setStatus(1);
            le.setRating(1);
            le.setUploadDate(new Date());
            le.setFilePath(AppConfig.DOWNLOAD_BASE_PATH);
            le.setFilename(file.getOriginalFilename());
            le.setContentType(file.getOriginalFilename().split("\\.")[1]);
            daoLE.addLearningElement(le);

            //   LearningElement lo = new LearningElement(title, authorID, description);

            //   lo.setType(file.getOriginalFilename().split("\\.")[1]);
            //   Database.get().save(lo);

            System.out.println("UPLOAD FINISHED");
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        System.err.println("EMPTY FILE.");
    }
}

From source file:com.oak_yoga_studio.controller.ProductController.java

@RequestMapping(value = "/productEdit", method = RequestMethod.POST)
public String updateProduct(@Valid Product product, BindingResult result,
        @RequestParam("file") MultipartFile file) {

    if (!result.hasErrors()) {
        try {/*from   w ww. j  a va 2  s .c  o m*/
            product.setImage(file.getBytes());
            product.setStatus("ACTIVE");
            productService.updateProduct(product);
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return "redirect:/products";
    } else {
        for (FieldError err : result.getFieldErrors()) {
            System.out.println(err.getField() + ": " + err.getDefaultMessage());

        }
        return "editProduct";
    }

}

From source file:br.edu.unidavi.restapp.FileUploadController.java

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody String handleFileUpload(@RequestParam("file") MultipartFile file) {
    if (!file.isEmpty()) {
        try {//w  w w .  ja  va 2  s  .c  om
            String name = file.getOriginalFilename();
            System.out.println(name);
            byte[] bytes = file.getBytes();
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name)));
            stream.write(bytes);
            stream.close();
            return "You successfully uploaded !";
        } catch (Exception e) {
            return "You failed to upload => " + e.getMessage();
        }
    } else {
        return "You failed to upload because the file was empty.";
    }
}

From source file:org.owasp.dependencytrack.service.ReportService.java

private File convert(MultipartFile file) throws IOException {
    File convFile = new File(System.currentTimeMillis() + "-" + file.getOriginalFilename());
    convFile.createNewFile();//ww  w .  j av a  2s  . c  o  m
    FileOutputStream fos = new FileOutputStream(convFile);
    fos.write(file.getBytes());
    fos.close();
    return convFile;
}