Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加本地储存图片 #171

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.aurora.config.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "upload.local")
public class LocalProperties {
private String imagePath;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public enum UploadModeEnum {

OSS("oss", "ossUploadStrategyImpl"),

Local("local","localUploadStrategyImpl"),

MINIO("minio", "minioUploadStrategyImpl");

private final String mode;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.aurora.strategy.impl;

import com.aurora.config.properties.LocalProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

@Service("localUploadStrategyImpl")
public class LocalUploadStrategyImpl extends AbstractUploadStrategyImpl {

@Value("${upload.local.baseUrl}")
private String baseUrl;

@Value("${upload.local.username}")
private String username;

@Value("${upload.local.password}")
private String password;

@Autowired
private LocalProperties localProperties;
private String imagePath;

@PostConstruct
public void init() {
imagePath = localProperties.getImagePath();
// 确保存储目录存在
new File(imagePath).mkdirs();
}

@Override
public Boolean exists(String filePath) {
// 检查文件是否存在
return new File(imagePath + filePath).exists();
}
private boolean validateCredentials() {
// 在这里实现账号密码验证逻辑,例如:
return username.equals(username) && password.equals(password);
}

@Override
public void upload(String path, String fileName, InputStream inputStream) throws IOException {
// 验证账号密码
if (!validateCredentials()) {
throw new RuntimeException("无效的凭据");
}
// 重命名文件,添加自定义前缀
// String customFileName = "aurora_" + fileName;
String customFileName = fileName;
// 确保上传目录存在
File directory = new File(imagePath + path);
if (!directory.exists()) {
directory.mkdirs();
}

// 写入文件
File file = new File(directory, customFileName);
try (FileOutputStream outputStream = new FileOutputStream(file)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
}
}

@Override
public String getFileAccessUrl(String filePath) {
// 假设您的图片通过HTTP服务器在以下URL下可访问
return baseUrl + filePath;
}
}
14 changes: 13 additions & 1 deletion aurora-springboot/src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ spring:
max-file-size: 100MB
max-request-size: 100MB

#把本地图片映射成local里的地址
resources:
static-locations: 'file:///C:/code/images/'
# linux修改为下面的
# static-locations: 'file:///var/www/images/'

mybatis-plus:
mapper-locations: classpath:mapper/*.xml
configuration:
Expand All @@ -84,7 +90,7 @@ search:
mode: elasticsearch

upload:
mode: oss
mode: local
oss:
url: http://Bucket域名/
endpoint: OSS配置endpoint
Expand All @@ -97,6 +103,12 @@ upload:
accesskey: 用户名
secretKey: 密码
bucketName: 桶的名称
local:
username: admin
password: secretpassword
imagePath: 'C:/code/images/'
baseUrl: http://localhost:8080/
# baseUrl: https://local.gysy.ltd/

website:
url: https://前台域名
Expand Down
2 changes: 1 addition & 1 deletion aurora-springboot/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ spring:
application:
name: aurora-springboot
profiles:
active: dev
active: prod