yangys
2024-04-01 c323e4ce593a886412bac2403a922ae2bf629655
smart-man-boot/src/main/java/com/qianwen/smartman/modules/resource/endpoint/OssEndpointController.java
@@ -4,6 +4,8 @@
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import io.swagger.annotations.Api;
import me.zhyd.oauth.log.Log;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletResponse;
@@ -75,35 +77,21 @@
    @GetResource({"/file-content"})
    public void getFileContent(@RequestParam String fileName, HttpServletResponse response) {
        InputStream objectStream;
        Throwable th;
        try {
            objectStream = this.ossBuilder.template().getObject(fileName);
            th = null;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e2) {
            throw new ServiceException("文件不存在");
        }
        try {
        try (InputStream objectStream = this.ossBuilder.template().getObject(fileName)) {
            response.setContentType("application/octet-stream; charset=UTF-8");
            response.addHeader("Expires", "0");
            response.addHeader("Pragma", "no-cache");
            response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            IoUtil.copy(objectStream, response.getOutputStream());
            if (objectStream != null) {
                if (0 != 0) {
                    try {
                        objectStream.close();
                    } catch (Throwable th2) {
                        th.addSuppressed(th2);
                    }
                } else {
                    objectStream.close();
                }
            }
        } finally {
        }
        } catch (IOException e) {
           Log.error("获取文件内容错误",e);
            e.printStackTrace();
        } catch (Exception e) {
            throw new ServiceException("文件不存在");
        }
    }
    @PostResource({"/put-file"})
@@ -128,19 +116,31 @@
    @PostResource({"/put-file-attach"})
    public R<BladeFile> putFileAttach(@RequestParam MultipartFile file) {
        String fileName = file.getOriginalFilename();
        BladeFile bladeFile = this.ossBuilder.template().putFile(fileName, file.getInputStream());
        Long attachId = buildAttach(fileName, Long.valueOf(file.getSize()), bladeFile);
        bladeFile.setAttachId(attachId);
        return R.data(bladeFile);
       try {
           String fileName = file.getOriginalFilename();
           BladeFile bladeFile = this.ossBuilder.template().putFile(fileName, file.getInputStream());
           Long attachId = buildAttach(fileName, Long.valueOf(file.getSize()), bladeFile);
           bladeFile.setAttachId(attachId);
           return R.data(bladeFile);
       } catch (Throwable ex) {
             //log.error("putFileAttachByName,异常,file={}",fileName,ex);
             throw new ServiceException("putFileAttach异常"+ex.getMessage());
           }
    }
    @PostResource({"/put-file-attach-by-name"})
    public R<BladeFile> putFileAttachByName(@RequestParam String fileName, @RequestParam MultipartFile file) {
        BladeFile bladeFile = this.ossBuilder.template().putFile(fileName, file.getInputStream());
        Long attachId = buildAttach(fileName, Long.valueOf(file.getSize()), bladeFile);
        bladeFile.setAttachId(attachId);
        return R.data(bladeFile);
       try {
           BladeFile bladeFile = this.ossBuilder.template().putFile(fileName, file.getInputStream());
           Long attachId = buildAttach(fileName, Long.valueOf(file.getSize()), bladeFile);
           bladeFile.setAttachId(attachId);
           return R.data(bladeFile);
        } catch (Throwable ex) {
           //log.error("putFileAttachByName,异常,file={}",fileName,ex);
           throw new ServiceException("putFileAttachByName异常"+ex.getMessage());
         }
    }
    private Long buildAttach(String fileName, Long fileSize, BladeFile bladeFile) {
@@ -170,10 +170,16 @@
    @PostResource({"/get-file-detail"})
    public R<BladeFile> getFileDetail(@RequestParam MultipartFile file) {
        BladeFile bladeFile = this.ossBuilder.template().putFile(file.getName(), file.getInputStream());
        long size = file.getSize();
        Long attachId = buildAttach(file.getName(), Long.valueOf(size), bladeFile);
        bladeFile.setAttachId(attachId);
        return R.data(bladeFile);
       try {
           BladeFile bladeFile = this.ossBuilder.template().putFile(file.getName(), file.getInputStream());
           long size = file.getSize();
           Long attachId = buildAttach(file.getName(), Long.valueOf(size), bladeFile);
           bladeFile.setAttachId(attachId);
           return R.data(bladeFile);
       } catch (Throwable ex) {
          //log.error("putFileAttachByName,异常,file={}",fileName,ex);
          throw new ServiceException("getFileDetail异常"+ex.getMessage());
      }
    }
}