Commit 3fa5bf89 by chentianzhong

下载

parent 0d61c378
......@@ -4,9 +4,7 @@ package com.zq.email.controller;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.*;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
......@@ -38,6 +36,7 @@ import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
......@@ -59,6 +58,8 @@ public class EmailAttachController {
private final IEmailAttachService emailAttachService;
private final FileFeignClient fileFeignClient;
@Value("${spring.cloud.config.profile}")
private String profile;
@ApiOperation("获取附件")
......@@ -68,7 +69,7 @@ public class EmailAttachController {
return ResultVo.success(emailAttachService.findByContentId(vo.getContentId()));
}
@ApiOperation("下载附件")
@ApiOperation("pc统一下载附件和移动端旧附件下载")
@AnonymousGetMapping("/download/{id}")
public void download(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Long id) {
EmailAttach emailAttach = emailAttachService.getById(id);
......@@ -81,27 +82,17 @@ public class EmailAttachController {
try {
HttpUtil.download(download, outputStream, true);
byte[] bytes = outputStream.toByteArray();
// String filename = URLEncoder.encode(emailAttach.getAttachName(),"UTF-8");
// response.setHeader("Content-disposition", "attachment;filename="+filename+";"+"filename*=utf-8''"+filename);
// ServletUtil.write(response, IoUtil.toStream(bytes), "application/octet-stream");
ServletUtil.write(response, IoUtil.toStream(bytes), "application/octet-stream",
new String(emailAttach.getAttachName().getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
// ServletUtil.write(response, IoUtil.toStream(bytes), "application/octet-stream",
// emailAttach.getAttachName());
response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + URLEncoder.encode(emailAttach.getAttachName(),"UTF-8"));
response.setCharacterEncoding("UTF-8");
ServletUtil.write(response, IoUtil.toStream(bytes), "application/octet-stream");
} catch (Exception e) {
log.error("下载文件失败:" + e.getLocalizedMessage(), e);
AssertUtils.isTrue(false, "网络异常,下载文件失败");
AssertUtils.isTrue(false, "下载文件失败,文件可能不存在");
}
}
@ApiOperation("下载附件")
@ApiOperation("移动办公新OA邮件下载附件")
@AnonymousGetMapping("/download2/{id}/{filename}")
public void downloadFile(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Long id, @PathVariable("filename") String filename) {
EmailAttach emailAttach = emailAttachService.getById(id);
......@@ -117,7 +108,11 @@ public class EmailAttachController {
ServletUtil.write(response, IoUtil.toStream(bytes), "application/octet-stream");
} catch (Exception e) {
log.error("下载文件失败:" + e.getLocalizedMessage(), e);
AssertUtils.isTrue(false, "网络异常,下载文件失败");
if (profile.contains("wan")){
AssertUtils.isTrue(false, "文件超过安全时间,禁止从外网访问");
}else{
AssertUtils.isTrue(false, "下载文件失败,文件可能不存在");
}
}
}
......
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