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:net.solarnetwork.node.setup.web.SettingsController.java

@RequestMapping(value = "/import", method = RequestMethod.POST)
public String importSettings(@RequestParam("file") MultipartFile file) throws IOException {
    final SettingsService service = settingsServiceTracker.service();
    if (!file.isEmpty() && service != null) {
        InputStreamReader reader = new InputStreamReader(file.getInputStream(), "UTF-8");
        service.importSettingsCSV(reader);
    }/*from w  ww  .  jav  a  2 s  .co  m*/
    return "redirect:/settings.do";
}

From source file:net.solarnetwork.node.setup.web.SettingsController.java

@RequestMapping(value = "/importBackup", method = RequestMethod.POST)
public String importBackup(@RequestParam("file") MultipartFile file) throws IOException {
    final BackupManager manager = backupManagerTracker.service();
    if (manager == null) {
        return "redirect:/settings.do";
    }/*from  w  w w  .  ja v a 2 s.  c  o m*/
    manager.importBackupArchive(file.getInputStream());
    return "redirect:/settings.do";
}

From source file:org.focusns.web.modules.profile.ProjectUserWidget.java

@RequestMapping("/user-avatar/upload")
public void doUpload(@RequestParam Long projectId, @RequestParam Long userId, MultipartFile file)
        throws IOException {
    ///*from   w  ww . ja  va  2  s. co m*/
    ProjectUser projectUser = projectUserService.getProjectUser(userId);
    Object[] avatarCoordinates = CoordinateHelper.getAvatarCoordinates(projectUser);
    storageService.persistTempResource(file.getInputStream(), avatarCoordinates);
    //
    Navigator.get().navigateTo("avatar-uploaded");
}

From source file:org.openmrs.module.dataexchange.web.controller.DataExchangeController.java

@RequestMapping(value = "/import", method = RequestMethod.POST)
public void importData(@RequestParam("file") MultipartFile file, Model model) throws IOException {
    Writer writer = null;//  w w  w  . jav  a2 s . com
    File tempFile = null;
    try {
        tempFile = File.createTempFile("concepts", ".xml");
        writer = new OutputStreamWriter(new FileOutputStream(tempFile), "UTF-8");
        IOUtils.copy(file.getInputStream(), writer, "UTF-8");
        writer.close();

        dataImporter.importData(tempFile.getPath());
    } finally {
        IOUtils.closeQuietly(writer);
        if (tempFile != null) {
            tempFile.delete();
        }
    }

    model.addAttribute("success", true);
}

From source file:pdl.web.service.common.FileService.java

public Map<String, String> uploadFile(MultipartFile theFile, String type, String username) {
    Map<String, String> rtnJson = new TreeMap<String, String>();
    try {/*  w  w  w . j  a  v  a2s  .  c om*/
        /*if(type.isEmpty())
        type="blob";*/

        String fileUid = null;
        if (theFile.getSize() > 0) {
            InputStream fileIn = theFile.getInputStream();
            fileUid = fileTool.createFile(type, fileIn, theFile.getOriginalFilename(), username);
        }

        if (fileUid == null)
            throw new Exception();

        rtnJson.put("id", fileUid);
    } catch (Exception ex) {
        rtnJson.put("error", "File upload failed for " + theFile.getOriginalFilename());
        rtnJson.put("message", ex.toString());
    }

    return rtnJson;
}

From source file:com.vsquaresystem.safedeals.readyreckoner.ReadyReckonerService.java

@Transactional(readOnly = false)
public Boolean insertAttachments(MultipartFile attachmentMultipartFile)
        throws JsonProcessingException, IOException {
    Readyreckoner rr = new Readyreckoner();

    File outputFile = attachmentUtils.storeAttachmentByAttachmentType(
            attachmentMultipartFile.getOriginalFilename(), attachmentMultipartFile.getInputStream(),
            AttachmentUtils.AttachmentType.READY_RECKONER);
    return outputFile.exists();
}

From source file:com.zjzcn.controller.admin.ProcessController.java

/**
 * ??//from  www  .j av a2s  . c om
 * @param snakerFile
 * @param id
 * @return
 * @throws IOException 
 */
@RequestMapping(value = "deploy", method = RequestMethod.POST)
public void processDeploy(@RequestParam(value = "snakerFile") MultipartFile snakerFile, String id,
        Writer writer) throws IOException {
    InputStream input = null;
    try {
        input = snakerFile.getInputStream();
        if (StringUtils.isNotEmpty(id)) {
            flowManager.getEngine().process().redeploy(id, input);
        } else {
            flowManager.getEngine().process().deploy(input);
        }
    } catch (IOException e) {
        writer.write("fail");
        e.printStackTrace();
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                writer.write("fail");
                e.printStackTrace();
            }
        }
    }
    writer.write("ok");
}

