fastdfs 使用原文件名下载
安装部署fastdfs
可参考我的上一篇文章fastdfs 单机多groupName部署和springboot区分环境上传
需求
由于fastdfs上传之后文件名使用的是框架编码的文件名所以下载的时候文件名不是原始的名称了。图片显示还好说,文档附件这种的无法忍。于是就有了这篇文章。
openresty代替nginx
通过在上传的时候将源文件名存表,在下载的时候可以在给nginx的请求中加?attname=来指定文件名。但是如果文件名是中文的,那么下载下来的文件都是url编码的(会有%)。所以需要使用openresty来代替nginx,这样就可以用Lua来转码了。
安装和配置openresty
- 下载组件
openresty-1.13.6.1、openssl-1.0.2q、pcre-8.42、zlib-1.2.11。这里有个坑,openresty的最新版不支持最新版的openssl。如果版本不对则会在gmake的时候报错。 - 解压后进入openresty目录开始安装
./configure --prefix=/opt/openresty --with-luajit --with-http_stub_status_module --with-http_ssl_module --with-pcre=/opt/pcre-8.42 --with-openssl=/opt/openssl-1.0.2q --with-zlib=/opt/zlib-1.2.11 --with-http_realip_module --add-module=/opt/fastdfs-nginx-module-master/src
gmake
gmake install
- nginx.conf配置
到openresty/nginx/conf目录下修改nginx.conf
location /test/M00 {
header_filter_by_lua_file lua/downloadfilename.lua;
ngx_fastdfs_module;
}
由于我的groupname有多个,所以这里配置test/M00目录。
header_filter_by_lua_file指定的文件目录是相对于nginx目录而言的。
- downloadfilename.lua
local args = ngx.req.get_uri_args()
local filename=args["attname"]
ngx.header["Content-Disposition"]="attachment;filename="..ngx.escape_uri(filename);
大体思路是从url传参中拿到attname然后进行url转码,最后放入Content-Disposition中指定文件名。
例如
http://localhost:8090/test/M00/00/03/Copdk1w1V1OADZNZAAC0ADl-5b0034.xls?attname=问题提报.xls
来源:CSDN
作者:Will4j
链接:https://blog.csdn.net/will0532/article/details/85631173