Commit dd2c5cdc by chentianzhong

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

parent 3fa5bf89
...@@ -28,6 +28,7 @@ import lombok.extern.slf4j.Slf4j; ...@@ -28,6 +28,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
...@@ -69,6 +70,26 @@ public class EmailAttachController { ...@@ -69,6 +70,26 @@ public class EmailAttachController {
return ResultVo.success(emailAttachService.findByContentId(vo.getContentId())); return ResultVo.success(emailAttachService.findByContentId(vo.getContentId()));
} }
@ApiOperation("前端调用下载前,判断文件是否是否可以下载")
@AnonymousGetMapping("/preDownload/{id}")
public ResultVo preDownload(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Long id) {
EmailAttach emailAttach = emailAttachService.getById(id);
AssertUtils.notNull(emailAttach, "获取附件信息失败");
String download = emailAttachService.getDownloadPath(emailAttach);
AssertUtils.hasText(download, "下载地址为空");
log.info("下载文件地址:" +download);
try {
byte[] bytes = HttpUtil.downloadBytes(download);
AssertUtils.isTrue(ArrayUtil.isNotEmpty(bytes), "下载文件失败,文件为空");
return ResultVo.success();
} catch (Exception e) {
log.error("下载文件失败:" + e.getLocalizedMessage(), e);
return ResultVo.fail("下载文件失败,文件可能不存在");
}
}
@ApiOperation("pc统一下载附件和移动端旧附件下载") @ApiOperation("pc统一下载附件和移动端旧附件下载")
@AnonymousGetMapping("/download/{id}") @AnonymousGetMapping("/download/{id}")
public void download(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Long id) { public void download(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Long id) {
...@@ -78,16 +99,18 @@ public class EmailAttachController { ...@@ -78,16 +99,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);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try { try {
HttpUtil.download(download, outputStream, true); byte[] bytes = HttpUtil.downloadBytes(download);
byte[] bytes = outputStream.toByteArray();
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");
} catch (Exception e) { } catch (Exception e) {
log.error("下载文件失败:" + e.getLocalizedMessage(), e); log.error("下载文件失败:" + e.getLocalizedMessage(), e);
AssertUtils.isTrue(false, "下载文件失败,文件可能不存在"); if (profile.contains("wan")){
AssertUtils.isTrue(false, "文件超过安全时间,禁止从外网访问下载");
}else{
AssertUtils.isTrue(false, "下载文件失败,文件可能不存在");
}
} }
} }
...@@ -101,10 +124,8 @@ public class EmailAttachController { ...@@ -101,10 +124,8 @@ 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);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try { try {
HttpUtil.download(download, outputStream, true); byte[] bytes = HttpUtil.downloadBytes(download);
byte[] bytes = outputStream.toByteArray();
ServletUtil.write(response, IoUtil.toStream(bytes), "application/octet-stream"); ServletUtil.write(response, IoUtil.toStream(bytes), "application/octet-stream");
} catch (Exception e) { } catch (Exception e) {
log.error("下载文件失败:" + e.getLocalizedMessage(), e); log.error("下载文件失败:" + e.getLocalizedMessage(), e);
......
...@@ -2,8 +2,10 @@ package com.zq.email.service.impl; ...@@ -2,8 +2,10 @@ package com.zq.email.service.impl;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.zq.common.utils.AssertUtils; import com.zq.common.utils.AssertUtils;
import com.zq.common.utils.TokenUtils; import com.zq.common.utils.TokenUtils;
...@@ -167,6 +169,14 @@ public class EmailAttachServiceImpl extends ServiceImpl<EmailAttachMapper, Email ...@@ -167,6 +169,14 @@ public class EmailAttachServiceImpl extends ServiceImpl<EmailAttachMapper, Email
String downloadPath = this.getDownloadPath(emailAttach); String downloadPath = this.getDownloadPath(emailAttach);
String suffix = emailAttach.getAttachName().substring(emailAttach.getAttachName().lastIndexOf(".") + 1); String suffix = emailAttach.getAttachName().substring(emailAttach.getAttachName().lastIndexOf(".") + 1);
AssertUtils.hasText(suffix, "错误的文件名称("+emailAttach.getAttachName()+"),导致获取文件类型失败"); AssertUtils.hasText(suffix, "错误的文件名称("+emailAttach.getAttachName()+"),导致获取文件类型失败");
log.info("wps预览文件地址: " + downloadPath);
try {
byte[] bytes = HttpUtil.downloadBytes(downloadPath);
AssertUtils.isTrue(ArrayUtil.isNotEmpty(bytes), "下载文件失败,文件为空");
}catch (Exception e){
AssertUtils.isTrue(false, "下载文件失败,文件可能不存在");
}
FileViewVo viewVo = new FileViewVo(); FileViewVo viewVo = new FileViewVo();
viewVo.setFileId(IdUtil.fastSimpleUUID()); viewVo.setFileId(IdUtil.fastSimpleUUID());
......
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