當前位置: 首頁>>代碼示例>>Java>>正文


Java Resource.getFile方法代碼示例

本文整理匯總了Java中org.springframework.core.io.Resource.getFile方法的典型用法代碼示例。如果您正苦於以下問題:Java Resource.getFile方法的具體用法?Java Resource.getFile怎麽用?Java Resource.getFile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.springframework.core.io.Resource的用法示例。


在下文中一共展示了Resource.getFile方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: executeGroovyScript

import org.springframework.core.io.Resource; //導入方法依賴的package包/類
/**
 * Execute groovy script t.
 *
 * @param <T>          the type parameter
 * @param groovyScript the groovy script
 * @param methodName   the method name
 * @param args         the args
 * @param clazz        the clazz
 * @return the t
 */
public static <T> T executeGroovyScript(final Resource groovyScript,
                                        final String methodName,
                                        final Object[] args,
                                        final Class<T> clazz) {

    if (groovyScript == null || StringUtils.isBlank(methodName)) {
        return null;
    }

    final ClassLoader parent = ScriptingUtils.class.getClassLoader();
    try (GroovyClassLoader loader = new GroovyClassLoader(parent)) {
        final File groovyFile = groovyScript.getFile();
        if (groovyFile.exists()) {                  
            final Class<?> groovyClass = loader.parseClass(groovyFile);
            LOGGER.trace("Creating groovy object instance from class [{}]", groovyFile.getCanonicalPath());

            final GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();

            LOGGER.trace("Executing groovy script's [{}] method, with parameters [{}]", methodName, args);
            final T result = (T) groovyObject.invokeMethod(methodName, args);
            LOGGER.trace("Results returned by the groovy script are [{}]", result);

            if (!clazz.isAssignableFrom(result.getClass())) {
                throw new ClassCastException("Result [" + result
                        + " is of type " + result.getClass()
                        + " when we were expecting " + clazz);
            }
            return result;
        } else {
            LOGGER.trace("Groovy script at [{}] does not exist", groovyScript);
        }
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return null;
}
 
開發者ID:mrluo735,項目名稱:cas-5.1.0,代碼行數:47,代碼來源:ScriptingUtils.java

示例2: getEmailContent

import org.springframework.core.io.Resource; //導入方法依賴的package包/類
public String getEmailContent() {
    //讀取郵件模板,構建發送內容
    String filePath = "emailtemplate.html";//類路徑,編譯後classes目錄下
    Resource resource1 = new ClassPathResource(filePath);
    try {
        File file = resource1.getFile(); //獲取file對象
        String encoding = "UTF-8";
        if (file.isFile() && file.exists()) { //判斷文件是否存在
            InputStreamReader read = new InputStreamReader(
                    new FileInputStream(file), encoding);//考慮到編碼格式
            BufferedReader bufferedReader = new BufferedReader(read);
            String lineTxt = "", temphtml = "";
            while ((lineTxt = bufferedReader.readLine()) != null) {
                temphtml += lineTxt;
            }
            read.close();
            temphtml = temphtml.replace("${toemail}", this.getToEmail());
            temphtml = temphtml.replace("${abstract}", this.getSabstract());
            SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日");//設置日期格式
            temphtml = temphtml.replace("${senddate}", df.format(new Date()));
            temphtml = temphtml.replace("${securityrating}", this.getSecurityrating());
            temphtml = temphtml.replace("${event}", this.getEvent());
            temphtml = temphtml.replace("${name}", this.getName());
            temphtml = temphtml.replace("${content}", this.emailContent);
            SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設置日期格式
            temphtml = temphtml.replace("${datetime}", df2.format(new Date()));
            SimpleDateFormat df3 = new SimpleDateFormat("yyyy");//設置日期格式
            temphtml = temphtml.replace("${year}", df3.format(new Date()));
            return temphtml;
        }
        return emailContent;
    } catch (Exception e) {
        return emailContent;
    }
}
 
開發者ID:NeilRen,項目名稱:NEILREN4J,代碼行數:36,代碼來源:EmailObject.java

示例3: testResourceForTempFile

import org.springframework.core.io.Resource; //導入方法依賴的package包/類
public void testResourceForTempFile() throws Exception {
	Resource res = storage.getResource();
	assertTrue(res.exists());
	File tempFile = res.getFile();
	assertTrue(tempFile.exists());
	assertTrue(tempFile.canRead());
	assertTrue(tempFile.canWrite());
}
 
開發者ID:eclipse,項目名稱:gemini.blueprint,代碼行數:9,代碼來源:FileSystemStorageTest.java

示例4: testDispose

import org.springframework.core.io.Resource; //導入方法依賴的package包/類
public void testDispose() throws Exception {
	Resource res = storage.getResource();
	File file = res.getFile();
	assertTrue(res.exists());
	assertTrue(file.exists());
	storage.dispose();
	assertFalse(res.exists());
	assertFalse(file.exists());
}
 
開發者ID:eclipse,項目名稱:gemini.blueprint,代碼行數:10,代碼來源:FileSystemStorageTest.java

示例5: getTemplateLoaderForPath

import org.springframework.core.io.Resource; //導入方法依賴的package包/類
/**
 * Determine a FreeMarker TemplateLoader for the given path.
 * <p>Default implementation creates either a FileTemplateLoader or
 * a SpringTemplateLoader.
 * @param templateLoaderPath the path to load templates from
 * @return an appropriate TemplateLoader
 * @see freemarker.cache.FileTemplateLoader
 * @see SpringTemplateLoader
 */
protected TemplateLoader getTemplateLoaderForPath(String templateLoaderPath) {
	if (isPreferFileSystemAccess()) {
		// Try to load via the file system, fall back to SpringTemplateLoader
		// (for hot detection of template changes, if possible).
		try {
			Resource path = getResourceLoader().getResource(templateLoaderPath);
			File file = path.getFile();  // will fail if not resolvable in the file system
			if (logger.isDebugEnabled()) {
				logger.debug(
						"Template loader path [" + path + "] resolved to file path [" + file.getAbsolutePath() + "]");
			}
			return new FileTemplateLoader(file);
		}
		catch (IOException ex) {
			if (logger.isDebugEnabled()) {
				logger.debug("Cannot resolve template loader path [" + templateLoaderPath +
						"] to [java.io.File]: using SpringTemplateLoader as fallback", ex);
			}
			return new SpringTemplateLoader(getResourceLoader(), templateLoaderPath);
		}
	}
	else {
		// Always load via SpringTemplateLoader (without hot detection of template changes).
		logger.debug("File system access not preferred: using SpringTemplateLoader");
		return new SpringTemplateLoader(getResourceLoader(), templateLoaderPath);
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:37,代碼來源:FreeMarkerConfigurationFactory.java

示例6: addListWords

import org.springframework.core.io.Resource; //導入方法依賴的package包/類
public void addListWords(String filename, String content) throws Exception {
    Resource resource = new ClassPathResource("dicts/"  + filename + ".txt");
    File file = resource.getFile();
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8"));
    out.write("\r\n" + content);
    out.close();
}
 
開發者ID:Jianfu-She,項目名稱:SensitiveWordFilter,代碼行數:8,代碼來源:WordEditService.java

示例7: showListWords

import org.springframework.core.io.Resource; //導入方法依賴的package包/類
public String showListWords(String filename) throws Exception {
    StringBuilder result = new StringBuilder();
    Resource resource = new ClassPathResource("dicts/"  + filename + ".txt");
    File file = resource.getFile();
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
    String lineTxt;
    while((lineTxt = br.readLine())!=null) {
        result.append(lineTxt).append(",");
    }
    br.close();
    return result.toString();
}
 
開發者ID:Jianfu-She,項目名稱:SensitiveWordFilter,代碼行數:13,代碼來源:WordEditService.java

示例8: resourceModifiedTime

import org.springframework.core.io.Resource; //導入方法依賴的package包/類
public static Date resourceModifiedTime(Resource resource) {
    try {
        File file = resource.getFile();
        if (file != null) {
            return new Date(file.lastModified());
        }
    } catch (IOException e) {
        // ignore it;
    }
    return null;
}
 
開發者ID:FastBootWeixin,項目名稱:FastBootWeixin,代碼行數:12,代碼來源:WxMediaUtils.java

示例9: JsonResourceSurrogateAuthenticationService

import org.springframework.core.io.Resource; //導入方法依賴的package包/類
public JsonResourceSurrogateAuthenticationService(final Resource json) throws Exception {
    this(json.getFile());
}
 
開發者ID:mrluo735,項目名稱:cas-5.1.0,代碼行數:4,代碼來源:JsonResourceSurrogateAuthenticationService.java

示例10: initVelocityResourceLoader

import org.springframework.core.io.Resource; //導入方法依賴的package包/類
/**
 * Initialize a Velocity resource loader for the given VelocityEngine:
 * either a standard Velocity FileResourceLoader or a SpringResourceLoader.
 * <p>Called by {@code createVelocityEngine()}.
 * @param velocityEngine the VelocityEngine to configure
 * @param resourceLoaderPath the path to load Velocity resources from
 * @see org.apache.velocity.runtime.resource.loader.FileResourceLoader
 * @see SpringResourceLoader
 * @see #initSpringResourceLoader
 * @see #createVelocityEngine()
 */
protected void initVelocityResourceLoader(VelocityEngine velocityEngine, String resourceLoaderPath) {
	if (isPreferFileSystemAccess()) {
		// Try to load via the file system, fall back to SpringResourceLoader
		// (for hot detection of template changes, if possible).
		try {
			StringBuilder resolvedPath = new StringBuilder();
			String[] paths = StringUtils.commaDelimitedListToStringArray(resourceLoaderPath);
			for (int i = 0; i < paths.length; i++) {
				String path = paths[i];
				Resource resource = getResourceLoader().getResource(path);
				File file = resource.getFile();  // will fail if not resolvable in the file system
				if (logger.isDebugEnabled()) {
					logger.debug("Resource loader path [" + path + "] resolved to file [" + file.getAbsolutePath() + "]");
				}
				resolvedPath.append(file.getAbsolutePath());
				if (i < paths.length - 1) {
					resolvedPath.append(',');
				}
			}
			velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
			velocityEngine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, "true");
			velocityEngine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, resolvedPath.toString());
		}
		catch (IOException ex) {
			if (logger.isDebugEnabled()) {
				logger.debug("Cannot resolve resource loader path [" + resourceLoaderPath +
						"] to [java.io.File]: using SpringResourceLoader", ex);
			}
			initSpringResourceLoader(velocityEngine, resourceLoaderPath);
		}
	}
	else {
		// Always load via SpringResourceLoader
		// (without hot detection of template changes).
		if (logger.isDebugEnabled()) {
			logger.debug("File system access not preferred: using SpringResourceLoader");
		}
		initSpringResourceLoader(velocityEngine, resourceLoaderPath);
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:52,代碼來源:VelocityEngineFactory.java

示例11: resolveAsArchive

import org.springframework.core.io.Resource; //導入方法依賴的package包/類
private Archive resolveAsArchive(Resource app) throws IOException {
		File moduleFile = app.getFile();
		return moduleFile.isDirectory() ? new ExplodedArchive(moduleFile) : new JarFileArchive(moduleFile);
}
 
開發者ID:spring-cloud,項目名稱:spring-cloud-dashboard,代碼行數:5,代碼來源:BootApplicationConfigurationMetadataResolver.java


注:本文中的org.springframework.core.io.Resource.getFile方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。