|
@@ -1,21 +1,31 @@
|
|
|
package com.zhixinghe1.ots.service.manager.impl;
|
|
package com.zhixinghe1.ots.service.manager.impl;
|
|
|
|
|
|
|
|
|
|
+import java.io.BufferedInputStream;
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
|
|
|
+import java.io.FileInputStream;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
|
+import java.io.OutputStream;
|
|
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
|
|
+import java.net.URLEncoder;
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+import java.util.Optional;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
import com.zhixinghe1.ots.atomic.service.IAttachmentService;
|
|
import com.zhixinghe1.ots.atomic.service.IAttachmentService;
|
|
|
|
|
+import com.zhixinghe1.ots.common.exception.BusinessException;
|
|
|
import com.zhixinghe1.ots.config.BizConfig;
|
|
import com.zhixinghe1.ots.config.BizConfig;
|
|
|
import com.zhixinghe1.ots.domain.dto.AttachmentDto;
|
|
import com.zhixinghe1.ots.domain.dto.AttachmentDto;
|
|
|
import com.zhixinghe1.ots.service.manager.IOssManager;
|
|
import com.zhixinghe1.ots.service.manager.IOssManager;
|
|
|
|
|
|
|
|
|
|
+import jakarta.servlet.http.HttpServletResponse;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -32,13 +42,13 @@ public class OssManagerImpl implements IOssManager {
|
|
|
private IAttachmentService attachmentService;
|
|
private IAttachmentService attachmentService;
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
- public List<String> upload(List<MultipartFile> multipartFiles) {
|
|
|
|
|
|
|
+ public List<Long> upload(List<MultipartFile> multipartFiles) {
|
|
|
if (CollectionUtils.isEmpty(multipartFiles)) {
|
|
if (CollectionUtils.isEmpty(multipartFiles)) {
|
|
|
return new ArrayList<>(0);
|
|
return new ArrayList<>(0);
|
|
|
}
|
|
}
|
|
|
List<AttachmentDto> attachmentDtoList = transfer(multipartFiles);
|
|
List<AttachmentDto> attachmentDtoList = transfer(multipartFiles);
|
|
|
attachmentService.batchAddAttachment(attachmentDtoList);
|
|
attachmentService.batchAddAttachment(attachmentDtoList);
|
|
|
- return attachmentDtoList.stream().map(attachmentDto -> attachmentDto.getPath()).collect(Collectors.toList());
|
|
|
|
|
|
|
+ return attachmentDtoList.stream().map(attachmentDto -> attachmentDto.getId()).collect(Collectors.toList());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -68,4 +78,29 @@ public class OssManagerImpl implements IOssManager {
|
|
|
return null;
|
|
return null;
|
|
|
}).filter(dto -> dto != null).collect(Collectors.toList());
|
|
}).filter(dto -> dto != null).collect(Collectors.toList());
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void download(Long attachmentId) throws UnsupportedEncodingException {
|
|
|
|
|
+ AttachmentDto attachmentDto = attachmentService.getById(attachmentId);
|
|
|
|
|
+
|
|
|
|
|
+ HttpServletResponse response =
|
|
|
|
|
+ Optional.ofNullable(((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse())
|
|
|
|
|
+ .orElseThrow();
|
|
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
|
|
+ response.setHeader("Content-Disposition",
|
|
|
|
|
+ "attachment;fileName=" + URLEncoder.encode(attachmentDto.getName(), "UTF-8"));
|
|
|
|
|
+
|
|
|
|
|
+ File file = new File(attachmentDto.getPath());
|
|
|
|
|
+ if (file.exists()) {
|
|
|
|
|
+ throw BusinessException.fail("资源文件不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ try (OutputStream outputStream = response.getOutputStream();
|
|
|
|
|
+ BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file))) {
|
|
|
|
|
+ bufferedInputStream.transferTo(outputStream);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ log.error("下载资源文件时发生异常", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|