修改下载附件后缀问题

This commit is contained in:
2025-07-16 18:21:27 +08:00
parent c5450c6e0c
commit 08791ac5a5

View File

@@ -357,12 +357,19 @@ public class EmailUtil {
} }
/** /**
* 下载网络文件到本地临时文件 * 下载网络文件到本地临时文件,保留原始文件后缀
* @param url 文件的http/https链接 * @param url 文件的http/https链接
* @return 本地临时文件 * @return 本地临时文件
*/ */
public static File downloadUrlToTempFile(String url) throws Exception { public static File downloadUrlToTempFile(String url) throws Exception {
File tempFile = File.createTempFile("mail_attach_", null); // 提取文件名和后缀
String fileName = url.substring(url.lastIndexOf('/') + 1);
String suffix = "";
int dotIdx = fileName.lastIndexOf('.');
if (dotIdx != -1) {
suffix = fileName.substring(dotIdx);
}
File tempFile = File.createTempFile("mail_attach_", suffix);
try (java.io.InputStream in = new java.net.URL(url).openStream(); try (java.io.InputStream in = new java.net.URL(url).openStream();
java.io.OutputStream out = new java.io.FileOutputStream(tempFile)) { java.io.OutputStream out = new java.io.FileOutputStream(tempFile)) {
byte[] buffer = new byte[8192]; byte[] buffer = new byte[8192];