| | |
| | | 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 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 com.kidgrow.common.utils.StringUtils; |
| | | import com.kidgrow.filecenter.mapper.FileMapper; |
| | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import net.coobird.thumbnailator.Thumbnails; |
| | | import org.apache.commons.collections4.MapUtils; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | 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.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.io.*; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | 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); |
| | | |
| | | @Value("${kidgrow.file-server.oss.domain}") |
| | | private String DOMAIN; |
| | | @Value("${spring.profiles.active}") |
| | | private String OssPathEn; |
| | | @Override |
| | | public FileInfo upload(MultipartFile file) throws Exception { |
| | | FileInfo fileInfo = FileUtil.getFileInfo(file); |
| | |
| | | */ |
| | | 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 String FilePath(String imgType,String folderByDate,String hospitalId,String departmentId); |
| | | protected abstract OSSObject down(String url); |
| | | |
| | | /** |
| | | * 删除文件 |
| | |
| | | @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); |
| | | String filefloder=this.FilePath(imgType,folderByDate,null,null); |
| | | FileInfo newfileInfo=new FileInfo(); |
| | | newfileInfo.setName(newFileName); |
| | | newfileInfo.setId(IdUtil.fastSimpleUUID()); |
| | |
| | | newfileInfo.setCreateTime(new Date()); |
| | | //文件上传到oss |
| | | this.fileUpLoadOss(newfileInfo,filefloder+newFileName,inputStream); |
| | | inputStream.close(); |
| | | // //删除暂存文件 |
| | | // MultipartFileUtils.delteTempFile(thuFile); |
| | | boolean delete = thuFile.delete(); |
| | | |
| | | // 将文件信息保存到数据库 |
| | | baseMapper.insert(newfileInfo); |
| | | fileInfo.setPath(newfileInfo.getUrl()); |
| | | //删除暂存文件 |
| | | MultipartFileUtils.delteTempFile(thuFile); |
| | | inputStream.close(); |
| | | } |
| | | return fileInfo; |
| | | } |
| | |
| | | return fileInfo; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public FileInfo baseUplaod(String file, String imgType, String hospitalId, String departmentId) { |
| | | 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()); |
| | | String folderByDate =DateUtil.formatDate(new Date()); |
| | | String filePath = FilePath(imgType, folderByDate, hospitalId, departmentId); |
| | | this.fileUpLoadOss(newfileInfo,filePath+newFileName,input); |
| | | input.close(); |
| | | baseMapper.insert(newfileInfo); |
| | | return newfileInfo; |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public ResultBody baseDownLoad(Map<String, Object> map) throws Exception { |
| | | String urlDiZhi=MapUtils.getString(map,"url"); |
| | | if(!urlDiZhi.contains(DOMAIN)&&!urlDiZhi.contains(DOMAIN.replace("https","http"))){ |
| | | ByteArrayOutputStream outPut = new ByteArrayOutputStream(); |
| | | byte[] data = new byte[1024]; |
| | | try { |
| | | // 创建URL |
| | | URL url = new URL(urlDiZhi); |
| | | // 创建链接 |
| | | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| | | conn.setRequestMethod("GET"); |
| | | conn.setConnectTimeout(10 * 1000); |
| | | InputStream inStream = conn.getInputStream(); |
| | | int len = -1; |
| | | while ((len = inStream.read(data)) != -1) { |
| | | outPut.write(data, 0, len); |
| | | } |
| | | inStream.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | String substring = urlDiZhi.substring(urlDiZhi.lastIndexOf(".") + 1, urlDiZhi.length()); |
| | | String str="data:image/" + substring + ";base64,"; |
| | | // 对字节数组Base64编码 |
| | | BASE64Encoder encoder = new BASE64Encoder(); |
| | | System.out.println(encoder.encode(outPut.toByteArray())); |
| | | return ResultBody.ok().data(str+encoder.encode(outPut.toByteArray()).replaceAll("\n", "").replaceAll("\r", "")); |
| | | }else { |
| | | List<FileInfo> fileInfos = baseMapper.selectByMap(map); |
| | | if (fileInfos.size() > 0) { |
| | | FileInfo fileInfo = fileInfos.get(0); |
| | | OSSObject down = this.down(fileInfo.getUrl().replace(DOMAIN, "").replace(DOMAIN.replace("https","http"),"")); |
| | | InputStream objectContent = down.getObjectContent(); |
| | | //返回Base64编码过的字节数组字符串 |
| | | 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(); |
| | | down.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("获取数据失败"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | |
| | | ByteArrayOutputStream outPut = new ByteArrayOutputStream(); |
| | | byte[] data = new byte[1024]; |
| | | try { |
| | | // 创建URL |
| | | URL url = new URL("http://192.168.2.25:8008/123/J-1.jpg"); |
| | | // 创建链接 |
| | | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| | | conn.setRequestMethod("GET"); |
| | | conn.setConnectTimeout(10 * 1000); |
| | | InputStream inStream = conn.getInputStream(); |
| | | int len = -1; |
| | | while ((len = inStream.read(data)) != -1) { |
| | | outPut.write(data, 0, len); |
| | | } |
| | | inStream.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | // 对字节数组Base64编码 |
| | | BASE64Encoder encoder = new BASE64Encoder(); |
| | | System.out.println(encoder.encode(outPut.toByteArray())); |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public FileInfo byteUplaod(String fileName, InputStream inputStream,int size) throws IOException { |
| | | String houzhui=fileName.substring(fileName.lastIndexOf(".")+1); |
| | | String newFileName= RandomValueUtils.uuid()+"."+houzhui; |
| | | FileInfo newfileInfo=new FileInfo(); |
| | | newfileInfo.setName(newFileName); |
| | | newfileInfo.setId(IdUtil.fastSimpleUUID()); |
| | | newfileInfo.setSize(size); |
| | | newfileInfo.setIsImg(false); |
| | | newfileInfo.setContentType("byte[]"); |
| | | newfileInfo.setSource("aliyun"); |
| | | newfileInfo.setImgType(houzhui); |
| | | newfileInfo.setCreateTime(new Date()); |
| | | this.fileUpLoadOss(newfileInfo,"pdf/"+newFileName,inputStream); |
| | | baseMapper.insert(newfileInfo); |
| | | inputStream.close(); |
| | | return newfileInfo; |
| | | } |
| | | |
| | | @Override |
| | | public FileInfo feignUploadHosIdAndDepId(MultipartFile file, String imgType, String hospitalId, String departmentId) throws Exception { |
| | | 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.toLowerCase())){ |
| | | return null; |
| | | } |
| | | String name = file.getOriginalFilename(); |
| | | String fileName= UUID.randomUUID().toString().replaceAll("-",""); |
| | | //image/jpeg;base64 |
| | | String newFileName=fileName+name.substring(name.lastIndexOf(".")); |
| | | FileInfo newfileInfo=new FileInfo(); |
| | | newfileInfo.setName(newFileName); |
| | | newfileInfo.setId(IdUtil.fastSimpleUUID()); |
| | | newfileInfo.setSize(file.getSize()); |
| | | newfileInfo.setIsImg(true); |
| | | newfileInfo.setContentType("image/jpeg"); |
| | | newfileInfo.setSource("aliyun"); |
| | | newfileInfo.setImgType(imgType); |
| | | newfileInfo.setCreateTime(new Date()); |
| | | String folderByDate =DateUtil.formatDate(new Date()); |
| | | String filePath = FilePath(imgType, folderByDate, hospitalId, departmentId); |
| | | InputStream inputStream = file.getInputStream(); |
| | | this.fileUpLoadOss(newfileInfo,filePath+newFileName,inputStream); |
| | | inputStream.close(); |
| | | baseMapper.insert(newfileInfo); |
| | | return newfileInfo; |
| | | } |
| | | } |
| | | |