zhanghua
2023-10-23 252b4943e54f7ef930f7f7ea01ad1a8a29655dcf
ycl-platform/src/main/java/com/ycl/controller/dingding/DingController.java
@@ -5,6 +5,9 @@
import com.alibaba.xxpt.gateway.shared.client.http.ExecutableClient;
import com.alibaba.xxpt.gateway.shared.client.http.GetClient;
import com.alibaba.xxpt.gateway.shared.client.http.PostClient;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.ycl.api.CommonResult;
import com.ycl.bo.AdminUserDetails;
import com.ycl.common.dingding.DingCommon;
@@ -14,23 +17,32 @@
import com.ycl.service.auth.AuthService;
import com.ycl.service.ding.BookRemarkService;
import com.ycl.service.ding.DingService;
import com.ycl.service.oss.OssService;
import com.ycl.utils.ConstantPropertiesUtils;
import com.ycl.utils.common.RandomUtils;
import com.ycl.vo.AddressBookVO;
import com.ycl.vo.NewAddressBookVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -69,6 +81,9 @@
    @Resource
    private DingConfig dingConfig;
    @Autowired
    private OssService ossService;
    @ApiOperation(value = "根据钉钉授权码获取token")
    @GetMapping("/dingLogin")
@@ -169,47 +184,118 @@
        return CommonResult.success(bookRemarkService.updateRemark(user, userId, remark));
    }
    private String getVideo(String accToken) {
    @ApiOperation(value = "获取视频")
    @GetMapping("/getVideo/{mediaId}")
    public CommonResult<String> getVideo(@PathVariable String mediaId) {
        String accessToken = getToken();
        String ticket = getTiker(accessToken);
        MultipartFile file = getDownload(ticket, mediaId);
        String url = ossService.uploadImages(file);
        return CommonResult.success(url);
    }
    private MultipartFile getDownload(String access_token, String media_id) {
        CloseableHttpResponse response = null;
        //调用API
        GetClient getClient = executableClient.newGetClient(GET_VIDEO);
        //设置参数
        try {
            if (ObjectUtil.isNotNull(accToken)) {
            getClient.addParameter("accessToken", accToken);
            getClient.addParameter("mediaId", dingConfig.getMediaId());
//            String result = getClient.get();
//            return parsingResult(result);
            String api = "/media/download";
            GetClient getClient = executableClient.newGetClient(api);
            //设置参数
            getClient.addParameter("access_token", access_token);
            getClient.addParameter("media_id", media_id);
            response = getClient.getB();
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            File voice = new File("/Users/video/log1.xlsx");
            OutputStream fos = new FileOutputStream(voice);
            int cache = 10 * 1024;
            byte[] buffer = new byte[cache];
            int ch = 0;
                while ((ch = is.read(buffer)) != -1) {
                fos.write(buffer, 0, ch);
                }
            is.close();
            fos.flush();
            fos.close();
            }
//            File voice = new File("D:/IMG_4401.MOV");
//            OutputStream fos = new FileOutputStream(voice);
//            int cache = 10 * 1024;
//            byte[] buffer = new byte[cache];
//            int ch = 0;
//            while ((ch = is.read(buffer)) != -1) {
//                fos.write(buffer, 0, ch);
//            }
//            is.close();
//            fos.flush();
//            fos.close();
            DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
            String strDate2 = dtf2.format(LocalDateTime.now());
            String fileName = strDate2 + RandomUtils.generateRandomInt(4) + ".MOV";
            return getMultipartFile(is, fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            System.out.println(e);
        } finally {
            if (response != null) {
                try {
                    //特别提醒:需要调用response的close方法关闭网络连接!!!
                    response.close();
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println(e);
                }
            }
        }
        return null;
    }
    /**
     * 获取封装得MultipartFile
     *
     * @param inputStream inputStream
     * @param fileName    fileName
     * @return MultipartFile
     */
    public MultipartFile getMultipartFile(InputStream inputStream, String fileName) {
        FileItem fileItem = createFileItem(inputStream, fileName);
        //CommonsMultipartFile是feign对multipartFile的封装,但是要FileItem类对象
        return new CommonsMultipartFile(fileItem);
    }
    /**
     * FileItem类对象创建
     *
     * @param inputStream inputStream
     * @param fileName    fileName
     * @return FileItem
     */
    public FileItem createFileItem(InputStream inputStream, String fileName) {
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        String textFieldName = "file";
        FileItem item = factory.createItem(textFieldName, MediaType.MULTIPART_FORM_DATA_VALUE, true, fileName);
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        OutputStream os = null;
        //使用输出流输出输入流的字节
        try {
            os = item.getOutputStream();
            while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            inputStream.close();
        } catch (IOException e) {
            log.error("Stream copy exception", e);
            throw new IllegalArgumentException("文件上传失败");
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    log.error("Stream close exception", e);
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    log.error("Stream close exception", e);
                }
            }
        }
        return item;
    }
}