package com.gkhy.exam.system.service.impl; import cn.hutool.core.date.DateUtil; import com.gkhy.exam.common.exception.ApiException; import com.gkhy.exam.system.domain.vo.UploadObjectVO; import com.gkhy.exam.system.service.SysCommonService; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import sun.misc.BASE64Decoder; import java.io.*; import java.util.Date; import java.util.UUID; @Service public class SysCommonServiceImpl implements SysCommonService { @Value("${image.upload_image}") private String uploadPath; @Override public UploadObjectVO uploadFile(MultipartFile file) { if(file==null){ throw new ApiException("上传文件不能为空"); } UploadObjectVO uploadObjectVO=doUpload(file); return uploadObjectVO; } @Override public boolean removeFile(String path) { String systemDir=System.getProperty("user.dir"); String filePath=systemDir+File.separator+path; File dirFile=new File(filePath); if(!dirFile.exists()){ throw new ApiException("文件不存在"); } if(!dirFile.isFile()){ throw new ApiException("非文件,不能删除"); } dirFile.delete(); return true; } @Override public UploadObjectVO doUpload(MultipartFile file){ String originName=file.getOriginalFilename(); String filename=originName; String subfix=filename.substring(filename.lastIndexOf(".")); Long size=file.getSize(); filename= UUID.randomUUID().toString().replace("-","")+subfix; String systemDir=System.getProperty("user.dir"); String dateStr= DateUtil.format(new Date(),"yyyyMMdd"); String filePath=uploadPath+File.separator+dateStr; File dirFile=new File(filePath); if(!dirFile.exists()){ dirFile.mkdirs(); } filePath=filePath+File.separator+filename; try { file.transferTo(new File(systemDir+File.separator+filePath)); } catch (FileNotFoundException e) { throw new ApiException("找不到文件"); } catch (IOException e) { throw new ApiException("发生错误,请联系管理员"); } filePath=filePath.replace("\\","/"); UploadObjectVO uploadObjectVO=new UploadObjectVO().setFilename(filename) .setPath(filePath).setOriginName(originName).setSize(size); return uploadObjectVO; } @Override public UploadObjectVO doUpload(String imageBase64) { String originName=""; String filename=originName; String subfix=".png"; filename= UUID.randomUUID().toString().replace("-","")+subfix; String systemDir=System.getProperty("user.dir"); String dateStr= DateUtil.format(new Date(),"yyyyMMdd"); String filePath=uploadPath+File.separator+dateStr; File dirFile=new File(filePath); if(!dirFile.exists()){ dirFile.mkdirs(); } filePath=filePath+File.separator+filename; BASE64Decoder decoder = new BASE64Decoder(); try { // Base64解码 byte[] bytes = decoder.decodeBuffer(imageBase64); for (int i = 0; i < bytes.length; ++i) { if (bytes[i] < 0) {// 调整异常数据 bytes[i] += 256; } } // 生成jpeg图片 OutputStream out = new FileOutputStream(systemDir+File.separator+filePath); out.write(bytes); out.flush(); out.close(); } catch (FileNotFoundException e) { throw new ApiException("找不到文件"); } catch (IOException e) { throw new ApiException("发生错误,请联系管理员"); } filePath=filePath.replace("\\","/"); UploadObjectVO uploadObjectVO=new UploadObjectVO().setFilename(filename) .setPath(filePath).setOriginName(originName); return uploadObjectVO; } }