Creating a simple static file server with Rewrite--reference
Today, I’d like to take a quick moment to demonstrate how to make a simple file server using Rewrite, and any Servlet Container, such as Tomcat, Wildfly, or Jetty. This can enable much easier file updates for static content, such as preventing the need to re-deploy an entire application just to update an image, or document.
Rewrite is an open-source Routing ↑↓ and /url/{rewriting} solution for Servlet, Java Web Frameworks, and Java EE.
To start, you’ll need to include the Rewrite dependencies in your project. If you’re using maven, this is as simple as making sure your POM has the following entries (You’ll also need the Servlet API):
<dependency>
<groupId>org.ocpsoft.rewrite</groupId>
<artifactId>rewrite-servlet</artifactId>
<version>2.0.8.Final</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
Next, we’ll create a simple ConfigurationProvider to tell Rewrite what to do; in this case, we’ll be streaming files directly from the local filesystem. This class can go anywhere in your project:
@RewriteConfiguration
public class StaticContentConfiguration extends HttpConfigurationProvider {
@Override
public Configuration getConfiguration(ServletContext context) {
/*
* You should select a path that exposes only the directory(s) from which content should be served, otherwise the
* entire hard drive could be accessible.
*/
File resource = new File("/home/contentserv/{file}"); return ConfigurationBuilder.begin()
.addRule()
.when(Path.matches("/{file}")
.and(Filesystem.fileExists(resource))
.and(Direction.isInbound())
)
.perform(Stream.from(resource)
.and(Response.setStatus(200))
.and(Response.complete())
)
.where("file").matches(".*");
} @Override
public int priority() {
return 0;
}
}
You could even add logging using the provided `Log` operation from rewrite-servlet, so that requests can be tracked in your server logs:
.perform(Stream.from(resource)
.and(Response.setStatus(200))
.and(Response.complete())
.and(Log.message(Level.INFO, "Served file: {file}."))
)
It’s as simple as that! Rewrite will match inbound requests with corresponding files in the specified resource location. Thanks to the simple parameterization API that Rewrite provides, this requires very little glue code – we are free to sit back and have a nice beverage.
Now you’ve not got an extremely simple file-server running as a web-application. This is only one very basic example of what Rewrite can do, so let us know what you think as you explore more Rewritefeatures or dive into the documentation!
原文:http://ocpsoft.org/java/servlet-java/creating-a-simple-static-file-server-with-rewrite/
Creating a simple static file server with Rewrite--reference的更多相关文章
- [ASP.NET Core] Static File Middleware
前言 本篇文章介绍ASP.NET Core里,用来处理静态档案的Middleware,为自己留个纪录也希望能帮助到有需要的开发人员. ASP.NET Core官网 结构 一个Web站台最基本的功能,就 ...
- Static File Middleware
[ASP.NET Core] Static File Middleware 前言 本篇文章介绍ASP.NET Core里,用来处理静态档案的Middleware,为自己留个纪录也希望能帮助到有需要 ...
- Asp.net core 学习笔记 ( IIS, static file 性能优化 )
更新 : 2019-02-06 最后还是把 rewrite 给替换掉了. 所以 rewrite url 也不依赖 iis 了咯. refer : https://docs.microsoft.com/ ...
- Simple Web API Server in Golang (1)
To be an better Gopher, get your hands dirty. Topcoder offered a serials of challenges for learning ...
- Creating a Simple Web Service and Client with JAX-WS
Creating a Simple Web Service and Client with JAX-WS 发布服务 package cn.zno.service.impl; import javax. ...
- Digital Audio - Creating a WAV (RIFF) file
Abstract:This tutorial covers the creation of a WAV (RIFF) audio file. It covers bit size, sample ra ...
- How to setup vsftpd FTP file Server on Redhat 7 Linux
Forward from: https://linuxconfig.org/how-to-setup-vsftpd-ftp-file-server-on-redhat-7-linux How to s ...
- java.lang.ClassFormatError: Illegal UTF8 string in constant pool in class file Server/Request
Linux服务器上,将本地编译好的文件上传后,Tomcat启动时报错: Exception in thread "Thread-2" java.lang.ClassFormatEr ...
- Http File Server小工具
一般情况下,在做一些测试(比如下载服务)的时候需要提供一个http文件下载服务. 下面这个轻量级的工具HFS可以在本地提供http服务: 官网地址传送门:Http File Server
随机推荐
- null和undefined的区别
不同之处: null是js语言的关键字,它表示一个特殊值,常用来描述“空值”.对null执行typeof运算,结果返回字符串“object”,也就是说,可以将null认为是一个特殊的对象值,含义是“非 ...
- [C#]获取最近在Windows上所使用的文件
class RecentlyFileHelper { public static string GetShortcutTargetFile(string shortcutFilename) { var ...
- php之文件上传简单介绍
要声明的form表单格式 <form action="act.php" method="post" enctype="multipart/for ...
- 在CMD下用java命令出现“找不到或无法加载主类”问题
解决思路: 从网上查找原因和解决方法,有提到环境变量classpath设置问题,但多次尝试问题依旧没有解决.然后使用java -cp %classpath; Hello执行,结果正确. 使用echo ...
- 当INPUT 连续输入是连续触发
在 Web 开发中经常会碰到需要动态监听输入框值变化的情况,如果使用 onkeydown.onkeypress.onkeyup 这个几个键盘事件来监测的话,监听不了右键的复制.剪贴和粘贴这些操作,处理 ...
- php基础知识【函数】(9)数学和对象类函数
一.数学 abs -- 绝对值 ceil -- 进一法取整 floor -- 舍去法取整 fmod -- 返回除法的浮点数余数 round -- 对浮点数进行四舍五入 sqrt -- 平方根 pi( ...
- Why is 0[0] syntactically valid in javascript?
Why is 0[0] syntactically valid in javascript? 原文链接 偶然在一篇帖子中看到了这个问题,所以打算记录一下. var a = 0[0]; console. ...
- Android RecyclerView Adapter 新式用法之SortedListAdapterCallback
引言 前几天在同事的提醒下发现V7中有了一个新的工具类SortedListAdapterCallback,配合RecyclerView Adapter和SortedList一起使用更加方便的管理我们在 ...
- Unity3d shader内置矩阵
内置矩阵 支持的矩阵(float4x4):UNITY_MATRIX_MVP 当前模型视图投影矩阵UNITY_MATRIX_MV 当前模型视图矩阵UNITY_MATRI ...
- Why GEMM is at the heart of deep learning
Why GEMM is at the heart of deep learning I spend most of my time worrying about how to make deep le ...