Pārlūkot izejas kodu

代码提交:
1.oss直接根据文件相对路径下载文件

sunruiqi 2 gadi atpakaļ
vecāks
revīzija
e8229376b1

+ 34 - 2
jeeplus-module/jeeplus-test/src/main/java/com/jeeplus/test/oss/controller/OssFileController.java

@@ -12,6 +12,9 @@ import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.bind.annotation.*;
 
 
 import javax.annotation.Resource;
 import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
 import java.util.List;
 import java.util.List;
 
 
 @Slf4j
 @Slf4j
@@ -23,13 +26,16 @@ public class OssFileController {
     @Value("${config.accessory.aliyun.aliyunUrl}")
     @Value("${config.accessory.aliyun.aliyunUrl}")
     private String aliyunUrl;
     private String aliyunUrl;
 
 
+    @Value("${config.accessory.aliyun.aliyunDownloadUrl}")
+    private String aliyunDownloadUrl;
+
     @Resource
     @Resource
     private OssService ossService;
     private OssService ossService;
 
 
     @ApiOperation(value = "保存数据")
     @ApiOperation(value = "保存数据")
     @PostMapping("/saveMsg")
     @PostMapping("/saveMsg")
-    public void saveMsg(@RequestBody List<OssServiceDto> dtos) {
-        ossService.saveMsg(dtos);
+    public void saveMsg(@RequestBody List<WorkAttachment> workAttachments) {
+        ossService.saveMsg(workAttachments);
     }
     }
 
 
     @GetMapping("/deleteMsgByFileName")
     @GetMapping("/deleteMsgByFileName")
@@ -57,4 +63,30 @@ public class OssFileController {
         List<WorkAttachment> list = ossService.findFileList(attachmentId);
         List<WorkAttachment> list = ossService.findFileList(attachmentId);
         return ResponseEntity.ok(list);
         return ResponseEntity.ok(list);
     }
     }
+
+    /**
+     * 下载附件
+     */
+    @GetMapping("/downLoadAttach")
+    @ApiOperation(value = "下载附件")
+    public String downLoadAttach(@RequestParam String file, HttpServletResponse response, HttpServletRequest request) throws IOException {
+        file = "http://oss.gangwaninfo.com" + file;
+        file = file.replace("amp;", "");
+        String fileName = file.substring(file.lastIndexOf("/") + 1, file.length());
+        String cons = "";
+        if (file.contains(aliyunUrl)) {
+            cons = aliyunUrl;
+        } else if (file.contains("http://gangwan-app.oss-cn-hangzhou.aliyuncs.com")) {
+            cons = "http://gangwan-app.oss-cn-hangzhou.aliyuncs.com";
+        } else {
+            cons = aliyunDownloadUrl;
+        }
+        String key = file.split(cons + "/")[1];
+        log.info("-----------------------------------------");
+        log.info("fileName=" + fileName);
+        log.info("key=" + key);
+        log.info("-----------------------------------------");
+        ossService.downByStream(key, fileName, response, request.getHeader("USER-AGENT"));
+        return null;
+    }
 }
 }

+ 61 - 18
jeeplus-module/jeeplus-test/src/main/java/com/jeeplus/test/oss/service/OssService.java

@@ -3,6 +3,7 @@ package com.jeeplus.test.oss.service;
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.collection.CollectionUtil;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.JSONObject;
 import com.aliyun.oss.OSSClient;
 import com.aliyun.oss.OSSClient;
+import com.aliyun.oss.model.OSSObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.jeeplus.sys.service.dto.UserDTO;
 import com.jeeplus.sys.service.dto.UserDTO;
 import com.jeeplus.sys.utils.UserUtils;
 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 com.jeeplus.test.oss.service.dto.OssServiceDto;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.flowable.editor.language.json.converter.util.CollectionUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
 import javax.annotation.Resource;
 import javax.annotation.Resource;
+import javax.servlet.http.HttpServletResponse;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
 import java.net.URL;
 import java.net.URL;
+import java.net.URLEncoder;
 import java.util.*;
 import java.util.*;
 
 
 @Slf4j
 @Slf4j
