| | |
| | | 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.multipart.MultipartFile; |
| | | import sun.misc.BASE64Decoder; |
| | | import sun.misc.BASE64Encoder; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | |
| | | import java.io.*; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.UUID; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br> |
| | |
| | | @Slf4j |
| | | public abstract class AbstractIFileService extends ServiceImpl<FileMapper, FileInfo> implements IFileService { |
| | | private static final String FILE_SPLIT = "."; |
| | | private static Logger logger = LoggerFactory.getLogger(AbstractIFileService.class); |
| | | |
| | | @Override |
| | | public FileInfo upload(MultipartFile file) throws Exception { |
| | |
| | | @Override |
| | | public FileInfo uploadForThumbnails(MultipartFile file, String imgType) throws Exception { |
| | | FileInfo fileInfo=this.upload(file,imgType); |
| | | List<String> lassStr= Arrays.asList("png","bmp","dib","gif","jfif","jpe","jpeg","jpg","tif","tiff","ico"); |
| | | String suffix=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1); |
| | | if(!lassStr.contains(suffix)){ |
| | | return null; |
| | | } |
| | | if (StringUtils.isNotBlank(fileInfo.getUrl())) { |
| | | //生成缩略图上传 |
| | | File nowFile= MultipartFileUtils.multipartFileToFile(file); |
| | | // File nowFile= MultipartFileUtils.multipartFileToFile(file); |
| | | // File nowFile= new File(file.getOriginalFilename()); |
| | | //暂存目录 发布后真实存在的磁盘目录 |
| | | String result = CommonConstant.TEMP_IMAGE_PATH; |
| | | //文件扩展名 |
| | | String suffix=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); |
| | | String newFileName= RandomValueUtils.uuid()+suffix; |
| | | String newFileName= RandomValueUtils.uuid()+"."+suffix; |
| | | //生成缩略图 |
| | | Thumbnails.of(nowFile).size(CommonConstant.TH_IMG_WIDTH, CommonConstant.TH_IMG_HEIGHT).toFile(result+newFileName); |
| | | Thumbnails.of(file.getInputStream()).size(CommonConstant.TH_IMG_WIDTH, CommonConstant.TH_IMG_HEIGHT).toFile(result+newFileName); |
| | | File thuFile= new File(result+newFileName); |
| | | if (thuFile.exists()) { |
| | | //将生成的文件转换为流 |
| | | InputStream inputStream=new FileInputStream(new File(result+newFileName)); |
| | | InputStream inputStream=new FileInputStream(thuFile); |
| | | String folderByDate = DateUtil.formatDate(new Date()); |
| | | String filefloder=this.FilePath(imgType,folderByDate); |
| | | FileInfo newfileInfo=new FileInfo(); |
| | |
| | | //文件上传到oss |
| | | this.fileUpLoadOss(newfileInfo,filefloder+newFileName,inputStream); |
| | | inputStream.close(); |
| | | //删除暂存文件 |
| | | MultipartFileUtils.delteTempFile(thuFile); |
| | | // //删除暂存文件 |
| | | // MultipartFileUtils.delteTempFile(thuFile); |
| | | boolean delete = thuFile.delete(); |
| | | |
| | | // 将文件信息保存到数据库 |
| | | baseMapper.insert(newfileInfo); |
| | | fileInfo.setPath(newfileInfo.getUrl()); |
| | |
| | | } |
| | | 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("获取数据失败"); |
| | | } |
| | | } |
| | | } |
| | | |