SpringBoot通过OSS实现大文件分片上传

String ossCode = ossService.doesObjectExist(fsRootDir + filePath);

//若不存在则返回注册成功

if (!ObjectUtils.isEmpty(ossCode)) {

String url = ossService.getUrl(ossCode);

SysUploadItemVo sysUploadItemVo = new SysUploadItemVo(ossCode, fileMd5, url, “done”, fileName);

return Response.success(sysUploadItemVo).setMessage(“文件已存在”);

} else {

String uploadId = ossService.initMultiPartUpload(fileName, filePath);

return Response.success(uploadId).setMessage(“注册成功”);

}

}

@Override

public Response breakpointRenewal(

MultipartFile file,

String fileMd5,

String uploadId,

Integer chunk,

String fileExt

) {

try {

// 检查分块目录是否存在

String objectName = this.getFilePath(fileMd5, fileExt);

PartETag uploadPart = ossService.uploadPart(objectName, uploadId, file.getInputStream(),

(int) file.getSize(), (chunk + 1));

redisService.hset(RedisPrefixConst.BREAKPOINT_H_KEY_PREFIX + fileMd5, (chunk + 1) + “”,

JSONObject.toJSONString(uploadPart), 86400);

} catch (IOException e) {

e.printStackTrace();

redisService.hdel(RedisPrefixConst.BREAKPOINT_H_KEY_PREFIX + fileMd5, (chunk + 1) + “”);

}

return null;

}

@Override

public Response existsChunk(

String fileMd5,

Integer chunk,

Integer chunkSize

) {

//检查分块信息是否存在于redis中,若存在则返回true,否则false

String partMd5 = redisService.hget(RedisPrefixConst.BREAKPOINT_H_KEY_PREFIX + fileMd5, (chunk + 1) + “”);

if (StringUtils.isEmpty(partMd5)) {

return Response.success(false);

}

return Response.success(true);

}

@Override

public Response mergeChunks(

String fileMd5,

String uploadId,

String fileName,

Long fileSize,

String mimetype,

String fileExt

) {

//从redis中获取改文件的所有分块信息列表(ETag)

Map<String, String> dataMap = redisService.hscan(RedisPrefixConst.BREAKPOINT_H_KEY_PREFIX + fileMd5, null);

//调用oss合并文件方法,获取完整文件标识信息

List partETagList = Lists.newArrayList();

for (Map.Entry<String, String> entry : dataMap.entrySet()) {

partETagList.add(JSONObject.parseObject(entry.getValue(), PartETag.class));

}

//删除redis分块记录

redisService.delete(RedisPrefixConst.BREAKPOINT_H_KEY_PREFIX + fileMd5);

//组装文件

String objectName = this.getFilePath(fileMd5, fileExt);

CompleteMultipartUploadResult result = ossService.completeMultipartUpload(objectName, uploadId, partETagList);

//组装返回结果到前端

String url = ossService.getUrl(result.getKey());

SysUploadItemVo sysUploadItemVo = new SysUploadItemVo(result.getKey(), fileMd5, url, “done”, fileName);

return Response.success(sysUploadItemVo).setMessage(“文件上传成功”);

}

///

// private functions

///

private File mergeFile(List chunkFileList, File mergeFile) {

try {

// 有删 无创建

if (mergeFile.exists()) {

mergeFile.delete();

} else {

File newParentFile = mergeFile.getParentFile();

if (!newParentFile.exists()) {

newParentFile.mkdirs();

}

mergeFile.createNewFile();

}

// 排序

Collections.sort(chunkFileList, (o1, o2) -> {

if (Integer.parseInt(o1.getName()) > Integer.parseInt(o2.getName())) {

return 1;

}

return -1;

});

byte[] b = new byte[1024];

RandomAccessFile writeFile = new RandomAccessFile(mergeFile, “rw”);

for (File chunkFile : chunkFileList) {

RandomAccessFile readFile = new RandomAccessFile(chunkFile, “r”);

int len = -1;

while ((len = readFile.read(b)) != -1) {

writeFile.write(b, 0, len);

}

readFile.close();

}

writeFile.close();

return mergeFile;

} catch (IOException e) {

e.printStackTrace();

return null;

}

}

private String getUploadFilePath(String filePath) {

return uploadDir + filePath;

}

}

8、RedisService接口代码

/**

  • @author chengmo on 2020/7/16

*/

