资源下载链路打通
This commit is contained in:
52
src/main/java/com/quinn/service/impl/SourceServiceImpl.java
Normal file
52
src/main/java/com/quinn/service/impl/SourceServiceImpl.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package com.quinn.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.quinn.pojo.Source;
|
||||
import com.quinn.mapper.SourceMapper;
|
||||
import com.quinn.service.SourceService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.quinn.utils.OSSClientUtil;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 服务实现类
|
||||
* </p>
|
||||
*
|
||||
* @author limqsh
|
||||
* @since 2022-05-03
|
||||
*/
|
||||
@Service
|
||||
public class SourceServiceImpl extends ServiceImpl<SourceMapper, Source> implements SourceService {
|
||||
|
||||
@Resource
|
||||
OSSClientUtil ossClientUtil;
|
||||
|
||||
@Override
|
||||
public void downloadSource(ServletOutputStream outputStream, String sid) throws IOException {
|
||||
Source source = getOne(new QueryWrapper<Source>().eq("id", sid));
|
||||
String sourceLink = source.getSourceLink();
|
||||
// 读取文件内容。
|
||||
BufferedInputStream in = new BufferedInputStream(ossClientUtil.downloadFile(sourceLink));
|
||||
BufferedOutputStream out = new BufferedOutputStream(outputStream);
|
||||
byte[] buffer = new byte[1024];
|
||||
int lenght = 0;
|
||||
while ((lenght = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, lenght);
|
||||
}
|
||||
if (out != null) {
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
if (in != null) {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user