我是靠谱客的博主 愤怒绿草,这篇文章主要介绍简单的bean利用spring得到服务器上的路径,现在分享给大家,希望可以做个参考。

利用spring中的ServletContextAware 接口获得ServletContext ,从而获得服务器上下文路径

程序片段:

package com.ahtec.oa.service.impl; import java.io.File; import java.io.IOException; import javax.servlet.ServletContext; import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.context.ServletContextAware; /** * @author Wu */ @Component public class AutoRunServiceImpl implements ServletContextAware { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); private ServletContext servletContext; public void setServletContext(ServletContext servletContext) { this.servletContext = servletContext; } public void deletePdcaZipFile() { logger.debug("start deletePdcaZipFile ..."); String pdcaDeleteFileLocation = ""; try { String fileLocation = "files//pdca//"; String ctxDir = this.servletContext.getRealPath(String.valueOf(File.separatorChar)); if (!ctxDir.endsWith(String.valueOf(File.separatorChar))) { ctxDir = ctxDir + File.separatorChar; } pdcaDeleteFileLocation = ctxDir + fileLocation; FileUtils.deleteDirectory(new File(pdcaDeleteFileLocation)); logger.debug("has deleted : '{}' all files!", pdcaDeleteFileLocation); logger.debug("end deletePdcaZipFile..."); } catch (IOException e) { logger.debug("do not find pdcaDeleteFileLocation: {}", pdcaDeleteFileLocation); } logger.debug("============================================================"); } }

最后

以上就是愤怒绿草最近收集整理的关于简单的bean利用spring得到服务器上的路径的全部内容,更多相关简单内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(134)

评论列表共有 0 条评论

立即
投稿
返回
顶部