public interface RedisService {

Long increment(String key);

boolean hasKey(String key);

String get(String key);

boolean delete(String key);

boolean set(String key, String value);

boolean set(String key, String value, Long timeout);

boolean setExpire(String key, Long expire);

long getExpire(String key);

String hget(String key, String hKey);

Map<String, String> hscan(String key, String pattern);

boolean hset(String key, String hKey, String hValue);

boolean hset(String key, String hKey, String hValue, long timeout);

boolean hdel(String key, String hKey);

boolean sadd(String key, String… value);

Set smembers(String key);

boolean szset(String key, String value, double score);

Long szdel(String key, String… value);

Set szrange(String key, long start, long end);

Set szdelAndGet(String key, long start, long end);

Double szscore(String key, String value);

Long lLeftPush(String key, String value);

Long lLeftPushAll(String key, Collection values);

String lRightPop(String key, long expire, TimeUnit unit);

RLock getLock(String key);

}

9、RedisServiceImpl代码

/**

  • @author chengmo on 2020/7/16

*/

@Service

public class RedisServiceImpl implements RedisService {

private static final GLog LOG = LogFactory.getLogger(RedisServiceImpl.class);

@Autowired

private RedissonClient client;

@Autowired

private StringRedisTemplate stringRedisTemplate;

private final String namespace;

public RedisServiceImpl(@Value(“${spring.application.name}”) String namespace) {

this.namespace = Constants.GLZ + Constants.COLON +

StringUtils.replace(namespace, “-”, “_”) +

Constants.COLON;

}

public Long increment(String key) {

String redisKey = this.rebuildKey(key);

Long req = null;

try {

ValueOperations<String, String> op = stringRedisTemplate.opsForValue();

req = op.increment(redisKey);

} catch (Exception e) {

LOG.error(“increment [Key:{0}] from redis failed!”, e, redisKey);

}

return req;

}

@Override

public boolean hasKey(String key) {

String redisKey = this.rebuildKey(key);

try {

return stringRedisTemplate.hasKey(redisKey);

} catch (Exception e) {

LOG.error(“hasKey [Key:{0}] from redis failed!”, e, redisKey);

}

return false;

}

@Override

public String get(String key) {

String redisKey = this.rebuildKey(key);

String result;

try {

result = stringRedisTemplate.opsForValue().get(redisKey);

if (null == result) {

return null;

}

return result;

} catch (Exception e) {

LOG.error(“Get [Key:{0}] from redis failed!”, e, redisKey);

}

return null;

}

@Override

public boolean delete(String key) {

String redisKey = this.rebuildKey(key);

try {

stringRedisTemplate.delete(redisKey);

return true;

} catch (Exception e) {

LOG.error(“Delete [Key:{0}]from redis failed!”, e, redisKey);

}

return false;

}

@Override

public boolean set(String key, String value) {

String redisKey = this.rebuildKey(key);

try {

stringRedisTemplate.opsForValue().set(redisKey, value);

return true;

} catch (Exception e) {

LOG.error(“Set [Key:{0}][Value:{1}] into redis failed!”, e, redisKey, value);

}

return false;

}

@Override

public boolean set(String key, String value, Long timeout) {

String redisKey = this.rebuildKey(key);

try {

stringRedisTemplate.opsForValue().set(redisKey, value, timeout, TimeUnit.SECONDS);

return true;

} catch (Exception e) {

LOG.error(“Set [Key:{0}][Value:{1}][TimeOut:{2}] into redis failed!”, e, redisKey, value, timeout);

}

return false;

}

@Override

public boolean setExpire(String key, Long expire) {

String redisKey = this.rebuildKey(key);

try {

stringRedisTemplate.expire(redisKey, expire, TimeUnit.SECONDS);

return true;

} catch (Exception e) {

LOG.error(“setExpire [Key:{0}][handleAutoRenew:{1}] into redis failed!”, e, redisKey, expire);

}

return false;

}

@Override

public long getExpire(String key) {

String redisKey = this.rebuildKey(key);

try {

return stringRedisTemplate.getExpire(redisKey, TimeUnit.SECONDS);

} catch (Exception e) {

LOG.error(“getExpire [Key:{0}][handleAutoRenew:{1}] into redis failed!”, e, redisKey);

}

return 0l;

}

@Override

public String hget(String key, String hKey) {

String redisKey = this.rebuildKey(key);

try {

return (String) stringRedisTemplate.opsForHash().get(redisKey, hKey);

} catch (Exception e) {

LOG.error("hget key:[{0}] hkey:[{1}] fail ", e, redisKey, hKey);

}

return null;

}

@Override

public Map<String, String> hscan(String key, String pattern) {

String redisKey = this.rebuildKey(key);

Cursor<Map.Entry<Object, Object>> cursor = null;

try {

ScanOptions scanOptions = null;

if (StringUtils.isEmpty(pattern))

scanOptions = ScanOptions.NONE;

else

scanOptions = ScanOptions.scanOptions().match(pattern).build();

cursor = stringRedisTemplate.opsForHash().scan(redisKey, scanOptions);

Map<String, String> map = new HashMap<>();

while (cursor.hasNext()) {

Map.Entry<Object, Object> entry = cursor.next();

Object value = entry.getValue();

map.put(entry.getKey().toString(), null != value ? value.toString() : null);

}

return map;

} catch (Exception e) {

LOG.error("hscan key:[{0}] fail ", e, redisKey);

} finally {

if (null != cursor) {

try {

cursor.close();

} catch (IOException e) {

LOG.error("hscan close cursor fail ", e);

}

}

}

return null;

}

@Override

public boolean hset(String key, String hKey, String hValue) {

String redisKey = this.rebuildKey(key);

try {

stringRedisTemplate.opsForHash().put(redisKey, hKey, hValue);

return true;

} catch (Exception e) {

LOG.error("hset key:[{0}] hkey:[{1}] fail ", e, redisKey, hKey);

}

return true;

}

@Override

public boolean hset(String key, String hKey, String hValue, long timeout) {

String redisKey = this.rebuildKey(key);

try {

stringRedisTemplate.opsForHash().put(redisKey, hKey, hValue);

stringRedisTemplate.expire(redisKey, timeout, TimeUnit.SECONDS);

return true;

} catch (Exception e) {

LOG.error("hset key:[{0}] hkey:[{1}] fail ", e, redisKey, hKey);

}

return true;

}

@Override

public boolean hdel(String key, String hKey) {

String redisKey = this.rebuildKey(key);

try {

stringRedisTemplate.opsForHash().delete(redisKey, hKey);

return true;

} catch (Exception e) {

LOG.error("hdel key:[{0}] hkey:[{1}] fail ", e, redisKey, hKey);

}

return false;

}

@Override

public boolean sadd(String key, String… value) {

String redisKey = this.rebuildKey(key);

try {

Long add = stringRedisTemplate.opsForSet().add(redisKey, value);

return Objects.nonNull(add) && add > 0;

} catch (Exception e) {

LOG.error(“sadd value:[{0}] to key:[{1}] fail”, redisKey, value, e);

}

return false;

}

@Override

public Set smembers(String key) {

String redisKey = this.rebuildKey(key);

try {

return stringRedisTemplate.opsForSet().members(redisKey);

} catch (Exception e) {

LOG.error(“scard values from key:[{0}] fail”, redisKey, e);

}

return new HashSet();

}

@Override

public boolean szset(String key, String value, double score) {

String redisKey = this.rebuildKey(key);

try {

return stringRedisTemplate.opsForZSet().add(redisKey, value, score);

}catch (Exception e){

LOG.error(“szset value:[{0}] from key:[{1}] fail”, value, redisKey, e);

}

return false;

}

@Override

public Long szdel(String key, String… value) {

String redisKey = this.rebuildKey(key);

try {

return stringRedisTemplate.opsForZSet().remove(redisKey, value);

}catch (Exception e){

LOG.error(“szdel values:[{0}] from key:[{1}] fail”, value, redisKey, e);

}

return 0L;

}

@Override

public Set szrange(String key, long start, long end) {

String redisKey = this.rebuildKey(key);

try {

return stringRedisTemplate.opsForZSet().range(redisKey, start, end);

}catch (Exception e){

LOG.error(“szrange start:[{0}] end:[{1}] from key:[{2}] fail”, start, end, redisKey, e);

}

return null;

}

@Override

public Set szdelAndGet(String key, long start, long end) {

String redisKey = this.rebuildKey(key);

RLock lock = client.getLock(redisKey+“_Lock”);

try {

lock.lock();

ZSetOperations<String, String> operation = stringRedisTemplate.opsForZSet();

Set set = operation.range(redisKey, start, end);

if(null == set || set.size()==0)return set;

operation.remove(redisKey, set.toArray());

return set;

}catch (Exception e){

LOG.error(“szdelAndGet start:[{0}] end:[{1}] from key:[{2}] fail”, start, end, redisKey, e);

}finally {

lock.unlock();

}

return null;

}

@Override

public Double szscore(String key, String value) {

String redisKey = this.rebuildKey(key);

try {

return stringRedisTemplate.opsForZSet().score(redisKey, value);

}catch (Exception e){

LOG.error(“szscore value:[{0}] from key:[{1}] fail”, value, redisKey, e);

}

return null;

}

@Override

public Long lLeftPush(String key, String value) {

String redisKey = this.rebuildKey(key);

try {

return stringRedisTemplate.opsForList().leftPush(redisKey, value);

}catch (Exception e){

LOG.error(“lLeftPush value:[{0}] from key:[{1}] fail”, value, redisKey, e);

}

return null;

}

@Override

public Long lLeftPushAll(String key, Collection values) {

String redisKey = this.rebuildKey(key);

try {

return stringRedisTemplate.opsForList().leftPushAll(key, values);

}catch (Exception e){

LOG.error(“lLeftPushAll values:[{0}] from key:[{1}] fail”, values, redisKey, e);

}

return null;

}

@Override

public String lRightPop(String key, long expire, TimeUnit unit) {

String redisKey = this.rebuildKey(key);

try {

return stringRedisTemplate.opsForList().rightPop(key, expire, unit);

}catch (Exception e){

LOG.error(“lRightPop key:[{1}] fail”, redisKey, e);

}

return null;

}

@Override

public RLock getLock(String key) {

String redisKey = this.rebuildKey(key);

try {

RLock lock = client.getLock(redisKey);

return lock;

} catch (Exception e) {

LOG.error("getLock key:[{0}] fail ", e, redisKey);

}

return null;

}

///

// private functions

///

private String rebuildKey(String redisKey) {

return namespace + redisKey;

}

}

