kongzy
2024-07-12 28aaf2ffa1dbb860a292ba330a7e9362e60e7832
assess-system/src/main/java/com/gkhy/assess/system/service/impl/SysCommonServiceImpl.java
@@ -1,16 +1,20 @@
package com.gkhy.assess.system.service.impl;
import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.gkhy.assess.common.exception.ApiException;
import com.gkhy.assess.system.excel.ExpertExcelListener;
import com.gkhy.assess.system.domain.vo.SysExpertInfoExcelVO;
import com.gkhy.assess.system.domain.vo.UploadObjectVO;
import com.gkhy.assess.system.service.SysCommonService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.*;
import java.util.Date;
import java.util.UUID;
@@ -22,6 +26,9 @@
    @Override
    public UploadObjectVO uploadFile(MultipartFile file) {
        if(file==null){
            throw new ApiException("上传文件不能为空");
        }
        UploadObjectVO uploadObjectVO=doUpload(file);
        return uploadObjectVO;
    }
@@ -41,8 +48,20 @@
        return true;
    }
    @Override
    public void importExcel() throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("abc.xlsx");
        InputStream inputStream = classPathResource.getInputStream();
        if(inputStream==null){
            throw new ApiException("获取文件失败");
        }
        EasyExcel.read(inputStream, SysExpertInfoExcelVO.class,new ExpertExcelListener()).sheet(0).doRead();
    }
    @Override
    public UploadObjectVO doUpload(MultipartFile file){
        String filename=file.getOriginalFilename();
        String originName=file.getOriginalFilename();
        String filename=originName;
        String subfix=filename.substring(filename.lastIndexOf("."));
        filename= UUID.randomUUID().toString().replace("-","")+subfix;
        String systemDir=System.getProperty("user.dir");
@@ -62,24 +81,47 @@
        }
        filePath=filePath.replace("\\","/");
        UploadObjectVO uploadObjectVO=new UploadObjectVO().setFilename(filename)
                .setPath(filePath);
                .setPath(filePath).setOriginName(originName);
        return uploadObjectVO;
    }
    public boolean checkImageType(String subfix){
        if(".jpg".equalsIgnoreCase(subfix)||
                ".jpeg".equalsIgnoreCase(subfix)||
                ".png".equalsIgnoreCase(subfix)||
                ".bmp".equalsIgnoreCase(subfix)||
                ".tif".equalsIgnoreCase(subfix)||
                ".jfif".equalsIgnoreCase(subfix)||
                ".webp".equalsIgnoreCase(subfix)||
                ".gif".equalsIgnoreCase(subfix)
        ){
            return true;
    @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();
        }
        return false;
        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;
    }
}