本文整理汇总了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;
}
示例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;
}
}
示例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());
}
示例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());
}
示例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);
}
}
示例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();
}
示例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();
}
示例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;
}
示例9: JsonResourceSurrogateAuthenticationService
import org.springframework.core.io.Resource; //导入方法依赖的package包/类
public JsonResourceSurrogateAuthenticationService(final Resource json) throws Exception {
this(json.getFile());
}
示例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);
}
}
示例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