@@ -46,29 +52,27 @@ public class OssService {
 
 
     /**
     /**
      * 保存数据
      * 保存数据
-     * @param dtos
+     * @param workAttachments
      * @return
      * @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();
             String id = UserUtils.getCurrentUserDTO().getId();
             int i = 1;
             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.setType(strings.get(strings.size()-1));
                 workAttachment.setId(UUID.randomUUID().toString().replace("-",""));
                 workAttachment.setId(UUID.randomUUID().toString().replace("-",""));
                 //排序赋值
                 //排序赋值
@@ -127,7 +131,6 @@ public class OssService {
             for (WorkAttachment workAttachment : list) {
             for (WorkAttachment workAttachment : list) {
                 UserDTO userDTO = UserUtils.get(workAttachment.getCreateBy());
                 UserDTO userDTO = UserUtils.get(workAttachment.getCreateBy());
                 workAttachment.setCreateBy(userDTO.getName());
                 workAttachment.setCreateBy(userDTO.getName());
-                workAttachment.setAttachmentName(workAttachment.getAttachmentName()+"."+workAttachment.getType());
             }
             }
         }
         }
         log.info("文件查询结束,查询结果:{}" , JSONObject.toJSONString(list));
         log.info("文件查询结束,查询结果:{}" , JSONObject.toJSONString(list));
@@ -168,4 +171,44 @@ public class OssService {
         OSSClient ossClient = new OSSClient(endpoint,accessKeyId,accessKeySecret);
         OSSClient ossClient = new OSSClient(endpoint,accessKeyId,accessKeySecret);
         return ossClient.generatePresignedUrl(bucketName, key, expiration).toString();
         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();
+        }
+    }
+
 }
 }

+ 5 - 4
jeeplus-module/jeeplus-test/src/main/java/com/jeeplus/test/projectRecords/controller/ProjectController.java

@@ -15,6 +15,7 @@ import com.jeeplus.core.excel.ExportMode;
 import com.jeeplus.core.excel.utils.EasyPoiUtil;
 import com.jeeplus.core.excel.utils.EasyPoiUtil;
 import com.jeeplus.core.query.QueryWrapperGenerator;
 import com.jeeplus.core.query.QueryWrapperGenerator;
 import com.jeeplus.sys.service.dto.UserDTO;
 import com.jeeplus.sys.service.dto.UserDTO;
+import com.jeeplus.test.oss.domain.WorkAttachment;
 import com.jeeplus.test.oss.service.OssService;
 import com.jeeplus.test.oss.service.OssService;
 import com.jeeplus.test.oss.service.dto.OssServiceDto;
 import com.jeeplus.test.oss.service.dto.OssServiceDto;
 import com.jeeplus.test.projectRecords.constant.enums.ProjectStatusEnum;
 import com.jeeplus.test.projectRecords.constant.enums.ProjectStatusEnum;
@@ -169,11 +170,11 @@ public class ProjectController {
             projectService.update (pro,wrapper);//修改
             projectService.update (pro,wrapper);//修改
         }
         }
         //项目id处理
         //项目id处理
-        List<OssServiceDto> dtos = projectDTO.getDtos();
-        if (CollectionUtils.isNotEmpty(dtos)) {
-            dtos.stream().forEach(i -> {i.setAttachmentId(project.getId());});
+        List<WorkAttachment> workAttachments = projectDTO.getWorkAttachments();
+        if (CollectionUtils.isNotEmpty(workAttachments)) {
+            workAttachments.stream().forEach(i -> {i.setAttachmentId(project.getId());});
             //oss上传文件相关
             //oss上传文件相关
-            ossService.saveMsg(projectDTO.getDtos());
+            ossService.saveMsg(projectDTO.getWorkAttachments());
         }
         }
 
 
         return ResponseEntity.ok ("保存项目成功");
         return ResponseEntity.ok ("保存项目成功");

+ 2 - 1
jeeplus-module/jeeplus-test/src/main/java/com/jeeplus/test/projectRecords/service/dto/ProjectDTO.java

@@ -2,6 +2,7 @@ package com.jeeplus.test.projectRecords.service.dto;
 
 
 import cn.afterturn.easypoi.excel.annotation.Excel;
 import cn.afterturn.easypoi.excel.annotation.Excel;
 import com.jeeplus.core.service.dto.BaseDTO;
 import com.jeeplus.core.service.dto.BaseDTO;
+import com.jeeplus.test.oss.domain.WorkAttachment;
 import com.jeeplus.test.oss.service.dto.OssServiceDto;
 import com.jeeplus.test.oss.service.dto.OssServiceDto;
 import lombok.Data;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.EqualsAndHashCode;
@@ -266,7 +267,7 @@ public class ProjectDTO extends BaseDTO {
     /**
     /**
      * oss上传文件相关数据
      * oss上传文件相关数据
      */
      */
-    public List<OssServiceDto> dtos;
+    public List<WorkAttachment> workAttachments;
 
 
 
 
 }
 }