yuan
4 天以前 7dadcb512bb57f0da63b054190ae88e04c84c4f6
feat(basic): 新增单文件上传及图片预览功能
已修改5个文件
82 ■■■■■ 文件已修改
.gitignore 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/basic/service/StorageBlobService.java 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/basic/service/impl/StorageBlobServiceImpl.java 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/other/controller/TempFileController.java 62 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/project/common/CommonController.java 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
.gitignore
@@ -24,6 +24,8 @@
*.iws
*.iml
*.ipr
### vscode ###
.vscode
### JRebel ###
rebel.xml
src/main/java/com/ruoyi/basic/service/StorageBlobService.java
@@ -28,6 +28,8 @@
    List<StorageBlobDTO> updateStorageBlobs(List<MultipartFile> files, String bucketName,Long type);
    List<StorageBlobDTO> updateStorageBlob(MultipartFile file, String bucketName, Long type);
    /**
     * 批量删除文件
src/main/java/com/ruoyi/basic/service/impl/StorageBlobServiceImpl.java
@@ -26,6 +26,7 @@
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@@ -110,6 +111,11 @@
        return storageBlobDTOs;
    }
    @Override
    public List<StorageBlobDTO> updateStorageBlob(MultipartFile file, String bucketName, Long type) {
        return updateStorageBlobs(Collections.singletonList(file), bucketName, type);
    }
    private StorageBlobDTO buildStorageBlobDTO(MultipartFile file, MinioResult res, String bucketName, Long type) {
        StorageBlobDTO dto = new StorageBlobDTO();
        dto.setContentType(file.getContentType());
src/main/java/com/ruoyi/other/controller/TempFileController.java
@@ -1,18 +1,25 @@
package com.ruoyi.other.controller;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.other.service.TempFileService;
import com.ruoyi.purchase.dto.ProductRecordDto;
import com.ruoyi.purchase.dto.TicketRegistrationDto;
import com.ruoyi.purchase.service.ITicketRegistrationService;
import com.ruoyi.purchase.service.impl.TicketRegistrationServiceImpl;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@RestController
@@ -45,4 +52,51 @@
        return AjaxResult.success();
    }
    /**
     * 图片预览(根据磁盘路径)
     */
    @GetMapping("/preview")
    @ApiOperation(value = "图片预览")
    public void previewImage(String url, HttpServletResponse response) {
        if (!StringUtils.hasText(url)) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            return;
        }
        try {
            Path filePath = Paths.get(url);
            if (!Files.exists(filePath) || !Files.isRegularFile(filePath)) {
                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
                return;
            }
            String filename = filePath.getFileName().toString();
            String ext = filename.contains(".") ? filename.substring(filename.lastIndexOf('.') + 1).toLowerCase() : "";
            MediaType mediaType;
            switch (ext) {
                case "png":
                    mediaType = MediaType.IMAGE_PNG;
                    break;
                case "gif":
                    mediaType = MediaType.IMAGE_GIF;
                    break;
                case "bmp":
                    mediaType = MediaType.parseMediaType("image/bmp");
                    break;
                case "webp":
                    mediaType = MediaType.parseMediaType("image/webp");
                    break;
                case "jpg":
                case "jpeg":
                    mediaType = MediaType.IMAGE_JPEG;
                    break;
                default:
                    mediaType = MediaType.APPLICATION_OCTET_STREAM;
            }
            response.setContentType(mediaType.toString());
            response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=3600");
            Files.copy(filePath, response.getOutputStream());
        } catch (IOException e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }
}
src/main/java/com/ruoyi/project/common/CommonController.java
@@ -90,6 +90,16 @@
    }
    /**
     * minio通用上传请求(单个)
     */
    @PostMapping("/minioUpload")
    @ApiOperation(value = "minio通用上传请求")
    public AjaxResult minioUploadFile(MultipartFile file, String bucketName, Long type) throws Exception
    {
        return AjaxResult.success(storageBlobService.updateStorageBlob(file, bucketName,type));
    }
    /**
     * 通用上传请求(单个)
     */
    @PostMapping("/upload")