package com.quinn.service.impl; 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 com.quinn.utils.RedisUtils; 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; /** *

* 服务实现类 *

* * @author limqsh * @since 2022-05-03 */ @Service public class SourceServiceImpl extends ServiceImpl implements SourceService { @Resource OSSClientUtil ossClientUtil; @Resource RedisUtils redisUtils; @Override public void downloadSource(ServletOutputStream outputStream, Source source) throws IOException { String sourceLink = source.getSourceLink(); addDownLoadRecord(source); // 读取文件内容。 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(); } } /** * 更新页码 * @param source */ private void addDownLoadRecord(Source source) { // Integer downLoadTime = (Integer) redisUtils.get(QuinnConstant.SOURCE_KEY + source.getId()); // int downTimes = 0; // if (StringUtils.isEmpty(downLoadTime)){ // downTimes = source.getDownRecord() + 1; // }else { // downTimes = downLoadTime + 1; // } // redisUtils.set(QuinnConstant.SOURCE_KEY + source.getId(),downTimes); source.setDownRecord(source.getDownRecord() + 1); updateById(source); } }