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:com.zilverline.MockHandler.java

@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    invariant();/*from  ww  w. ja v a  2  s .c om*/
    ServletOutputStream outputStream = response.getOutputStream();
    IOUtils.copy(responseResource.getInputStream(), outputStream);
    outputStream.flush();
}

From source file:net.genesishub.gFeatures.Feature.gWarsSuiteOld.CrackshotConfiguration.java

public void MakeFile(String filename) throws IOException {
    Reader paramReader = new InputStreamReader(
            getClass().getResourceAsStream("/tk/genesishub/gFeatures/gWarsSuite/WeaponConfig/" + filename));
    StringWriter writer = new StringWriter();
    IOUtils.copy(paramReader, writer);
    String theString = writer.toString();
    File f = new File("plugins/CrackShot/weapons/" + filename + ".yml");
    f.createNewFile();/* w w  w.j av  a2  s  .c  o  m*/
    BufferedWriter bw = new BufferedWriter(new FileWriter(f));
    bw.write(theString);
    bw.close();
}

From source file:com.digitalizat.control.TdocController.java

@RequestMapping(value = "obtenerFichero/{codigo}")
public void obtenerFichero(@PathVariable(value = "codigo") String codigo, HttpServletResponse response)
        throws FileNotFoundException, IOException, Exception {

    Document doc = tdocManager.getDocument(Integer.valueOf(codigo));

    response.setContentType("application/pdf");
    response.setHeader("Content-Disposition", "attachment; filename=" + doc.getFileName().replace(" ", ""));

    InputStream is = new FileInputStream(doc.getBasePath() + doc.getFileName());

    IOUtils.copy(is, response.getOutputStream());

    response.flushBuffer();//from  w  w  w . j a v  a 2 s.  c  o  m

}

From source file:frameworkcontentspeed.Utils.SendEmailAtachament.java

public static void sendGRUP(String from, String password, String bcc, String sub, String msg, String filename)
        throws Exception {
    //Get properties object    
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
    //get Session   
    Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(from, password);
        }/*  www .  j  a  va  2 s .c  o  m*/
    });
    //compose message    
    try {
        MimeMessage message = new MimeMessage(session);
        //message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));

        message.setSubject(sub);
        message.setText(msg);

        BodyPart messageBodyPart = new MimeBodyPart();

        messageBodyPart.setText("Raport teste automat");

        Multipart multipart = new MimeMultipart();

        multipart.addBodyPart(messageBodyPart);

        messageBodyPart = new MimeBodyPart();

        DataSource source = new FileDataSource(filename);

        messageBodyPart.setDataHandler(new DataHandler(source));

        messageBodyPart.setFileName(filename);

        multipart.addBodyPart(messageBodyPart);

        //message.setContent(multipart);
        StringWriter writer = new StringWriter();
        IOUtils.copy(new FileInputStream(new File(filename)), writer);

        message.setContent(writer.toString(), "text/html");

        //send message  
        Transport.send(message);
        System.out.println("message sent successfully");
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }

}

From source file:io.inkstand.it.StaticContentITCase.java

private static File createZip(final File file, final String... resources) throws IOException {

    try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream((file)))) {

        for (String resourceName : resources) {

            final InputStream is = StaticContentITCase.class.getResourceAsStream(resourceName);
            final ZipEntry zipEntry = new ZipEntry(resourceName);
            zos.putNextEntry(zipEntry);//from  w w w.  j a va  2s.  co m
            IOUtils.copy(is, zos);
            zos.closeEntry();
        }
    }
    return file;
}

From source file:io.milton.cloud.server.web.InputStreamResource.java

@Override
public void sendContent(OutputStream out, Range range, Map<String, String> params, String contentType)
        throws IOException, NotAuthorizedException, BadRequestException {
    try {//from   www .j  av a 2 s .  c o m
        IOUtils.copy(content, out);
    } catch (NullPointerException npe) {
        log.debug("NullPointerException, this is often expected");
    }
}

From source file:net.sf.jsignpdf.utils.FontUtils.java

/**
 * Returns BaseFont for text of visible signature;
 * /*from ww w  .  j a  v  a2 s  . c o m*/
 * @return
 */
public static synchronized BaseFont getL2BaseFont() {
    if (l2baseFont == null) {
        final ConfigProvider conf = ConfigProvider.getInstance();
        try {
            final ByteArrayOutputStream tmpBaos = new ByteArrayOutputStream();
            String fontPath = conf.getNotEmptyProperty("font.path", null);
            String fontName;
            String fontEncoding;
            InputStream tmpIs;
            if (fontPath != null) {
                fontName = conf.getNotEmptyProperty("font.name", null);
                if (fontName == null) {
                    fontName = new File(fontPath).getName();
                }
                fontEncoding = conf.getNotEmptyProperty("font.encoding", null);
                if (fontEncoding == null) {
                    fontEncoding = BaseFont.WINANSI;
                }
                tmpIs = new FileInputStream(fontPath);
            } else {
                fontName = Constants.L2TEXT_FONT_NAME;
                fontEncoding = BaseFont.IDENTITY_H;
                tmpIs = FontUtils.class.getResourceAsStream(Constants.L2TEXT_FONT_PATH);
            }
            IOUtils.copy(tmpIs, tmpBaos);
            tmpIs.close();
            tmpBaos.close();
            l2baseFont = BaseFont.createFont(fontName, fontEncoding, BaseFont.EMBEDDED, BaseFont.CACHED,
                    tmpBaos.toByteArray(), null);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                l2baseFont = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
            } catch (Exception ex) {
                // where is the problem, dear Watson?
            }
        }
    }
    return l2baseFont;
}

From source file:ee.ria.xroad.common.CustomAttachmentWriter.java

@Override
protected void writeAttachment() throws IOException {
    IOUtils.copy(attachmentInputStream, mpos);
}

From source file:com.scorpio4.util.io.JarArchiver.java

public String add(InputStream inputStream, String filename, String comment, long size)
        throws IOException, NoSuchAlgorithmException {
    if (jarOutputStream == null)
        throw new IOException("not open");
    JarEntry entry = new JarEntry(filename);
    entry.setSize(size);/*w ww  . ja va  2 s  .  co  m*/

    entry.setMethod(JarEntry.DEFLATED); // compressed
    entry.setTime(System.currentTimeMillis());
    if (comment != null)
        entry.setComment(comment);
    jarOutputStream.putNextEntry(entry);

    // copy I/O & return SHA-1 fingerprint
    String fingerprint = Fingerprint.copy(inputStream, jarOutputStream, 4096);
    IOUtils.copy(inputStream, jarOutputStream);
    log.debug("JAR add " + size + " bytes: " + filename + " -> " + fingerprint);

    jarOutputStream.closeEntry();
    //      jarOutputStream.finish();
    return fingerprint;
}

From source file:com.doculibre.constellio.servlets.DownloadFileServlet.java

@Override
public final void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String file = request.getParameter("file");
    String mimetype = request.getParameter("mimetype");

    String filePathFound = getFilePathFor(file);
    if (filePathFound == null) {
        response.sendError(404);/*from   w  w  w.  j  a  va  2s .c  o  m*/
    } else {
        File fileFound = new File(filePathFound);
        response.setContentType((mimetype != null) ? mimetype : "application/octet-stream");
        response.setContentLength((int) fileFound.length());
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileFound.getName() + "\"");

        InputStream is = new BufferedInputStream(new FileInputStream(fileFound));
        IOUtils.copy(is, response.getOutputStream());
    }
}