Commit fbd5f40f by chentianzhong

下载和预览前判断文件是否能到达

parent dd2c5cdc
...@@ -4,6 +4,7 @@ package com.zq.email.controller; ...@@ -4,6 +4,7 @@ package com.zq.email.controller;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.*; import cn.hutool.core.util.*;
import cn.hutool.extra.servlet.ServletUtil; import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.HttpUtil; import cn.hutool.http.HttpUtil;
...@@ -79,14 +80,18 @@ public class EmailAttachController { ...@@ -79,14 +80,18 @@ public class EmailAttachController {
String download = emailAttachService.getDownloadPath(emailAttach); String download = emailAttachService.getDownloadPath(emailAttach);
AssertUtils.hasText(download, "下载地址为空"); AssertUtils.hasText(download, "下载地址为空");
log.info("下载文件地址:" +download); log.info("下载文件地址:" + download);
try { try {
byte[] bytes = HttpUtil.downloadBytes(download); byte[] bytes = HttpUtil.downloadBytes(download);
AssertUtils.isTrue(ArrayUtil.isNotEmpty(bytes), "下载文件失败,文件为空"); AssertUtils.isTrue(ArrayUtil.isNotEmpty(bytes), "下载文件失败,文件为空");
return ResultVo.success(); return ResultVo.success();
} catch (Exception e) { } catch (Exception e) {
log.error("下载文件失败:" + e.getLocalizedMessage(), e); log.error("下载文件失败:" + e.getLocalizedMessage(), e);
return ResultVo.fail("下载文件失败,文件可能不存在"); if (profile.contains("wan")) {
return ResultVo.fail("文件可能超过安全时间,禁止从外网访问下载,请从内网查看下载");
} else {
return ResultVo.fail("下载文件失败,文件可能不存在");
}
} }
} }
...@@ -103,7 +108,8 @@ public class EmailAttachController { ...@@ -103,7 +108,8 @@ public class EmailAttachController {
byte[] bytes = HttpUtil.downloadBytes(download); byte[] bytes = HttpUtil.downloadBytes(download);
response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + URLEncoder.encode(emailAttach.getAttachName(),"UTF-8")); response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + URLEncoder.encode(emailAttach.getAttachName(),"UTF-8"));
response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
ServletUtil.write(response, IoUtil.toStream(bytes), "application/octet-stream"); //ServletUtil.write(response, IoUtil.toStream(bytes),"application/octet-stream");
ServletUtil.write(response, IoUtil.toStream(bytes), FileUtil.getMimeType(emailAttach.getAttachName())!=null?FileUtil.getMimeType(emailAttach.getAttachName()):"application/octet-stream");
} catch (Exception e) { } catch (Exception e) {
log.error("下载文件失败:" + e.getLocalizedMessage(), e); log.error("下载文件失败:" + e.getLocalizedMessage(), e);
if (profile.contains("wan")){ if (profile.contains("wan")){
......
package com.zq.email.utils; package com.zq.email.utils;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import com.zq.email.entity.EmailAttach; import com.zq.email.entity.EmailAttach;
import io.swagger.models.auth.In; import io.swagger.models.auth.In;
...@@ -20,11 +21,15 @@ public class EmailUtil { ...@@ -20,11 +21,15 @@ public class EmailUtil {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
InternetAddress[] parse = InternetAddress.parse("111@sina.com,111@sina.com,"); String mimeType = FileUtil.getMimeType("ccccc.ofd");
for (InternetAddress internetAddress : parse) { System.out.println(mimeType);
System.out.println(internetAddress.toString());
System.out.println(internetAddress.getAddress());
} // InternetAddress[] parse = InternetAddress.parse("111@sina.com,111@sina.com,");
// for (InternetAddress internetAddress : parse) {
// System.out.println(internetAddress.toString());
// System.out.println(internetAddress.getAddress());
// }
// Session session = Session.getDefaultInstance(new Properties()); // Session session = Session.getDefaultInstance(new Properties());
// MimeMessage message = createMessage(session); // MimeMessage message = createMessage(session);
...@@ -83,4 +88,6 @@ public class EmailUtil { ...@@ -83,4 +88,6 @@ public class EmailUtil {
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment