update Oss新增File文件上传方法
This commit is contained in:
parent
d058fd01ca
commit
2e5702dc86
@ -24,6 +24,7 @@ import org.dromara.common.oss.exception.OssException;
|
|||||||
import org.dromara.common.oss.properties.OssProperties;
|
import org.dromara.common.oss.properties.OssProperties;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.File;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@ -115,6 +116,18 @@ public class OssClient {
|
|||||||
return UploadResult.builder().url(getUrl() + "/" + path).filename(path).build();
|
return UploadResult.builder().url(getUrl() + "/" + path).filename(path).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public UploadResult upload(File file, String path) {
|
||||||
|
try {
|
||||||
|
PutObjectRequest putObjectRequest = new PutObjectRequest(properties.getBucketName(), path, file);
|
||||||
|
// 设置上传对象的 Acl 为公共读
|
||||||
|
putObjectRequest.setCannedAcl(getAccessPolicy().getAcl());
|
||||||
|
client.putObject(putObjectRequest);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new OssException("上传文件失败,请检查配置信息:[" + e.getMessage() + "]");
|
||||||
|
}
|
||||||
|
return UploadResult.builder().url(getUrl() + "/" + path).filename(path).build();
|
||||||
|
}
|
||||||
|
|
||||||
public void delete(String path) {
|
public void delete(String path) {
|
||||||
path = path.replace(getUrl() + "/", "");
|
path = path.replace(getUrl() + "/", "");
|
||||||
try {
|
try {
|
||||||
@ -132,6 +145,10 @@ public class OssClient {
|
|||||||
return upload(inputStream, getPath(properties.getPrefix(), suffix), contentType);
|
return upload(inputStream, getPath(properties.getPrefix(), suffix), contentType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public UploadResult uploadSuffix(File file, String suffix) {
|
||||||
|
return upload(file, getPath(properties.getPrefix(), suffix));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取文件元数据
|
* 获取文件元数据
|
||||||
*
|
*
|
||||||
|
@ -7,6 +7,7 @@ import org.dromara.system.domain.vo.SysOssVo;
|
|||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -26,6 +27,8 @@ public interface ISysOssService {
|
|||||||
|
|
||||||
SysOssVo upload(MultipartFile file);
|
SysOssVo upload(MultipartFile file);
|
||||||
|
|
||||||
|
SysOssVo upload(File file);
|
||||||
|
|
||||||
void download(Long ossId, HttpServletResponse response) throws IOException;
|
void download(Long ossId, HttpServletResponse response) throws IOException;
|
||||||
|
|
||||||
Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid);
|
Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid);
|
||||||
|
@ -27,11 +27,13 @@ import org.dromara.system.mapper.SysOssMapper;
|
|||||||
import org.dromara.system.service.ISysOssService;
|
import org.dromara.system.service.ISysOssService;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.springframework.cache.annotation.Cacheable;
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@ -130,12 +132,27 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
|
|||||||
throw new ServiceException(e.getMessage());
|
throw new ServiceException(e.getMessage());
|
||||||
}
|
}
|
||||||
// 保存文件信息
|
// 保存文件信息
|
||||||
|
return buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SysOssVo upload(File file) {
|
||||||
|
String originalfileName = file.getName();
|
||||||
|
String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
|
||||||
|
OssClient storage = OssFactory.instance();
|
||||||
|
UploadResult uploadResult = storage.uploadSuffix(file, suffix);
|
||||||
|
// 保存文件信息
|
||||||
|
return buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private SysOssVo buildResultEntity(String originalfileName, String suffix, String configKey, UploadResult uploadResult) {
|
||||||
SysOss oss = new SysOss();
|
SysOss oss = new SysOss();
|
||||||
oss.setUrl(uploadResult.getUrl());
|
oss.setUrl(uploadResult.getUrl());
|
||||||
oss.setFileSuffix(suffix);
|
oss.setFileSuffix(suffix);
|
||||||
oss.setFileName(uploadResult.getFilename());
|
oss.setFileName(uploadResult.getFilename());
|
||||||
oss.setOriginalName(originalfileName);
|
oss.setOriginalName(originalfileName);
|
||||||
oss.setService(storage.getConfigKey());
|
oss.setService(configKey);
|
||||||
baseMapper.insert(oss);
|
baseMapper.insert(oss);
|
||||||
SysOssVo sysOssVo = MapstructUtils.convert(oss, SysOssVo.class);
|
SysOssVo sysOssVo = MapstructUtils.convert(oss, SysOssVo.class);
|
||||||
return this.matchingUrl(sysOssVo);
|
return this.matchingUrl(sysOssVo);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user