10、application-local.properties代码

#==========================================

tomcat

#==========================================

server.port=10030

#==========================================

cloud

#==========================================

spring.application.name=iot-sys

spring.cloud.nacos.discovery.server-addr=localhost:8848

spring.main.allow-bean-definition-overriding=true

spring.servlet.multipart.enabled=true

spring.servlet.multipart.max-file-size=1024MB

spring.servlet.multipart.max-request-size=1024MB

#==========================================

jackson

#==========================================

spring.jackson.property-naming-strategy=SNAKE_CASE

#==========================================

redis

#==========================================

spring.redis.host=localhost

小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
img

====================================

spring.application.name=iot-sys

spring.cloud.nacos.discovery.server-addr=localhost:8848

spring.main.allow-bean-definition-overriding=true

spring.servlet.multipart.enabled=true

spring.servlet.multipart.max-file-size=1024MB

spring.servlet.multipart.max-request-size=1024MB

#==========================================

jackson

#==========================================

spring.jackson.property-naming-strategy=SNAKE_CASE

#==========================================

redis

#==========================================

spring.redis.host=localhost

小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-DpEqLnwX-1710843262493)]
[外链图片转存中…(img-fYpF1Nzg-1710843262493)]
[外链图片转存中…(img-VTj16QZH-1710843262494)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
[外链图片转存中…(img-6E7a04ly-1710843262494)]

  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot可以通过阿里云OSS(Object Storage Service)实现文件上传,以下是实现步骤: 1. 引入阿里云OSS SDK依赖 在pom.xml中引入以下依赖: ```xml <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.10.2</version> </dependency> ``` 2. 配置OSS连接信息 在application.properties文件中配置OSS连接信息: ```properties oss.endpoint=your-endpoint oss.accessKeyId=your-access-key-id oss.accessKeySecret=your-access-key-secret oss.bucketName=your-bucket-name ``` 3. 创建OSS客户端 在配置类中创建OSS客户端: ```java @Configuration public class OSSConfiguration { @Value("${oss.endpoint}") private String endpoint; @Value("${oss.accessKeyId}") private String accessKeyId; @Value("${oss.accessKeySecret}") private String accessKeySecret; @Bean public OSSClient ossClient() { return new OSSClient(endpoint, accessKeyId, accessKeySecret); } } ``` 4. 实现上传接口 ```java @RestController public class UploadController { @Autowired private OSSClient ossClient; @Value("${oss.bucketName}") private String bucketName; @PostMapping("/upload") public String upload(@RequestParam("file") MultipartFile file) { String fileName = file.getOriginalFilename(); try { ossClient.putObject(bucketName, fileName, new ByteArrayInputStream(file.getBytes())); return "success"; } catch (IOException e) { e.printStackTrace(); } finally { ossClient.shutdown(); } return "fail"; } } ``` 以上就是通过阿里云OSS实现文件上传的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值