fastdfs文件下载支持token认证,文件下载时去掉fullfilename等多余的拼接参数

This commit is contained in:
王跑跑 2025-02-21 17:26:44 +08:00
parent b99c2b67ab
commit cea410e044
2 changed files with 35 additions and 10 deletions

View File

@ -114,19 +114,40 @@ public class TokenService {
return cacheServiceRedisImpl.getAccessTokenCacheAndSetValue(TOKEN_KEY, token);
}
/**
* 去除 URL 中的 &fullfilename= 参数
*
* @param url
* @return
* String
* @Date 2025年2月21日16:33:51
* @author wangshanzhen2
*/
public static String removeParameter(String url) {
// 去除 &fullfilename= 参数
url = url.replaceAll("&fullfilename=[^&]*", "");
// 去除 &accessToken= 参数
url = url.replaceAll("&accessToken=[^&]*", "");
return url;
}
public static void main(String[] args) {
//String url = "http://172.168.27.251:26066/fastdfs/service/fastdfs/download/ppWtKACHGUMnMM9U42.docx&fullfilename=ppWtKACHGUMnMM9U42.docx&accessToken=12223456&type=32";
//String url = "http://172.168.27.251:26066/fastdfs/service/fastdfs/download/ppWtKACHGUMnMM9U42.docx&fullfilename=ppWtKACHGUMnMM9U42.docx&accessToken=12223456";
String url = "http://172.168.27.251:26066/fastdfs/service/fastdfs/download/ppWtKACHGUMnMM9U42.docx&fullfilename=ppWtKACHGUMnMM9U42.docx";
// 提取 accessToken 参数的值
String accessToken = getAccessToken(url);
// 打印 accessToken
System.out.println("Extracted accessToken: " + accessToken);
// 使用正则表达式删除 accessToken 参数
String newUrl = removeAccessToken(url);
// 打印移除后的 URL
System.out.println("URL after removing accessToken: " + newUrl);
String url = "http://172.168.27.251:26066/fastdfs/service/fastdfs/download/ppWtKACHGUMnMM9U42.docx&fullfilename=ppWtKACHGUMnMM9U42.docx&accessToken=68";
// // 提取 accessToken 参数的值
// String accessToken = getAccessToken(url);
// // 打印 accessToken
// System.out.println("Extracted accessToken: " + accessToken);
// // 使用正则表达式删除 accessToken 参数
// String newUrl = removeAccessToken(url);
// // 打印移除后的 URL
// System.out.println("URL after removing accessToken: " + newUrl);
// 去掉 &fullfilename= 参数
String resultUrl = removeParameter(url);
System.out.println("URL after removing the fullfilename parameter: " + resultUrl);
}
}

View File

@ -24,6 +24,7 @@ import org.springframework.web.client.RestTemplate;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
@ -122,7 +123,10 @@ public class DownloadUtils {
};
try {
logger.info("http资源下载: {}",url.toURI());
restTemplate.execute(url.toURI(), HttpMethod.GET, requestCallback, fileResponse -> {
url = WebUtils.normalizedURL(TokenService.removeParameter(url.toURI().toString()));
logger.info("removeParameter url: {}",url.toURI());
restTemplate.execute(url.toURI(), HttpMethod.GET, requestCallback, fileResponse -> {
FileUtils.copyToFile(fileResponse.getBody(), realFile);
return null;
});