15 changed files with 292 additions and 27 deletions
			
			
		| @ -0,0 +1,124 @@ | |||
| package com.elink.esua.epdc.commons.tools.utils; | |||
| 
 | |||
| import cn.hutool.http.HttpResponse; | |||
| import com.alibaba.fastjson.JSONObject; | |||
| import org.apache.commons.codec.binary.Base64; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.springframework.http.HttpEntity; | |||
| import sun.net.www.http.HttpClient; | |||
| 
 | |||
| import java.io.*; | |||
| import java.net.HttpURLConnection; | |||
| import java.net.URL; | |||
| import java.util.Map; | |||
| 
 | |||
| /** | |||
|  * @Author LC | |||
|  * @Date 2019/9/11 14:53 | |||
|  */ | |||
| public class UploadImageUtils { | |||
| 
 | |||
|     /** | |||
|      * 从微信下载图片 | |||
|      * @Params: [params] | |||
|      * @Return: java.lang.String | |||
|      * @Author: liuchuang | |||
|      * @Date: 2019/9/11 17:04 | |||
|      */ | |||
|     public static String downloadImageFromWx(Map<String, String> params) throws Exception { | |||
|         InputStream inputStream = getMediaStream(params.get("accessTokenUrl"), params.get("mediaUrl"), params.get("mediaId"), params.get("appid"), params.get("secret")); | |||
|         if (null == inputStream) { | |||
|             return null; | |||
|         } | |||
|         byte[] data = null; | |||
|         ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); | |||
|         byte[] buffer = new byte[1024]; | |||
|         int len = 0; | |||
|         while((len = inputStream.read(buffer, 0, 1024)) != -1){ | |||
|             swapStream.write(buffer, 0, len); | |||
|         } | |||
|         data = swapStream.toByteArray(); | |||
|         String base64 = "data:image/png;base64,"+Base64.encodeBase64String(data); | |||
|         return base64; | |||
|     } | |||
| 
 | |||
|     /** | |||
|      * 获取临时素材 | |||
|      */ | |||
|     private static InputStream getMediaStream(String accessTokenUrl, String mediaUrl, String mediaId, String appid, String secret)throws IOException { | |||
|         String access_token = getAccessToken(accessTokenUrl, appid, secret); | |||
|         String params = "access_token=" + access_token + "&media_id=" + mediaId; | |||
|         InputStream is = null; | |||
|         try { | |||
|             String urlNameString = mediaUrl + "?" + params; | |||
|             URL urlGet = new URL(urlNameString); | |||
|             HttpURLConnection http = (HttpURLConnection) urlGet.openConnection(); | |||
|             http.setRequestMethod("GET"); // 必须是get方式请求
 | |||
|             http.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); | |||
|             http.setDoOutput(true); | |||
|             http.setDoInput(true); | |||
|             http.connect(); | |||
|             if (!http.getContentType().contains("image")) { | |||
|                 return null; | |||
|             } | |||
|             // 获取文件转化为byte流
 | |||
|             is = http.getInputStream(); | |||
|         } catch (Exception e) { | |||
|             e.printStackTrace(); | |||
|         } | |||
|         return is; | |||
|     } | |||
| 
 | |||
|     /** | |||
|      * 获取微信Jsapi的accessToken | |||
|      * 这里获取的获取微信Jsapi的accessToken跟小程序以及其他的不一样 | |||
|      */ | |||
|     public static String getAccessToken(String accessTokenUrl, String appId, String secret) { | |||
|         accessTokenUrl = accessTokenUrl.replace("APPID", appId).replace("APPSECRET", secret); | |||
|         JSONObject jsonObj = doHttpGet(accessTokenUrl); | |||
|         String accessToken = jsonObj.getString("access_token"); | |||
|         return accessToken; | |||
|     } | |||
| 
 | |||
|     /** | |||
|      * 发送GET请求 | |||
|      * @Params: [requestUrl, params] | |||
|      * @Return: java.lang.String | |||
|      * @Author: liuchuang | |||
|      * @Date: 2019/8/5 14:25 | |||
|      */ | |||
|     public static JSONObject doHttpGet(String requestUrl) { | |||
|         JSONObject jsonObj = null; | |||
|         // buffer用于接受返回的字符
 | |||
|         StringBuffer buffer = new StringBuffer(); | |||
|         try { | |||
|             // 建立URL,把请求地址给补全,其中urlEncode()方法用于把params里的参数给取出来
 | |||
|             URL url = new URL(requestUrl); | |||
|             // 打开http连接
 | |||
|             HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection(); | |||
|             httpUrlConn.setDoInput(true); | |||
|             httpUrlConn.setRequestMethod("GET"); | |||
|             httpUrlConn.connect(); | |||
|             // 获得输入
 | |||
|             InputStream inputStream = httpUrlConn.getInputStream(); | |||
|             InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8"); | |||
|             BufferedReader bufferedReader = new BufferedReader(inputStreamReader); | |||
|             // 将bufferReader的值给放到buffer里
 | |||
|             String str = null; | |||
|             while ((str = bufferedReader.readLine()) != null) { | |||
|                 buffer.append(str); | |||
|             } | |||
|             // 关闭bufferReader和输入流
 | |||
|             bufferedReader.close(); | |||
|             inputStreamReader.close(); | |||
|             inputStream.close(); | |||
|             // 断开连接
 | |||
|             httpUrlConn.disconnect(); | |||
|         } catch (Exception e) { | |||
|             e.printStackTrace(); | |||
|         } | |||
|         jsonObj = JSONObject.parseObject(buffer.toString()); | |||
|         return jsonObj; | |||
|     } | |||
| 
 | |||
| } | |||
| @ -0,0 +1,38 @@ | |||
| package com.elink.esua.epdc.dto.form; | |||
| 
 | |||
| import lombok.Data; | |||
| 
 | |||
| import javax.validation.constraints.NotBlank; | |||
| import java.io.Serializable; | |||
| 
 | |||
| /** | |||
|  * 图片上传Form DTO | |||
|  * @Author LC | |||
|  * @Date 2019/9/11 16:27 | |||
|  */ | |||
| @Data | |||
| public class UploadImageFormDTO implements Serializable { | |||
|     private static final long serialVersionUID = -4670602967272919640L; | |||
| 
 | |||
|     /** | |||
|      * 文件标识 | |||
|      */ | |||
|     @NotBlank(message = "文件标识不能为空") | |||
|     private String mediaId; | |||
|     /** | |||
|      * 获取accessToken地址 | |||
|      */ | |||
|     private String accessTokenUrl; | |||
|     /** | |||
|      * 下载文件地址 | |||
|      */ | |||
|     private String mediaUrl; | |||
|     /** | |||
|      * appid | |||
|      */ | |||
|     private String appid; | |||
|     /** | |||
|      * secret | |||
|      */ | |||
|     private String secret; | |||
| } | |||
| @ -0,0 +1,16 @@ | |||
| package com.elink.esua.epdc.dto; | |||
| 
 | |||
| import lombok.Data; | |||
| 
 | |||
| import java.io.Serializable; | |||
| 
 | |||
| /** | |||
|  * @Author LC | |||
|  * @Date 2019/9/11 19:22 | |||
|  */ | |||
| @Data | |||
| public class UploadFormDTO implements Serializable { | |||
|     private static final long serialVersionUID = -3595962954673866413L; | |||
| 
 | |||
|     private String base64String; | |||
| } | |||
					Loading…
					
					
				
		Reference in new issue