Example usage for java.io BufferedOutputStream write

List of usage examples for java.io BufferedOutputStream write

Introduction

In this page you can find the example usage for java.io BufferedOutputStream write.

Prototype

@Override
public synchronized void write(int b) throws IOException 

Source Link

Document

Writes the specified byte to this buffered output stream.

Usage

From source file:com.leapfrog.springFramework.Controller.UserDashBoardController.java

@RequestMapping(value = "/display", method = RequestMethod.GET)
public void displayImage(HttpServletRequest request, HttpServletResponse response) {
    try {/*from w w  w  .  jav a  2 s  . c om*/
        String fileName = request.getParameter("image");
        FileInputStream fis = null;
        if (fileName != null) {
            fis = new FileInputStream(new File(fileName));
            BufferedInputStream bis = new BufferedInputStream(fis);
            response.setContentType("text/html");
            BufferedOutputStream output = new BufferedOutputStream(response.getOutputStream());
            for (int data; (data = bis.read()) > -1;) {
                output.write(data);
            }
        }
    } catch (IOException e) {

    } finally {
        // close the streams
    }
}

From source file:com.galois.qrstream.lib.DecodeThread.java

private @NotNull File storeData(Job message) throws IOException {
    File cacheDir = context.getCacheDir();
    File tmpFile = File.createTempFile(Constants.APP_TAG, "", cacheDir);

    // make tmpFile world-readable:
    tmpFile.setReadable(true, false);//from w  w w .j  a v  a 2 s. co  m
    tmpFile.deleteOnExit();

    BufferedOutputStream bos = null;
    try {
        bos = new BufferedOutputStream(new FileOutputStream(tmpFile));
        bos.write(message.getData());
        bos.flush();
    } finally {
        if (null != bos) {
            bos.close();
        }
    }
    return tmpFile;
}

From source file:org.pdfgal.pdfgalweb.utils.impl.FileUtilsImpl.java

@Override
public String saveFile(final MultipartFile file) {

    String name = null;/*w  w w  .j av  a 2s.c  o m*/

    if (!file.isEmpty()) {
        try {
            final String originalName = file.getOriginalFilename();
            name = this.getAutogeneratedName(originalName);
            final byte[] bytes = file.getBytes();
            final BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name)));
            stream.write(bytes);
            stream.close();
        } catch (final Exception e) {
            name = null;// TODO Incluir no log.
        }
    }

    return name;
}

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   ww  w .j a v a2  s  .c  om*/
            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:de.betterform.agent.web.servlet.XFormsRequestURIServlet.java

/**
 * This servlet uses the requestURI to locate and parse a XForms document for processing. The actual processing
 * is done by XFormsFilter. The parsed DOM of the document is passed as a request param to the filter.
 *
 * @param request  servlet request/* www .j  a v  a2s . co  m*/
 * @param response servlet response
 * @throws javax.servlet.ServletException
 * @throws java.io.IOException
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    LOGGER.debug("hit XFormsRequestURIServlet");

    request.setCharacterEncoding("UTF-8");
    WebUtil.nonCachingResponse(response);

    Document doc;

    //locate it
    String formRequestURI = request.getRequestURI().substring(request.getContextPath().length() + 1);
    String realPath = null;
    try {
        realPath = WebFactory.getRealPath(formRequestURI, getServletContext());
    } catch (XFormsConfigException e) {
        throw new ServletException(e);
    }
    File xfDoc = new File(realPath);

    if (request.getHeader("betterform-internal") != null) {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(xfDoc));
        BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());

        int read;
        while ((read = in.read()) > -1) {
            out.write(read);
        }
        out.flush();
    } else {
        try {
            //parse it
            doc = DOMUtil.parseXmlFile(xfDoc, true, false);
        } catch (ParserConfigurationException e) {
            throw new ServletException(e);
        } catch (SAXException e) {
            throw new ServletException(e);
        }

        request.setAttribute(WebFactory.XFORMS_NODE, doc);
        //do the Filter twist
        response.getOutputStream().close();
    }
}

From source file:com.sastix.cms.common.services.htmltopdf.PdfImpl.java

private File saveAs(String path, byte[] document) throws IOException {
    File file = new File(path);

    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
    bufferedOutputStream.write(document);
    bufferedOutputStream.flush();//from   w  w  w  .  j  av  a2s .com
    bufferedOutputStream.close();

    return file;
}

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 {/*from  www . ja v a 2s  .c o 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:equipeDFK.sistemaX.controller.ControllerAgenda.java

/**
 * Mtodo que recebe uma requisio para importar os feriados,
 * ento ler o arquivo CSV e depois persistir os dados no banco
 * @param arquivoCSV//from ww w  .j av a 2s  . co m
 * @param sobrescrever
 * @param req
 * @return String
 * @throws SQLException 
 */
@RequestMapping("openCSV")
public String OpenCSV(MultipartFile arquivoCSV, boolean sobrescrever, HttpServletRequest req)
        throws SQLException {
    GerenciadorDeFeriado gf = new GerenciadorDeFeriado();
    if (!arquivoCSV.isEmpty()) {
        try {
            byte[] b = arquivoCSV.getBytes();
            String caminho = req.getServletContext().getRealPath("/") + arquivoCSV.getOriginalFilename();
            System.out.println(caminho);
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(caminho)));
            stream.write(b);
            stream.close();
            OpenCSV opencsv = new OpenCSV();
            if (sobrescrever) {

            } else {

            }
            gf.importaferiado(opencsv.lerCSV(new File(caminho)), sobrescrever);
            List eventos = gf.listar();
            eventos.stream().forEach((evento) -> {
                System.out.println(evento);
            });
            req.getSession().setAttribute("feriados", eventos);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    return "managerHoliday";
}

From source file:com.xumpy.timesheets.controller.pages.TimesheetsCtrl.java

@RequestMapping(value = "timesheets/saveSQLite")
public String saveSQLiteDB(@RequestParam("file") MultipartFile file) throws IOException {
    if (!file.isEmpty()) {
        BufferedOutputStream stream = new BufferedOutputStream(
                new FileOutputStream(new File("/tmp/timeRecording.db")));
        stream.write(file.getBytes());
        stream.close();// w  w  w  .ja va 2s.c  om
    }

    return "redirect:/timesheets/importTimeRecordings";
}

From source file:com.att.voice.TTS.java

public void say(String text, String file) {

    text = text.replace("\"", "");

    try {//from   w  w w . j  a v a 2  s  .c om

        HttpPost httpPost = new HttpPost("https://api.att.com/speech/v3/textToSpeech");
        httpPost.setHeader("Authorization", "Bearer " + mAuthToken);
        httpPost.setHeader("Accept", "audio/x-wav");
        httpPost.setHeader("Content-Type", "text/plain");
        httpPost.setHeader("Tempo", "-16");
        HttpEntity entity = new StringEntity(text, "UTF-8");

        httpPost.setEntity(entity);
        HttpResponse response = httpclient.execute(httpPost);
        //String result = EntityUtils.toString(response.getEntity());
        HttpEntity result = response.getEntity();

        BufferedInputStream bis = new BufferedInputStream(result.getContent());
        String filePath = System.getProperty("user.dir") + tempFile;
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath)));
        int inByte;
        while ((inByte = bis.read()) != -1) {
            bos.write(inByte);
        }
        bis.close();
        bos.close();

        executeOnCommandLine("afplay " + System.getProperty("user.dir") + "/" + tempFile);
    } catch (Exception ex) {
        System.err.println(ex.getMessage());

    }
}