From source file:com.qq.tars.web.controller.patch.UploadController.java

@RequestMapping(value = "server/api/upload_patch_package", produces = "application/json")
@ResponseBody//from  w ww  .j av a  2 s  .  co m
public ServerPatchView upload(@Application @RequestParam String application,
        @ServerName @RequestParam("module_name") String moduleName, HttpServletRequest request,
        ModelMap modelMap) throws Exception {
    String comment = StringUtils.trimToEmpty(request.getParameter("comment"));
    String uploadTgzBasePath = systemConfigService.getUploadTgzPath();

    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
            request.getSession().getServletContext());
    if (multipartResolver.isMultipart(request)) {
        MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
        Iterator<String> it = multiRequest.getFileNames();
        if (it.hasNext()) {
            MultipartFile file = multiRequest.getFile(it.next());

            String originalName = file.getOriginalFilename();
            String extension = FilenameUtils.getExtension(originalName);
            String temporary = uploadTgzBasePath + "/" + UUID.randomUUID() + "." + extension;
            IOUtils.copy(file.getInputStream(), new FileOutputStream(temporary));

            String packageType = "suse";

            // war?
            if (temporary.endsWith(".war")) {
                temporary = patchService.war2tgz(temporary, moduleName);
            }

            // ?
            String updateTgzPath = uploadTgzBasePath + "/" + application + "/" + moduleName;

            // ????
            String uploadTgzName = application + "." + moduleName + "_" + packageType + "_"
                    + System.currentTimeMillis() + ".tgz";

            // ??
            String uploadTgzFullPath = updateTgzPath + "/" + uploadTgzName;

            log.info("temporary path={}, upload path={}", temporary, uploadTgzFullPath);

            File uploadPathDir = new File(updateTgzPath);
            if (!uploadPathDir.exists()) {
                if (!uploadPathDir.mkdirs()) {
                    throw new IOException(
                            String.format("mkdirs error, path=%s", uploadPathDir.getCanonicalPath()));
                }
            }

            FileUtils.moveFile(new File(temporary), new File(uploadTgzFullPath));

            return mapper.map(patchService.addServerPatch(application, moduleName, uploadTgzFullPath, comment),
                    ServerPatchView.class);
        }
    }

    throw new Exception("???");
}

From source file:com.kdgregory.pomutil.cleaner.web.MainController.java

@RequestMapping(method = RequestMethod.POST)
public ModelAndView doPost(WebRequest request,
        @RequestParam(value = "file", required = true) MultipartFile file) throws Exception {
    OptionTranslator options = new OptionTranslator(request);

    logger.info("invoked via POST, options = {}", options);

    InputStream in = null;/*from w w  w  . j a v  a2  s  .  c  o m*/
    String result = "";
    try {
        in = file.getInputStream();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        new Cleaner(options.toCommandLine(), in, out).run();
        result = new String(out.toByteArray(), "UTF-8");
    } catch (Exception ex) {
        result = "unable to process POM";
    } finally {
        IOUtil.closeQuietly(in);
    }

    ModelAndView mav = new ModelAndView("main");
    mav.addObject("options", options.toOptionList());
    mav.addObject("cleanedPom", result);
    return mav;
}

From source file:org.openmrs.module.idcards.web.controller.PrintEmptyIdcardsFormController.java

/**
 * Handles an upload of identifiers to print onto empty id cards
 *///from  w  w w .  j av a2  s . c  o  m
@RequestMapping(method = RequestMethod.POST, value = "/module/idcards/uploadAndPrintIdCards")
public void uploadAndPrintIdCards(ModelMap model, HttpServletRequest request, HttpServletResponse response,
        @RequestParam(required = true, value = "templateId") Integer templateId,
        @RequestParam(required = true, value = "inputFile") MultipartFile inputFile,
        @RequestParam(required = true, value = "pdfPassword") String pdfPassword) throws Exception {

    // Get identifiers from file
    List<String> identifiers = new ArrayList<String>();
    BufferedReader r = null;
    try {
        r = new BufferedReader(new InputStreamReader(inputFile.getInputStream()));
        for (String s = r.readLine(); s != null; s = r.readLine()) {
            identifiers.add(s);
        }
    } finally {
        if (r != null) {
            r.close();
        }
    }

    IdcardsService is = (IdcardsService) Context.getService(IdcardsService.class);
    IdcardsTemplate card = is.getIdcardsTemplate(templateId);

    StringBuffer requestURL = request.getRequestURL();
    String baseURL = requestURL.substring(0, requestURL.indexOf("/module/idcards"));

    PrintEmptyIdcardsServlet.generateOutputForIdentifiers(card, baseURL, response, identifiers, pdfPassword);
}