FastDFS安裝及其他問題參考:https://www.cnblogs.com/jxd283465/p/11556263.html
直接調用FastDFS返回的url,瀏覽器訪問后默認打開方式。
/usr/bin/fdfs_test /etc/fdfs/client.conf upload test.jpg
文件下載方案
1.調用download接口,定義下載的文件名
public void download(String fileUrl, HttpServletResponse response) throws Exception{
byte[] data = fdfsClient.download(fileUrl);
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("test.7z", "UTF-8"));
// 寫出
ServletOutputStream outputStream = response.getOutputStream();
IOUtils.write(data, outputStream);
}
2.nginx反向代理增加請求頭Content-disposition及attachment
server {
listen 8082;
server_name 192.168.8.200;
location /group1/M00 {
add_header Content-Disposition "attachment;filename=$arg_attname";
ngx_fastdfs_module;
}
}
在upload接口中進行url拼接
fdfsClient.uploadFile(file) + "?attname=" + file.getOriginalFilename()
轉:Nginx-Fastdfs根據入參判斷為直接打開文件還是強制下載文件
版權聲明:本文為博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
本文鏈接:https://blog.csdn.net/qq_40809549/article/details/81984798
場景需求:Fastdfs存儲的文件,瀏覽器訪問時默認為打開模式;部分文件要求強制為下載模式
解決辦法:調整nginx配置,根據入參判斷是否添加attachment,強制為下載模式
PS:調試時注意瀏覽器緩存干擾,建議關閉緩存:F12-Network 勾選Disable cache
Nginx根據入參判斷文件是顯示還是下載
location ~/group[0-9] {
if ( $query_string ~* ^(.*)parameter=config\b|1\b(.*)$ ){
add_header Content-Disposition "attachment;";
}
ngx_fastdfs_module;
}
原url訪問時,瀏覽器直接打開文件;
后面跟參數“?parameter=1”時,加attachment,強制為下載模式