我打算开发一个 Intranet 应用程序(Java 客户端、JSP、SQLite)
这样做的目的是,当用户单击链接时,如果用户有权访问(在业务逻辑中处理的团队),则应提供文件以供下载
数据库中有一个表,其中包含信息,下面是示例行
ID | file | team | md5
1 | D:\test\output_20140915_100012.zip | Falcon | 5cceaf4cc400fd8f5c7fb4b2af754f4093369f59
其中 MD5 是字符串“D:\test\output.zip”的 MD% 校验和,而不是文件本身
我创建 MD5 只是为了获得随机数。我不使用 RAND 的原因是为了避免冲突(这部分是微不足道的)
我希望我的 URL 看起来像这样
http://mywebserver:8080/<appname>/5cceaf4cc400fd8f5c7fb4b2af754f4093369f59
应该去哪里
http://mywebserver:8080/<appname>/download.jsp?id=5cceaf4cc400fd8f5c7fb4b2af754f4093369f59
我正在使用 Tomcat,我想知道我们是否可以为这种情况重写 URL
最佳答案
我建议使用 Apache HTTP Server 或其他服务器来完成。但是,如果您只想使用 Tomcat 7 来执行此操作,请使用 Tuckey UrlRewriteFilter :
A Java Web Filter for any compliant web application servers (such as Tomcat, JBoss, Jetty or Resin), which allows you to rewrite URLs before they get to your code. It is a very powerful tool just like Apache's mod_rewrite.
此过滤器允许您定义如下规则:
Clean a url
<rule> <from>/products/([0-9]+)</from> <to>/products/index.jsp?product_id=$1</to> </rule>
eg, /products/1234 will be passed on to /products/index.jsp?product_id=1234 without the user noticing
从 Tomcat 8 开始,您可以使用重写阀。
The rewrite valve implements URL rewrite functionality in a way that is very similar to mod_rewrite from Apache HTTP Server.
关于java - Tomcat 7 中的 URLRewrite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25873406/