| | |
| | | package com.kidgrow.filecenter.service.impl; |
| | | |
| | | import cn.hutool.core.util.IdUtil; |
| | | import com.aliyun.oss.model.ObjectMetadata; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.kidgrow.common.constant.CommonConstant; |
| | | import com.kidgrow.common.model.PageResult; |
| | | import com.kidgrow.common.model.ResultBody; |
| | | import com.kidgrow.common.utils.DateUtil; |
| | | import com.kidgrow.common.utils.MultipartFileUtils; |
| | | import com.kidgrow.common.utils.RandomValueUtils; |
| | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import net.coobird.thumbnailator.Thumbnails; |
| | | import org.apache.commons.collections4.MapUtils; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.web.bind.annotation.RequestBody; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import sun.misc.BASE64Decoder; |
| | | import sun.misc.BASE64Encoder; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.InputStream; |
| | | import java.io.*; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.UUID; |
| | | |
| | | /** |
| | | * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br> |
| | |
| | | return fileInfo; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public FileInfo baseUplaod(String file, String imgType) { |
| | | if(file==null||"".equals(file.trim())){ |
| | | return null; |
| | | } |
| | | String name=file.split(",")[0]; |
| | | file = file.split(",")[1]; |
| | | BASE64Decoder decoder = new BASE64Decoder(); |
| | | try { |
| | | byte[] bytes = decoder.decodeBuffer(file); |
| | | ObjectMetadata objectMeta = new ObjectMetadata(); |
| | | objectMeta.setContentLength(file.length()); |
| | | //将字节码转换成流 |
| | | InputStream input = new ByteArrayInputStream(bytes); |
| | | String fileName= UUID.randomUUID().toString().replaceAll("-",""); |
| | | //image/jpeg;base64 |
| | | String newFileName=fileName+"."+name.substring(name.lastIndexOf("/")+1,name.lastIndexOf(";")); |
| | | FileInfo newfileInfo=new FileInfo(); |
| | | newfileInfo.setName(newFileName); |
| | | newfileInfo.setId(IdUtil.fastSimpleUUID()); |
| | | newfileInfo.setSize(file.length()); |
| | | newfileInfo.setIsImg(true); |
| | | newfileInfo.setContentType("image/jpeg"); |
| | | newfileInfo.setSource("aliyun"); |
| | | newfileInfo.setImgType(imgType); |
| | | newfileInfo.setCreateTime(new Date()); |
| | | this.fileUpLoadOss(newfileInfo,"HospitalLogo/"+newFileName,input); |
| | | input.close(); |
| | | baseMapper.insert(newfileInfo); |
| | | return newfileInfo; |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public ResultBody baseDownLoad(Map<String, Object> map) { |
| | | 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(); |
| | | //返回Base64编码过的字节数组字符串 |
| | | String encode = encoder.encode(data); |
| | | return ResultBody.ok().data(encode); |
| | | }else { |
| | | return ResultBody.failed("获取数据失败"); |
| | | } |
| | | } |
| | | } |
| | | |