| | |
| | | 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.*; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | |
| | | private DingConfig dingConfig; |
| | | |
| | | |
| | | |
| | | @Autowired |
| | | private OssService ossService; |
| | | |
| | | @ApiOperation(value = "æ ¹æ®ééææç è·åtoken") |
| | | @GetMapping("/dingLogin") |
| | |
| | | return CommonResult.success(bookRemarkService.updateRemark(user, userId, remark)); |
| | | } |
| | | |
| | | private String getVideo(String MediaId,MultipartFile file) { |
| | | @ApiOperation(value = "é讯å½éå½") |
| | | @GetMapping("/getVideo/{mediaId}") |
| | | public CommonResult<String> getVideo(@PathVariable String mediaId) { |
| | | String accessToken = getToken(); |
| | | |
| | | MultipartFile file = getDownload(accessToken, 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); |
| | | String token =getToken(); |
| | | //è®¾ç½®åæ° |
| | | try { |
| | | if (ObjectUtil.isNotNull(MediaId)) { |
| | | getClient.addParameter("accessToken", token); |
| | | getClient.addParameter("mediaId", MediaId); |
| | | // String result = getClient.get(); |
| | | // return parsingResult(result); |
| | | String api = "/media/download"; |
| | | GetClient getClient = executableClient.newGetClient(api); |
| | | //è®¾ç½®åæ° |
| | | getClient.addParameter("access_token", "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ7XCJ1c2VySWRcIjozLFwidXNlcm5hbWVcIjpcImFkbWluXCJ9IiwiY3JlYXRlZCI6MTY5NDY2NDE5MjA4MiwiZXhwIjoxNjk1MjY4OTkyfQ.fSc0sEfLhD3pgrguYZ0Q4Tr-0wjZNwY2xnGsaGzN1d4oXMnEoX9MJmDQcwEiZ1uLKZbGQCqUtJciAIvINmNq9w "); |
| | | 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"); |
| | | 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) { |
| | | while ((ch = is.read(buffer)) != -1) { |
| | | fos.write(buffer, 0, ch); |
| | | } |
| | | } |
| | | is.close(); |
| | | fos.flush(); |
| | | fos.close(); |
| | | } |
| | | if (file == null) { |
| | | return "ä¸ä¼ è§é¢ä¸ºç©º"; |
| | | } |
| | | String endpoint = ConstantPropertiesUtils.END_POINT; |
| | | String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID; |
| | | String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET; |
| | | String bucketName = ConstantPropertiesUtils.BUCKET_NAME; |
| | | DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); |
| | | |
| | | // å建OSSClientå®ä¾ |
| | | OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
| | | // è·åæä»¶çåç§° |
| | | String fileName = "sczf/" + file.getOriginalFilename(); |
| | | ObjectMetadata objectMetadata = new ObjectMetadata(); |
| | | objectMetadata.setContentType(getcontentType(fileName.substring(fileName.lastIndexOf(".")))); |
| | | // è°ç¨ossçæ¹æ³å®ç°é¿ä¼ |
| | | // 第ä¸ä¸ªåæ° bucketName |
| | | // 第äºä¸ªåæ° ä¸ä¼ å°ossçæä»¶è·¯å¾åæä»¶åç§° |
| | | ossClient.putObject(bucketName, fileName, new ByteArrayInputStream(file.getBytes()), objectMetadata); |
| | | // å
³éOSSClientã |
| | | ossClient.shutdown(); |
| | | // æä¸ä¼ çæä»¶è·¯å¾è¿å ï¼æå¨æ¼æ¥ï¼ |
| | | // è¿é设置å¾çæææ¶é´ æè®¾ç½®äº30å¹´ |
| | | Date expiration = new Date(System.currentTimeMillis() + (long) 946080000 * 1000); |
| | | String url = ossClient.generatePresignedUrl(bucketName, fileName, expiration).toString(); |
| | | String strDate2 = dtf2.format(LocalDateTime.now()); |
| | | String fileName = strDate2 + RandomUtils.generateRandomInt(4) + ".MOV"; |
| | | return getMultipartFile(is, fileName); |
| | | |
| | | return url; |
| | | }catch (Exception e) { |
| | | e.printStackTrace(); |
| | | }finally { |
| | | } catch (Exception e) { |
| | | System.out.println(e); |
| | | } finally { |
| | | if (response != null) { |
| | | try { |
| | | //ç¹å«æéï¼éè¦è°ç¨responseçcloseæ¹æ³å
³éç½ç»è¿æ¥ï¼ï¼ï¼ |
| | | response.close(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | System.out.println(e); |
| | | } |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | public static String getcontentType(String FilenameExtension) { |
| | | if (FilenameExtension.equalsIgnoreCase(".mp4")||FilenameExtension.equalsIgnoreCase(".mov")) { |
| | | return "video/mp4"; |
| | | } |
| | | if (FilenameExtension.equalsIgnoreCase(".bmp")) { |
| | | return "image/bmp"; |
| | | } |
| | | if (FilenameExtension.equalsIgnoreCase(".gif")) { |
| | | return "image/gif"; |
| | | } |
| | | if (FilenameExtension.equalsIgnoreCase(".jpeg") || |
| | | FilenameExtension.equalsIgnoreCase(".jpg") || |
| | | FilenameExtension.equalsIgnoreCase(".png")) { |
| | | return "image/jpg"; |
| | | } |
| | | if (FilenameExtension.equalsIgnoreCase(".html")) { |
| | | return "text/html"; |
| | | } |
| | | if (FilenameExtension.equalsIgnoreCase(".txt")) { |
| | | return "text/plain"; |
| | | } |
| | | if (FilenameExtension.equalsIgnoreCase(".vsd")) { |
| | | return "application/vnd.visio"; |
| | | } |
| | | if (FilenameExtension.equalsIgnoreCase(".pptx") || |
| | | FilenameExtension.equalsIgnoreCase(".ppt")) { |
| | | return "application/vnd.ms-powerpoint"; |
| | | } |
| | | if (FilenameExtension.equalsIgnoreCase(".docx") || |
| | | FilenameExtension.equalsIgnoreCase(".doc")) { |
| | | return "application/msword"; |
| | | } |
| | | if (FilenameExtension.equalsIgnoreCase(".xml")) { |
| | | return "text/xml"; |
| | | } |
| | | return "image/jpg"; |
| | | |
| | | /** |
| | | * è·åå°è£
å¾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; |
| | | } |
| | | } |
| | | |