| | |
| | | package com.kidgrow.filecenter.service.impl; |
| | | |
| | | import cn.hutool.core.util.IdUtil; |
| | | import com.aliyun.oss.model.OSSObject; |
| | | import com.aliyun.oss.model.ObjectMetadata; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | |
| | | import org.apache.commons.collections4.MapUtils; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import sun.misc.BASE64Decoder; |
| | | import sun.misc.BASE64Encoder; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | |
| | |
| | | protected abstract void uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception; |
| | | protected abstract void fileUpLoadOss(FileInfo fileInfo,String newFilePath, InputStream fileStream); |
| | | protected abstract String FilePath(String imgType,String folderByDate); |
| | | protected abstract OSSObject down(String url); |
| | | |
| | | /** |
| | | * 删除文件 |
| | |
| | | } |
| | | |
| | | @Override |
| | | public ResultBody baseDownLoad(Map<String, Object> map) { |
| | | public ResultBody baseDownLoad(Map<String, Object> map) throws Exception { |
| | | List<FileInfo> fileInfos = baseMapper.selectByMap(map); |
| | | if(fileInfos.size()>0){ |
| | | FileInfo fileInfo = fileInfos.get(0); |
| | | InputStream in = null; |
| | | byte[] data = null; |
| | | //读取图片字节数组 |
| | | try{ |
| | | in = new FileInputStream(fileInfo.getUrl()); |
| | | data = new byte[in.available()]; |
| | | in.read(data); |
| | | in.close(); |
| | | }catch (IOException e){ |
| | | e.printStackTrace(); |
| | | } |
| | | //对字节数组Base64编码 |
| | | BASE64Encoder encoder = new BASE64Encoder(); |
| | | OSSObject down1 = this.down(fileInfo.getUrl().replace("https://kidgrow.oss-accelerate.aliyuncs.com/","")); |
| | | InputStream objectContent = down1.getObjectContent(); |
| | | //返回Base64编码过的字节数组字符串 |
| | | String encode = encoder.encode(data); |
| | | byte[] data = null; |
| | | // 读取图片字节数组 |
| | | try { |
| | | ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); |
| | | byte[] buff = new byte[100]; |
| | | int rc = 0; |
| | | while ((rc = objectContent.read(buff, 0, 100)) > 0) { |
| | | swapStream.write(buff, 0, rc); |
| | | } |
| | | data = swapStream.toByteArray(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | if (objectContent != null) { |
| | | try { |
| | | objectContent.close(); |
| | | down1.close(); |
| | | } catch (IOException e) { |
| | | throw new Exception("输入流关闭异常"); |
| | | } |
| | | } |
| | | } |
| | | String encode="data:"+fileInfo.getContentType()+";base64,"+Base64.getEncoder().encodeToString(data); |
| | | return ResultBody.ok().data(encode); |
| | | }else { |
| | | return ResultBody.failed("获取数据失败"); |
| | | } |
| | | } |
| | | |
| | | } |
| | | |