spring MVC 获取servletContext,实现文件下载功能

以下是获取servletContext:

import javax.servlet.ServletContext;

import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

/**
 * ServletContext辅助类。提供springmvc获取servletContext对象及项目真实路径的静态方法
 * @author Administrator
 *
 */
public class ServletContextUtils {

    private ServletContextUtils() {

    }

    /**
     * 获取ServletContext对象
     * @return ServletContext对象
     */
    public static ServletContext getServletContext() {
        WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
        ServletContext context = webApplicationContext.getServletContext();
        return context;
    }

    /**
     * 根据folder获取文件的真实路径
     * @param folder 要获取文件夹的真实路径
     * @return folder的真实路径
     */
    public static String getRealPath(String folder) {
        ServletContext context = getServletContext();
        String path = context.getRealPath(folder);
        return path;
    }
}

以下是文件下载代码:

@RequestMapping("/{filename}")
    public ResponseEntity<byte[]> download(@PathVariable String filename) throws IOException {
        ResponseEntity<byte[]> entity = null;
        try {
            HttpHeaders headers = new HttpHeaders();
            String pathname = getFilepath(filename);
            File file = new File(pathname);
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            entity = new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
            return entity;
        } catch (IOException e) {
            logger.error(e.getMessage());
            throw e;
        }
    }

    private String getFilepath(String filename) {
        String pathname = ServletContextUtils.getRealPath("/file") + "\\" + filename + ".txt";
        return pathname;
    }

springmvc4 以上版本已经实现与servlet的低耦合,不知道为什么很多人写代码用的也是springMVC4或者5,仍然在使用httprequest。

转载于:https://blog.51cto.com/14220961/2365599

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC中,你可以使用监听器(Listener)来监听请求和响应的事件。Spring MVC提供了一些内置的监听器,你可以使用它们来拦截和处理请求。以下是一些常用的监听器: 1. ContextLoaderListener:这是一个ServletContext监听器,用于加载Spring应用程序上下文。它可以在应用程序启动时初始化Spring容器,并在应用程序关闭时进行资源清理。 2. RequestContextListener:这是一个ServletRequest监听器,用于绑定当前请求的Locale和Theme到Spring的上下文中。它允许你在控制器和视图中访问当前请求的Locale和Theme。 3. HttpSessionListener:这是一个HttpSession监听器,用于跟踪会话的创建和销毁。你可以通过实现该监听器来执行自定义的会话管理逻辑。 4. ServletRequestListener:这是一个ServletRequest监听器,用于跟踪请求的创建和销毁。你可以通过实现该监听器来执行自定义的请求处理逻辑。 要使用这些监听器,你需要在web.xml文件中进行配置。例如,以下是在web.xml中配置ContextLoaderListener的示例: ```xml <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> ``` 请注意,以上示例中的类名是一个示例,具体的类名可能会因你使用的Spring版本而有所不同。你需要根据你的Spring版本选择正确的类名。 希望这能帮到你!如果你还有其他问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值