本文整理匯總了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