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.jalios.ejpt.sync.utils.IOUtil.java

/**
 * Copy f1 into f2 (mkdirs for f2)//from  w ww  .  jav  a 2  s  .c o  m
 * 
 * @param f1
 *          the source file
 * @param f2
 *          the target file
 * @throws IOException
 */
public static void copyFile(File f1, File f2) throws IOException {
    if (f1 == null || f2 == null) {
        throw new IllegalArgumentException("f1 and f2 arguments must not be null");
    }

    // Create target directories
    if (f2.getParentFile() != null) {
        f2.getParentFile().mkdirs();
    }

    // Copy
    InputStream input = null;
    OutputStream output = null;
    try {
        input = new FileInputStream(f1);
        output = new FileOutputStream(f2);
        IOUtils.copy(input, output);
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException ex) {
            }
        }
        if (output != null) {
            try {
                output.close();
            } catch (IOException ex) {
            }
        }
    }
    f2.setLastModified(f1.lastModified());

}

From source file:com.netflix.hystrix.dashboard.stream.EurekaInfoServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String uri = request.getParameter("url");
    if (uri == null || "".equals(uri))
        response.getOutputStream().write("Error. You need supply a valid eureka URL ".getBytes());

    try {/*from w w  w  .  ja v  a2s  .  co  m*/
        response.setContentType("application/xml");
        response.setHeader("Content-Encoding", "gzip");
        IOUtils.copy(UrlUtils.readXmlInputStream(uri), response.getOutputStream());
    } catch (Exception e) {
        response.getOutputStream()
                .write(("Error. You need supply a valid eureka URL. Ex: " + e + "").getBytes());
    }

}

From source file:at.asitplus.regkassen.core.base.util.CashBoxUtils.java

/**
 * Helper method for storing printed PDF receipts to files
 *
 * @param printedReceipts binary representation of receipts to be stored
 * @param prefix          prefix for file names
 * @param baseDir         base directory, where files should be written
 *//*w  ww . j  a  v  a2  s  .c  om*/
public static void writeReceiptsToFiles(List<byte[]> printedReceipts, String prefix, File baseDir) {
    try {
        int index = 1;
        for (byte[] printedReceipt : printedReceipts) {
            ByteArrayInputStream bIn = new ByteArrayInputStream(printedReceipt);
            File receiptFile = new File(baseDir, prefix + "Receipt " + index + ".pdf");
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
                    new FileOutputStream(receiptFile));
            IOUtils.copy(bIn, bufferedOutputStream);
            bufferedOutputStream.close();
            index++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.pm.myshop.controller.FilesController.java

@RequestMapping("/pictures/{thumb}/{fileName}")
@ResponseBody/*from w w  w .j a  va  2  s . co m*/
public void viewFiles(@PathVariable("fileName") String fileName, @PathVariable("thumb") String thumb,
        HttpServletRequest request, HttpServletResponse response) throws IOException {
    try {

        String path = request.getRealPath("/");

        response.setContentType("image/jpeg");

        InputStream inputStream = new FileInputStream(path + "../../files/" + thumb + "/" + fileName + ".jpg");
        IOUtils.copy(inputStream, response.getOutputStream());
        response.flushBuffer();

    } catch (IOException ex) {
        System.out.println(ex.getMessage());
        response.getWriter().print("File Not Found");
    }

}

From source file:com.youTransactor.uCube.LogManager.java

public static void getLogs(OutputStream out) throws IOException {
    File logDir = context.getDir(LOG_DIR, Context.MODE_PRIVATE);

    if (logDir.listFiles().length > 0) {
        ZipOutputStream zout = new ZipOutputStream(out);

        for (File file : logDir.listFiles()) {
            ZipEntry entry = new ZipEntry(file.getName());
            zout.putNextEntry(entry);//from   www  .j  av  a 2 s  .c o  m
            IOUtils.copy(new FileInputStream(file), zout);
            zout.closeEntry();
        }

        zout.close();
    }
}

From source file:com.ewcms.web.filter.render.ResourceRender.java

/**
 * ?//  w w  w.jav  a  2s. c  o m
 * 
 * @param response
 * @param uri   ?
 * @return
 * @throws IOException
 */
@Override
protected boolean output(HttpServletResponse response, String uri) throws IOException {
    //TODO windowUnix
    Resource resource = resourceService.getResourceByUri(uri);
    if (resource == null) {
        logger.debug("Resource is not exist,uri is {}", uri);
        return false;
    }
    String realPath = resource.getPath();
    if (StringUtils.endsWith(resource.getThumbUri(), uri)) {
        realPath = resource.getThumbPath();
    }

    try {
        IOUtils.copy(new FileInputStream(realPath), response.getOutputStream());
    } catch (Exception e) {
        logger.warn("Resource is not exit,real path is{}", realPath);
    }
    response.flushBuffer();

    return true;
}

From source file:com.ettrema.httpclient.Utils.java

public static HttpResult executeHttpWithResult(HttpClient client, HttpUriRequest m, OutputStream out)
        throws IOException {
    HttpResponse resp = client.execute(m);
    HttpEntity entity = resp.getEntity();
    if (entity != null) {
        InputStream in = null;// w w  w  .j  av a2  s .  c o  m
        try {
            in = entity.getContent();
            if (out != null) {
                IOUtils.copy(in, out);
            }
        } finally {
            IOUtils.closeQuietly(in);
        }
    }
    Map<String, String> mapOfHeaders = new HashMap<String, String>();
    for (Header h : resp.getAllHeaders()) {
        mapOfHeaders.put(h.getName(), h.getValue()); // TODO: should concatenate multi-valued headers
    }
    HttpResult result = new HttpResult(resp.getStatusLine().getStatusCode(), mapOfHeaders);
    return result;
}

From source file:com.tango.BucketSyncer.S32GCSTestFile.java

public S32GCSTestFile() throws Exception {
    file = File.createTempFile(getClass().getName(), ".tmp");
    data = S32GCSMirrorTest.random(TEST_FILE_SIZE + (RandomUtils.nextInt() % 1024));
    @Cleanup/*  w  ww . ja va 2  s. c o  m*/
    FileOutputStream out = new FileOutputStream(file);
    IOUtils.copy(new ByteArrayInputStream(data.getBytes()), out);
    file.deleteOnExit();
}

From source file:com.seleniumtests.it.driver.support.server.PageServlet.java

/**
  * Allow downloading of files in upload folder
  *//*from  ww  w .j  a  va  2  s  .  c o m*/
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    try {
        if (this.resourceFile.endsWith(".css")) {
            resp.addHeader("Content-Type", "text/css ");
        }
        IOUtils.copy(getClass().getResourceAsStream(this.resourceFile), resp.getOutputStream());
    } catch (IOException e) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Error while handling request: " + e.getMessage());
    }
}

From source file:com.comcast.magicwand.spells.web.chrome.ChromePhoenixDriver.java

/**
 * Gets the latest known version of chromedriver.
 * /*from   w w w .  ja  v  a 2  s .co m*/
 * @return Latest known version
 */
public static String getLatestVersion() {
    String latest = LATEST_KNOWN_VERSION;

    try {
        URL url = new URL(LATEST_RELEASE_URL);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        IOUtils.copy(url.openStream(), baos);
        latest = baos.toString();
    } catch (IOException e) {
        LOG.error("Error retrieving url[{}]: {}", LATEST_RELEASE_URL, e);
    }
    return latest;
}