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:eu.scidipes.toolkits.pawebapp.util.Base64MultipartFileEditor.java

@Override
public void setValue(final Object value) {
    if (value instanceof MultipartFile) {
        final MultipartFile multipartFile = (MultipartFile) value;
        try {/*from   www  . j  av  a  2 s.  c  om*/
            super.setValue(Base64.encodeBase64String(multipartFile.getBytes()));
        } catch (final IOException ex) {
            throw new IllegalArgumentException("Cannot read contents of multipart file", ex);
        }
    } else {
        super.setValue(value);
    }
}

From source file:org.unidle.service.AttachmentServiceImpl.java

@Override
@Transactional//from   ww w.j  av a 2 s.c om
public Attachment createAttachment(final MultipartFile file) throws IOException {
    final Attachment attachment = new Attachment();

    attachment.setContent(file.getBytes());
    attachment.setContentType(file.getContentType());
    attachment.setTitle(file.getName());

    return attachmentRepository.save(attachment);
}

From source file:com.orchestra.portale.controller.UserRegistrationController.java

@RequestMapping(value = "/userSignIn", method = RequestMethod.POST)
public ModelAndView addUser(HttpServletRequest request, @ModelAttribute("SpringWeb") User user,
        MultipartFile avatar) {//from  w w  w. j  a  va2s.  c  o  m
    ModelAndView model2 = new ModelAndView("okpage");
    User usertest = pm.findUserByUsername(user.getUsername());
    if (usertest != null && usertest.getUsername().toLowerCase().equals(user.getUsername().toLowerCase())) {
        model2.addObject("err", "Esiste gi un utente con username: " + user.getUsername());
        return model2;
    }
    //HASH PASSWORD
    user.setPassword(crypt(user.getPassword()));
    /*Create Role*/
    Role new_user_role = new Role();
    new_user_role.setRole("ROLE_USER");
    new_user_role.setUser(user);
    ArrayList<Role> new_user_roles = new ArrayList<Role>();
    new_user_roles.add(new_user_role);
    user.setRoles(new_user_roles);
    pm.saveUser(user);

    User user2 = pm.findUserByUsername(user.getUsername());

    MultipartFile file = avatar;

    try {
        byte[] bytes = file.getBytes();

        // Creating the directory to store file
        HttpSession session = request.getSession();
        ServletContext sc = session.getServletContext();

        File dir = new File(sc.getRealPath("/") + "dist" + File.separator + "user" + File.separator + "img"
                + File.separator + user2.getId());
        if (!dir.exists())
            dir.mkdirs();

        // Create the file on server
        File serverFile = new File(dir.getAbsolutePath() + File.separator + "avatar.jpg");
        BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
        stream.write(bytes);
        stream.close();
    } catch (Exception e) {

    }
    model2.addObject("mess",
            "Registrazione completata con successo!<br><br><center> <a href='page?sec=home' class='btn btn-primary'>Torna alla Home</a></center> ");
    return model2;
}

From source file:de.ingrid.iplug.excel.controller.SwitchXlsController.java

/**
 * Upload excel files./*from w  w  w  .  j av  a 2 s.c o  m*/
 * 
 * @param sheetIndex
 * @param plugDescription
 * @param uploadBean
 * @return
 *       Web request "redirect:/iplug-pages/listMappings.html"
 * @throws IOException
 */
@RequestMapping(value = "/iplug-pages/switchXls.html", method = RequestMethod.POST)
public String upload(@RequestParam final Integer sheetIndex,
        @ModelAttribute("plugDescription") final PlugdescriptionCommandObject plugDescription,
        @ModelAttribute("uploadBean") final UploadBean uploadBean) throws IOException {
    final MultipartFile multipartFile = uploadBean.getFile();
    final byte[] uploadBytes = multipartFile.getBytes();
    final Sheets sheets = (Sheets) plugDescription.get("sheets");
    final String fileName = sheets.getSheets().get(sheetIndex).getFileName();
    final File mappingDir = new File(plugDescription.getWorkinDirectory(), "mapping");
    final File newXlsFile = new File(mappingDir, fileName);
    final FileOutputStream outputStream = new FileOutputStream(newXlsFile);
    outputStream.write(uploadBytes);
    outputStream.close();
    return "redirect:/iplug-pages/listMappings.html";

}

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

@Test
public void testCreate() throws IOException {
    MultipartFile file = mock(MultipartFile.class);
    when(file.getBytes()).thenReturn(null);
    workflowController.create(1L, Optional.empty(), Optional.empty(), file);
    verify(workflowService, times(1)).createWorkflow(1L, Optional.empty(), null);
}

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

@Test
public void testCreateWorkflows() throws IOException {
    MultipartFile file = mock(MultipartFile.class);
    when(file.getBytes()).thenReturn(null);
    workflowController.create(1L, Optional.empty(), Optional.of("zip"), file);
    verify(workflowService, times(1)).createWorkflows(1L, Optional.empty(), null);
}

From source file:com.anthony.forumspring.controller.FileUploadController.java

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public @ResponseBody String uploadFildHander(@RequestParam(value = "name") String name,
        @RequestParam("file") MultipartFile file) {

    if (!file.isEmpty()) {
        try {//w  w w  .  j  ava 2  s. com
            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());
            return "You successfully uploaded file=" + name;
        } 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.digitaslbi.helios.controllers.FileController.java

@RequestMapping(value = "/uploadObject", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)/*from   www  .  j  a  v  a 2s  . c  om*/
public void uploadObject(@RequestParam("fileName") String fileName, @RequestParam("file") MultipartFile file) {
    try {
        delegate = new S3Delegate();
        byte[] content = file.getBytes();
        delegate.uploadObject(fileName, content);

        log.info("[FileController][uploadObject] File: " + fileName + " created.");
    } catch (IOException e) {
        log.error("[FileController][uploadObject] Error creating file: " + e);
    }
}

From source file:it.polimi.diceH2020.launcher.controller.FilesController.java

private File saveFile(MultipartFile file, String fileName) {
    try {//  w w  w.j  a v a 2  s  .  c o  m
        byte[] bytes = file.getBytes();
        File f = fileUtility.provideFile(fileName);
        BufferedOutputStream buffStream = new BufferedOutputStream(new FileOutputStream(f));
        buffStream.write(bytes);
        buffStream.close();
        return f;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
}

From source file:service.FilesServiceImpl.java

@Override
public void add(MultipartFile file, UsersEntity usersEntity) {
    FilesEntity fileEntity;/* w  w  w  . ja  v a2  s .c  o m*/
    try {
        fileEntity = new FilesEntity(file.getName(), file.getContentType(), file.getBytes(), usersEntity);
        this.filesDao.save(fileEntity);
    } catch (IOException ex) {
        Logger.getLogger(FilesServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
}