Example usage for org.springframework.core.io Resource getInputStream

List of usage examples for org.springframework.core.io Resource getInputStream

Introduction

In this page you can find the example usage for org.springframework.core.io Resource getInputStream.

Prototype

InputStream getInputStream() throws IOException;

Source Link

Document

Return an InputStream for the content of an underlying resource.

Usage

From source file:org.jasig.schedassist.impl.InitializeSchedulingAssistantDatabase.java

/**
 * Optionally accepts 1 argument./* www  . j  a va  2  s. co m*/
 * If the argument evaluates to true ({@link Boolean#parseBoolean(String)}, the main method
 * will NOT run the SQL statements in the "destroyDdl" Resource.
 * 
 * @param args
 */
public static void main(String[] args) throws Exception {
    LOG.info("loading applicationContext: " + CONFIG);
    ApplicationContext context = new ClassPathXmlApplicationContext(CONFIG);

    boolean firstRun = false;
    if (args.length == 1) {
        firstRun = Boolean.parseBoolean(args[0]);
    }
    InitializeSchedulingAssistantDatabase init = new InitializeSchedulingAssistantDatabase();
    init.setDataSource((DataSource) context.getBean("dataSource"));

    if (!firstRun) {
        Resource destroyDdl = (Resource) context.getBean("destroyDdl");
        if (null != destroyDdl) {
            String destroySql = IOUtils.toString(destroyDdl.getInputStream());
            init.executeDdl(destroySql);
            LOG.warn("existing tables removed");
        }
    }

    Resource createDdl = (Resource) context.getBean("createDdl");
    String createSql = IOUtils.toString(createDdl.getInputStream());

    init.executeDdl(createSql);
    LOG.info("database initialization complete");
}

From source file:org.spring.resource.FileSourceExample.java

public static void main(String[] args) {
    try {/*from   ww w.j a v  a2s  . co  m*/
        String filePath = "E:\\JEELearning\\ideaworlplace\\springdream\\springdream.ioc\\src\\main\\resources\\FileSource.txt";
        // ?
        Resource res1 = new FileSystemResource(filePath);
        // ?
        Resource res2 = new ClassPathResource("FileSource.txt");

        System.out.println("?:" + res1.getFilename());
        System.out.println("?:" + res2.getFilename());

        InputStream in1 = res1.getInputStream();
        InputStream in2 = res2.getInputStream();
        //            System.out.println("?:" + read(in1));
        //            System.out.println("?:" + read(in2));
        System.out.println("?:" + new String(FileCopyUtils.copyToByteArray(in1)));
        System.out.println("?:" + new String(FileCopyUtils.copyToByteArray(in2)));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:net.nicoll.boot.metadata.Sample.java

private static List<InputStream> getResources() throws IOException {
    Resource[] resources = new PathMatchingResourcePatternResolver()
            .getResources("classpath*:/META-INF/spring-configuration-metadata.json");
    List<InputStream> result = new ArrayList<InputStream>();
    for (Resource resource : resources) {
        result.add(resource.getInputStream());
    }//from   ww  w.ja  va  2s  .co  m
    return result;
}

From source file:com.jaxio.celerio.util.XsdHelper.java

public static String getResourceContentAsString(String resourcePath) {
    PathMatchingResourcePatternResolver o = new PathMatchingResourcePatternResolver();

    try {//ww  w  .jav  a 2  s  . c  o m
        Resource packInfosAsResource[] = o.getResources(resourcePath);
        for (Resource r : packInfosAsResource) {
            return IOUtils.toString(r.getInputStream());
        }
        return null;
    } catch (IOException ioe) {
        throw new RuntimeException("Error while searching for : " + resourcePath, ioe);
    }
}

From source file:com.otz.transport.common.config.ContentUtil.java

public static String getStringContent(ApplicationContext context, String fileName) throws IOException {

    Resource resource = context.getResource("classpath:/" + fileName);
    int availableByte = resource.getInputStream().available();
    byte[] contentBytes = new byte[availableByte];
    resource.getInputStream().read(contentBytes);
    return new String(contentBytes);
}

From source file:ar.com.zauber.commons.spring.web.utils.FailsafeResourceManifestProvider.java

/** */
public static InputStream getInputStream(final Resource resouce) {
    try {/*w  w  w .  j  a  v  a 2  s  .c  om*/
        return resouce == null ? null : resouce.getInputStream();
    } catch (final IOException e) {
        return null;
    }
}

From source file:org.zalando.github.spring.AbstractTemplateTest.java

public static String resourceToString(Resource resource) throws IOException {
    return StreamUtils.copyToString(resource.getInputStream(), Charset.defaultCharset());
}

From source file:com.jdy.ddj.common.utils.PropertiesLoader.java

/**
 * , Spring Resource?.// w  ww  .j  av  a  2 s. co m
 */
public static Properties loadProperties(String... resourcesPaths) throws IOException {
    Properties props = new Properties();

    for (String location : resourcesPaths) {

        logger.debug("Loading properties file from:" + location);

        InputStream is = null;
        try {
            Resource resource = resourceLoader.getResource(location);
            is = resource.getInputStream();
            props.load(is);
        } catch (IOException ex) {
            logger.info("Could not load properties from path:" + location + ", " + ex.getMessage());
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    return props;
}

From source file:com.github.dactiv.common.utils.PropertiesUtils.java

/**
 * properties, ???./*w ww . java2s. c o  m*/
 * Spring Resource?, ?UTF-8.
 * 
 * @param resourcesPaths Spring Resource path
 */
public static Properties loadProperties(String... resourcesPaths) {
    Properties props = new Properties();

    for (String location : resourcesPaths) {

        logger.debug("Loading properties file from:" + location);

        InputStream is = null;
        try {
            Resource resource = resourceLoader.getResource(location);
            is = resource.getInputStream();
            propertiesPersister.load(props, new InputStreamReader(is, DEFAULT_ENCODING));
        } catch (IOException ex) {
            logger.info("Could not load properties from classpath:" + location + ": " + ex.getMessage());
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    return props;
}

From source file:com.gc.core.framework.utils.PropertiesUtils.java

/**
 * properties, ???./* w w  w  .jav  a 2s  . c  o  m*/
 * Spring Resource?, ?UTF-8.
 * 
 * @see org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
 */
public static Properties loadProperties(String... resourcesPaths) throws IOException {
    Properties props = new Properties();

    for (String location : resourcesPaths) {

        logger.debug("Loading properties file from:" + location);

        InputStream is = null;
        try {
            Resource resource = resourceLoader.getResource(location);
            is = resource.getInputStream();
            propertiesPersister.load(props, new InputStreamReader(is, DEFAULT_ENCODING));
        } catch (IOException ex) {
            logger.info("Could not load properties from classpath:" + location + ": " + ex.getMessage());
        } finally {
            if (is != null) {
                is.close();
            }
        }
    }
    return props;
}