|
@@ -3,6 +3,7 @@ package com.jeeplus.test.oss.service;
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.aliyun.oss.OSSClient;
|
|
|
+import com.aliyun.oss.model.OSSObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.jeeplus.sys.service.dto.UserDTO;
|
|
|
import com.jeeplus.sys.utils.UserUtils;
|
|
@@ -11,12 +12,17 @@ import com.jeeplus.test.oss.mapper.OssServiceMapper;
|
|
|
import com.jeeplus.test.oss.service.dto.OssServiceDto;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.flowable.editor.language.json.converter.util.CollectionUtils;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.BufferedInputStream;
|
|
|
+import java.io.BufferedOutputStream;
|
|
|
import java.net.URL;
|
|
|
+import java.net.URLEncoder;
|
|
|
import java.util.*;
|
|
|
|
|
|
@Slf4j
|
|
@@ -46,29 +52,27 @@ public class OssService {
|
|
|
|
|
|
/**
|
|
|
* 保存数据
|
|
|
- * @param dtos
|
|
|
+ * @param workAttachments
|
|
|
* @return
|
|
|
*/
|
|
|
- public void saveMsg(List<OssServiceDto> dtos) {
|
|
|
- if (CollectionUtil.isNotEmpty(dtos)) {
|
|
|
- //若数据有修改,先删除原有数据,再执行保存操作
|
|
|
- String attachmentId = dtos.get(0).getAttachmentId();
|
|
|
- log.info("开始执行删除操作,条件attachmentId值:{}" , attachmentId);
|
|
|
- if (StringUtils.isNotEmpty(attachmentId)) {
|
|
|
- LambdaQueryWrapper<WorkAttachment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
- lambdaQueryWrapper.eq(WorkAttachment::getAttachmentId, attachmentId);
|
|
|
- int delete = ossServiceMapper.delete(lambdaQueryWrapper);
|
|
|
- log.info("删除操作完成,共删除{}条数据" , attachmentId);
|
|
|
- }
|
|
|
- log.info("开始执行保存操作,入参:{}" , JSONObject.toJSONString(dtos));
|
|
|
+ public void saveMsg(List<WorkAttachment> workAttachments) {
|
|
|
+ if (CollectionUtil.isNotEmpty(workAttachments)) {
|
|
|
//获取当前登录人信息
|
|
|
String id = UserUtils.getCurrentUserDTO().getId();
|
|
|
int i = 1;
|
|
|
- for (OssServiceDto dto : dtos) {
|
|
|
- WorkAttachment workAttachment = new WorkAttachment();
|
|
|
- BeanUtils.copyProperties(dto,workAttachment);
|
|
|
+ for (WorkAttachment workAttachment : workAttachments) {
|
|
|
+ //判断文件是否存在
|
|
|
+ LambdaQueryWrapper<WorkAttachment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ lambdaQueryWrapper.eq(WorkAttachment::getUrl, workAttachment.getUrl());
|
|
|
+ lambdaQueryWrapper.eq(WorkAttachment::getAttachmentId, workAttachment.getAttachmentId());
|
|
|
+ List<WorkAttachment> list = ossServiceMapper.selectList(lambdaQueryWrapper);
|
|
|
+ if (CollectionUtils.isNotEmpty(list)) {
|
|
|
+ i++;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ log.info("开始执行保存操作,入参:{}" , JSONObject.toJSONString(workAttachments));
|
|
|
//文件后缀名赋值
|
|
|
- List<String> strings = Arrays.asList(dto.getUrl().split("\\."));
|
|
|
+ List<String> strings = Arrays.asList(workAttachment.getUrl().split("\\."));
|
|
|
workAttachment.setType(strings.get(strings.size()-1));
|
|
|
workAttachment.setId(UUID.randomUUID().toString().replace("-",""));
|
|
|
//排序赋值
|
|
@@ -127,7 +131,6 @@ public class OssService {
|
|
|
for (WorkAttachment workAttachment : list) {
|
|
|
UserDTO userDTO = UserUtils.get(workAttachment.getCreateBy());
|
|
|
workAttachment.setCreateBy(userDTO.getName());
|
|
|
- workAttachment.setAttachmentName(workAttachment.getAttachmentName()+"."+workAttachment.getType());
|
|
|
}
|
|
|
}
|
|
|
log.info("文件查询结束,查询结果:{}" , JSONObject.toJSONString(list));
|
|
@@ -168,4 +171,44 @@ public class OssService {
|
|
|
OSSClient ossClient = new OSSClient(endpoint,accessKeyId,accessKeySecret);
|
|
|
return ossClient.generatePresignedUrl(bucketName, key, expiration).toString();
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 附件下载
|
|
|
+ * @param key
|
|
|
+ * @param fileName
|
|
|
+ * @param response
|
|
|
+ */
|
|
|
+ public void downByStream(String key, String fileName, HttpServletResponse response, String agent){
|
|
|
+ try {
|
|
|
+ String newName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20").replaceAll("%28", "\\(").replaceAll("%29", "\\)").replaceAll("%3B", ";").replaceAll("%40", "@").replaceAll("%23", "\\#").replaceAll("%26", "\\&").replaceAll("%2C", "\\,");
|
|
|
+ // 创建OSSClient实例
|
|
|
+ //初始化OSSClient
|
|
|
+ OSSClient ossClient = new OSSClient(endpoint,accessKeyId,accessKeySecret);
|
|
|
+ OSSObject ossObject = ossClient.getObject(bucketName, key);
|
|
|
+ BufferedInputStream in = new BufferedInputStream(ossObject.getObjectContent());
|
|
|
+ BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
|
|
|
+ response.setHeader("Content-Disposition","attachment;filename*=UTF-8''"+ newName);
|
|
|
+
|
|
|
+ /*if(agent != null && agent.toLowerCase().indexOf("firefox") > 0){
|
|
|
+ response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''"+ URLEncoder.encode(fileName,"utf-8"));
|
|
|
+ }else {
|
|
|
+ response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(fileName,"utf-8"));
|
|
|
+ }*/
|
|
|
+ byte[] car=new byte[1024];
|
|
|
+ int L=0;
|
|
|
+ while((L=in.read(car))!=-1){
|
|
|
+ out.write(car, 0,L);
|
|
|
+ }
|
|
|
+ if(out!=null){
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+ if(in!=null){
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|