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):

Include Rewrite in your 'pom.xml'

 
<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:

The Rewrite ConfigurationProvider

 
@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的更多相关文章

  1. [ASP.NET Core] Static File Middleware

    前言 本篇文章介绍ASP.NET Core里,用来处理静态档案的Middleware,为自己留个纪录也希望能帮助到有需要的开发人员. ASP.NET Core官网 结构 一个Web站台最基本的功能,就 ...

  2. Static File Middleware

    [ASP.NET Core] Static File Middleware   前言 本篇文章介绍ASP.NET Core里,用来处理静态档案的Middleware,为自己留个纪录也希望能帮助到有需要 ...

  3. Asp.net core 学习笔记 ( IIS, static file 性能优化 )

    更新 : 2019-02-06 最后还是把 rewrite 给替换掉了. 所以 rewrite url 也不依赖 iis 了咯. refer : https://docs.microsoft.com/ ...

  4. Simple Web API Server in Golang (1)

    To be an better Gopher, get your hands dirty. Topcoder offered a serials of challenges for learning ...

  5. 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. ...

  6. 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 ...

  7. 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 ...

  8. java.lang.ClassFormatError: Illegal UTF8 string in constant pool in class file Server/Request

    Linux服务器上,将本地编译好的文件上传后,Tomcat启动时报错: Exception in thread "Thread-2" java.lang.ClassFormatEr ...

  9. Http File Server小工具

    一般情况下,在做一些测试(比如下载服务)的时候需要提供一个http文件下载服务. 下面这个轻量级的工具HFS可以在本地提供http服务: 官网地址传送门:Http File Server

随机推荐

  1. php基础知识【函数】(7)url和ob函数

    一.URl函数 1.urlencode -- 编码 URL 字符串 2.urldecode -- 解码已编码的 URL 字符串 3.rawurlencode -- 按照 RFC 1738 对 URL ...

  2. C# ERP开发框架

    C/S系统开发框架-企业版 V4.0 (Enterprise Edition) 简介: http://www.csframework.com/cs-framework-4.0.htm 视频下载: 百度 ...

  3. Http和Socket连接的区别

    Http和Socket连接区别 相信不少初学手机联网开发的朋友都想知道Http与Socket连接究竟有什么区别,希望通过自己的浅显理解能对初学者有所帮助. 1.TCP连接 要想明白Socket连接,先 ...

  4. day2练习题

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...

  5. Mongodb2.6升级到Mongodb3.0.2笔记

    PS:升级到3版本的Mongodb以后,drop集合是可以直接释放磁盘空间的 停止老版本Mongodb >use admin >db.shutdownServer(); 下载解压新版本Mo ...

  6. Ncurses <一>

    前言: 最好的ncurses教程是 ncurses HOWTO,网上有中文版 编译ncurses引用的程序,需要加编译参数 -lncurses 并在.c文件中包含 ncurses.h头文件 1. 启动 ...

  7. Door man

    poj1300:http://poj.org/problem?id=1300 题意:给你n个房间,房间之间有一些门,房间是按0~~n-进行编号的.然后给出一些房间的之间门,n行,每行的数字表示该们与其 ...

  8. hdu 5063 Operation the Sequence

    http://acm.hdu.edu.cn/showproblem.php?pid=5063 思路:因为3查询最多50,所以可以在查询的时候逆操作找到原来的位置,然后再求查询的值. #include ...

  9. bzoj1221

    网络流与线性规划24题中的餐巾计划吧明显要拆点吧,把每一天拆成2个点,i,i+n起点   终点    容量    费用 s      i      inf      c    每天都可以购买新毛巾 i ...

  10. OpenSSH远程拒绝服务漏洞

    漏洞版本: OpenSSH 漏洞描述: Bugtraq ID:61286 OpenSSH是一种开放源码的SSH协议的实现 OpenSSH存在一个安全漏洞,允许远程攻击者利用漏洞提交恶意请求,使应用程序 ...