新版仿ok交易所-后端
zyy
2025-09-18 238171af3716cbeeac0c262c497a6d782cbb26d8
文件上传
4 files modified
81 ■■■■■ changed files
trading-order-admin/src/main/java/com/yami/trading/admin/controller/UploadFileController.java 17 ●●●● patch | view | raw | blame | history
trading-order-bean/src/main/java/com/yami/trading/bean/model/FileUploadParamsModel.java 3 ●●●●● patch | view | raw | blame | history
trading-order-service/src/main/java/com/yami/trading/service/AwsS3OSSFileService.java 2 ●●●●● patch | view | raw | blame | history
trading-order-service/src/main/java/com/yami/trading/service/impl/AwsS3OSSFileServiceImpl.java 59 ●●●●● patch | view | raw | blame | history
trading-order-admin/src/main/java/com/yami/trading/admin/controller/UploadFileController.java
@@ -32,13 +32,22 @@
            if (model.getFile().getSize() / 1000L > 4500) {
               throw  new YamiShopBindException("图片大小不能超过4M");
            }
            String path;
            FileInfoVo fileInfoVo=new FileInfoVo();
            if (StringUtils.isEmptyString(model.getModuleName())){
                model.setModuleName("common");
                path=  awsS3OSSFileService.uploadFile(model.getModuleName(), model.getFile());
                fileInfoVo.setHttpUrl(Constants.IMAGES_HTTP + path);
                fileInfoVo.setPath(path);
            } else if(model.getModuleName().equals("icon")){
                path=  awsS3OSSFileService.uploadIcon(model.getFileName(), model.getFile());
                fileInfoVo.setHttpUrl(Constants.IMAGES_HTTP + path);
                fileInfoVo.setPath(path);
            } else {
                path=  awsS3OSSFileService.uploadFile(model.getModuleName(), model.getFile());
                fileInfoVo.setHttpUrl(Constants.IMAGES_HTTP + path);
                fileInfoVo.setPath(path);
            }
            String path=  awsS3OSSFileService.uploadFile(model.getModuleName(), model.getFile());
            FileInfoVo fileInfoVo=new FileInfoVo();
             fileInfoVo.setHttpUrl(Constants.IMAGES_HTTP+path);
             fileInfoVo.setPath(path);
            return Result.ok(fileInfoVo);
        }
         catch (Exception e) {
trading-order-bean/src/main/java/com/yami/trading/bean/model/FileUploadParamsModel.java
@@ -14,4 +14,7 @@
    @ApiModelProperty("模块名")
    private String moduleName;
    @ApiModelProperty("文件名")
    private String fileName;
}
trading-order-service/src/main/java/com/yami/trading/service/AwsS3OSSFileService.java
@@ -10,4 +10,6 @@
    void setPolicy(String bucketName);
    String getUrl( String path);
    String uploadIcon(String moduleName, MultipartFile file);
}
trading-order-service/src/main/java/com/yami/trading/service/impl/AwsS3OSSFileServiceImpl.java
@@ -353,4 +353,63 @@
        String fileType = FilenameUtils.getExtension(fileName);
        return imgTypes.contains(fileType.toLowerCase());
    }
    /**
     * 上传图标
     * @param moduleName
     * @param file
     * @return
     */
    @Override
    public String uploadIcon(String moduleName, MultipartFile file) {
        // 兼容c端组件上传原理
        String originalFilename = file.getOriginalFilename();
        String fileType = originalFilename != null ? originalFilename : "";
        if (StrUtil.isEmpty(fileType) || fileType.contains("blob")) {
            fileType = "blob.png";
        }
        // 提取文件扩展名
        String extension = "";
        int dotIndex = fileType.lastIndexOf('.');
        if (dotIndex > 0) {
            extension = fileType.substring(dotIndex);
        }
        String filename = moduleName + extension;
        String path = filename; // 直接使用文件名作为路径
        // 确保目标文件夹存在
        File targetDir = new File(PropertiesUtil.getProperty("loca.images.dir") + "/symbol");
        if (!targetDir.exists()) {
            targetDir.mkdirs();
            // 设置目录权限为755 (rwxr-xr-x)
            setFilePermissions(targetDir, "755");
        }
        // 构建本地文件路径
        File localFile = new File(targetDir, filename);
        // 打印上传路径
        log.info("LocalFileUploadService uploadFile localFilePath: {}", localFile.getAbsolutePath());
        try {
            // 将文件保存到本地
            file.transferTo(localFile);
            // 设置文件权限为644 (rw-r--r--)
            setFilePermissions(localFile, "644");
            // 如果需要自定义元数据,可以在此处理
            Map<String, String> metadata = new HashMap<>();
            metadata.put("x-amz-meta-myVal", "test");
            // 返回文件名
            return "symbol/" + path;
        } catch (IOException e) {
            log.error("LocalFileUploadService uploadFile IOException", e.getMessage(), e);
            throw new YamiShopBindException("文件上传失败");
        }
    }
}