Example usage for org.apache.commons.io IOUtils copy

List of usage examples for org.apache.commons.io IOUtils copy

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils copy.

Prototype

public static void copy(Reader input, OutputStream output) throws IOException 

Source Link

Document

Copy chars from a Reader to bytes on an OutputStream using the default character encoding of the platform, and calling flush.

Usage

From source file:gerenciador.incubadora.controller.ArquivoController.java

@RequestMapping(value = "/imagem/{id}")
public ModelAndView download(HttpServletResponse response, @PathVariable Long id)
        throws FileNotFoundException, Exception {

    InputStream is = new ByteArrayInputStream(
            FileUtils.readFileToByteArray(new File(Empreendimento.EMPREENDIMENTO_PATH_LOGO_DEFAULT)));

    ModelAndView mv = new ModelAndView("redirect:/empreendedor");
    Empreendimento empreendimento = ServiceLocator.getEmpreendimentoService().readById(id);
    mv.addObject("empreendimento", empreendimento);
    //is.close();
    response.setContentType("image/jpeg");
    IOUtils.copy(is, response.getOutputStream());
    response.flushBuffer();//w  w w.j  a  v a 2  s. c  om
    if (response.isCommitted()) {
        System.out.println("Resposta Comitada!");
    }
    return new ModelAndView("empreendimento/new");
}

From source file:name.martingeisse.ecobuild.util.CommandLineToolInvocation.java

@Override
protected void invoke(List<String> tokens) throws IOException {

    final ProcessBuilder processBuilder = new ProcessBuilder(tokens);
    processBuilder.directory(getWorkingDirectory());
    processBuilder.redirectErrorStream(true);
    final Process process;
    try {/*from w  ww .j  ava  2  s  .co m*/
        process = processBuilder.start();
    } catch (IOException e) {
        getLogger().logError(
                "Exception while starting tool. Command Line: " + getCommand() + " / " + tokens.get(0));
        throw e;
    }
    final MyLoggerWriter loggerWriter = new MyLoggerWriter(getLogger());
    IOUtils.copy(process.getInputStream(), loggerWriter);
    loggerWriter.endStartedLine();
    try {
        process.waitFor();
    } catch (final InterruptedException e) {
        throw new ToolBuildException("An InterruptedException occurred", e);
    }

}

From source file:gob.osinergmin.inpsweb.controller.DocumentoAdjuntoController.java

@RequestMapping(value = "/descargaArchivoAlfresco", method = RequestMethod.GET)
//public @ResponseBody Map<String, Object> descargaArchivoAlfresco(DocumentoAdjuntoDTO filtro){
public void descargaArchivoAlfresco(DocumentoAdjuntoDTO filtro, HttpServletResponse response) {
    LOG.info("procesando descargaArchivoAlfresco--->" + filtro.getNombreArchivo());
    InputStream is = documentoAdjuntoServiceNeg.descargarDatosAlfresco(filtro);

    try {//from  ww  w  .  j ava 2s.c  o m
        if (is == null) {
            response.getWriter().write("Error al insertar Documento");
            return;
        }
        String nombreFichero = filtro.getNombreArchivo();
        response.setHeader("Content-Disposition", "attachment; filename=\"" + nombreFichero + "\"");
        IOUtils.copy(is, response.getOutputStream());
        response.flushBuffer();

    } catch (Exception ex) {
        LOG.info("--->" + ex.getMessage());
        LOG.info("Error writing file to output stream. Filename was '" + filtro.getNombreArchivo() + "'");
        throw new RuntimeException("IOError writing file to output stream");
    }
}

From source file:edu.jhu.hlt.concrete.serialization.TarCompactCommunicationSerializer.java

@Override
public void toTar(Collection<Communication> commColl, Path outPath) throws ConcreteException, IOException {
    try (OutputStream os = Files.newOutputStream(outPath);
            BufferedOutputStream bos = new BufferedOutputStream(os);
            TarArchiveOutputStream tos = new TarArchiveOutputStream(bos);) {
        for (Communication c : commColl) {
            TarArchiveEntry entry = new TarArchiveEntry(c.getId() + ".concrete");
            byte[] cbytes = this.toBytes(c);
            entry.setSize(cbytes.length);
            tos.putArchiveEntry(entry);//  w ww  .ja  v a 2 s .  c o m
            try (ByteArrayInputStream bis = new ByteArrayInputStream(cbytes)) {
                IOUtils.copy(bis, tos);
                tos.closeArchiveEntry();
            }
        }

    } catch (IOException e) {
        throw new ConcreteException(e);
    }
}

