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'

 
  1. <dependency>
  2. <groupId>org.ocpsoft.rewrite</groupId>
  3. <artifactId>rewrite-servlet</artifactId>
  4. <version>2.0.8.Final</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>javax.servlet</groupId>
  8. <artifactId>javax.servlet-api</artifactId>
  9. <version>3.1.0</version>
  10. <scope>provided</scope>
  11. </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

 
  1. @RewriteConfiguration
  2. public class StaticContentConfiguration extends HttpConfigurationProvider {
  3. @Override
  4. public Configuration getConfiguration(ServletContext context) {
  5. /*
  6. * You should select a path that exposes only the directory(s) from which content should be served, otherwise the
  7. * entire hard drive could be accessible.
  8. */
  9. File resource = new File("/home/contentserv/{file}");
  10.  
  11. return ConfigurationBuilder.begin()
  12. .addRule()
  13. .when(Path.matches("/{file}")
  14. .and(Filesystem.fileExists(resource))
  15. .and(Direction.isInbound())
  16. )
  17. .perform(Stream.from(resource)
  18. .and(Response.setStatus(200))
  19. .and(Response.complete())
  20. )
  21. .where("file").matches(".*");
  22. }
  23.  
  24. @Override
  25. public int priority() {
  26. return 0;
  27. }
  28. }

You could even add logging using the provided `Log` operation from rewrite-servlet, so that requests can be tracked in your server logs:

  1. .perform(Stream.from(resource)
  2. .and(Response.setStatus(200))
  3. .and(Response.complete())
  4. .and(Log.message(Level.INFO, "Served file: {file}."))
  5. )

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. 命令行,备份、导入数据库Oracle

    备份库:exp username/password@Database file="h:\datas.dmp" owner=username 导入语句 :imp username2/ ...

  2. 根据select不同的选项实现相应input框添加项的显示

    实现效果: @1.单击包时,显示包时的添加项 @2.单击包里程,显示包里程的添加项 二  代码实现: 给select添加change事件 获取当前select的value 根据value判断对象显示其 ...

  3. 百度的一个Ajax跨域方法 JavaScript是没有域的限制

    baidu的通行证处理都是在二级域名passport.baidu.com中处理的,但是baidu很多地方登录都好像是用ajax处理的,他是怎么做的呢?研究了一下,发现一个小技巧. 在http://zh ...

  4. OC-深浅复制

    [OC学习-26]对象的浅拷贝和深拷贝——关键在于属性是否可被拷贝 对象的拷贝分为浅拷贝和深拷贝, 浅拷贝就是只拷贝对象,但是属性不拷贝,拷贝出来的对象和原来的对象共用属性,即指向同一个属性地址. 深 ...

  5. NSValue NSNumber NSData类

    NSValue NSNumber NSData类 步骤1 NSValue 我们先看看NSValue能做什么: 一个NSValue对象是用来存储一个C或者Objective-C数据的简单容器.它可以保存 ...

  6. oracle利用merge更新一表的某列数据到另一表中

    假设你有两张表 t1 表 -------------------------- id |    name   |   pwd 1  |      n1     | t2 表 ------------- ...

  7. 【Java】Java垃圾回收机制

    Java垃圾回收机制 说到垃圾回收(Garbage Collection,GC),很多人就会自然而然地把它和Java联系起来.在Java中,程序员不需要去关心内存动态分配和垃圾回收的问题,这一切都交给 ...

  8. ibatis动态语句加and 和不加and

    <select id="queryGoodsGroupCodeName" parameterClass="String" resultClass=&quo ...

  9. ARM单片机的头文件如何用结构体定义地址

    下面我们以ARM Cortex-M0内核单片机LPC1114的头文件lpc11xx.h文件进行说明. 1.先说两句 lpc11xx.h文件是lpc11xx系列单片机包含的头文件.这个文件的作用和51单 ...

  10. WebService引擎Axis2完美教程

    1.http://wenku.baidu.com/link?url=O05r69TGaLmrJrjJqGz-5EASox8FhhUO6i97xoUOV2dcbh8BEnOlX2EN3nuYGE_3HE ...