From source file:com.accumulobook.designs.rdf.FreebaseExample.java

public static List<Pair<String, String>> getProperties(final String subject) throws Exception {

    ArrayList<Pair<String, String>> properties = new ArrayList<>();

    URL url = new URL("https://www.googleapis.com/freebase/v1/topic/en/" + subject);

    StringWriter writer = new StringWriter();
    IOUtils.copy(url.openStream(), writer);

    String string = writer.toString();

    JSONObject obj = new JSONObject(string);

    JSONObject propArray = obj.getJSONObject("property");

    Iterator iter = propArray.keys();
    while (iter.hasNext()) {
        String propertyName = (String) iter.next();
        JSONObject property = propArray.getJSONObject(propertyName);

        JSONArray values = property.getJSONArray("values");
        for (int i = 0; i < values.length(); i++) {
            properties.add(new Pair(propertyName, values.getJSONObject(i).getString("text")));
        }/*from w  w w . j  a  v a2 s.c  o m*/
    }

    return properties;
}

From source file:com.fishcart.delivery.service.OrderInformer.java

@RequestMapping(value = "/orderhistory", method = { RequestMethod.POST, RequestMethod.GET }, produces = {
        "application/text" })
public void getOrderHistory(@RequestParam(value = "date") String date, HttpServletResponse response)
        throws IOException {
    String data = generator.getOrderHistory(date);
    response.setHeader("Content-Disposition", "attachment; filename=report.csv");
    IOUtils.copy(IOUtils.toInputStream(data, "UTF-8"), response.getOutputStream());
    response.flushBuffer();/*from   w  ww .j  a  v  a  2 s.  c  o  m*/
}

From source file:eu.europa.esig.dss.pdf.DSSPDFUtils.java

/**
 *
 *
 * @param toSignFile//from  w w w . ja  v  a2s . c  o  m
 * @param signedFile
 * @return
 * @throws DSSException
 */
public static FileOutputStream getFileOutputStream(final File toSignFile, final File signedFile)
        throws DSSException {

    FileInputStream fileInputStream = null;
    try {

        final FileOutputStream fileOutputStream = new FileOutputStream(signedFile);
        fileInputStream = new FileInputStream(toSignFile);
        IOUtils.copy(fileInputStream, fileOutputStream);
        return fileOutputStream;
    } catch (IOException e) {
        IOUtils.closeQuietly(fileInputStream);
        throw new DSSException(e);
    }
}

From source file:com.opengamma.web.server.push.rest.ReportMessageBodyWriter.java

/**
 * Writes from {@link Report#getInputStream()} to {@code entityStream}.
 *///w w w  . j  a va 2s.  c  o m
@Override
public void writeTo(Report report, Class<?> type, Type genericType, Annotation[] annotations,
        MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
        throws IOException, WebApplicationException {
    InputStream inputStream = null;
    try {
        inputStream = report.getInputStream();
        IOUtils.copy(inputStream, entityStream);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:com.mindquarry.jcr.xml.source.JCRSourceComplexXmlTest.java

public void testCreateNewXMLFile() throws InvalidNodeTypeDefException, ParseException, Exception {

    JCRNodeSource emptySource = loadTestSource();
    assertEquals(false, emptySource.exists());

    OutputStream sourceOut = emptySource.getOutputStream();
    assertNotNull(sourceOut);/*from  w  w  w . j a  v a  2 s . c  o  m*/

    InputStream contentIn = getClass().getResourceAsStream(CONTENT_FILE);
    try {
        IOUtils.copy(contentIn, sourceOut);
        sourceOut.flush();
    } finally {
        sourceOut.close();
        contentIn.close();
    }

    InputStream expected = getClass().getResourceAsStream(CONTENT_FILE);

    JCRNodeSource persistentSource = loadTestSource();
    assertEquals(true, persistentSource.exists());
    InputStream actual = persistentSource.getInputStream();

    try {
        assertTrue(isXmlEqual(expected, actual));
    } finally {
        expected.close();
        actual.close();
    }
    JCRNodeSource tmpSrc = (JCRNodeSource) resolveSource(BASE_URL + "users/alexander.saar");
    persistentSource.delete();
    tmpSrc.delete();
}

From source file:com.webtide.jetty.load.generator.web.UploadServlet.java

@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //Path tmp = Files.createTempFile( "loadgenerator", ".jetty" );
    //try (OutputStream outputStream = Files.newOutputStream( tmp ))
    //{//from   ww  w  . ja  v  a2 s  .  c om
    IOUtils.copy(req.getInputStream(), resp.getOutputStream());
    //}

}