Commit 052c94932c8aee8000fc273cac3a857a290f4d67
1 parent
ab530c7dfa
Exists in
master
and in
6 other branches
update
Showing 2 changed files with 69 additions and 52 deletions
platform-common/src/main/java/com/lyms/platform/common/utils/LymsEncodeUtil.java
View file @
052c949
| 1 | 1 | package com.lyms.platform.common.utils; |
| 2 | 2 | |
| 3 | +import java.io.ByteArrayOutputStream; | |
| 4 | +import java.io.IOException; | |
| 5 | +import java.io.InputStream; | |
| 3 | 6 | import java.math.BigInteger; |
| 7 | +import java.net.HttpURLConnection; | |
| 8 | +import java.net.URL; | |
| 4 | 9 | import java.security.MessageDigest; |
| 5 | 10 | import java.security.SecureRandom; |
| 6 | 11 | |
| ... | ... | @@ -222,6 +227,67 @@ |
| 222 | 227 | result[i] = (byte) (high * 16 + low); |
| 223 | 228 | } |
| 224 | 229 | return result; |
| 230 | + } | |
| 231 | + | |
| 232 | + /** | |
| 233 | + * 将图片转换成Base64编码字符串 | |
| 234 | + * | |
| 235 | + * @param imgUrl | |
| 236 | + * @return String | |
| 237 | + * @throws | |
| 238 | + * @method imgConvertBase64 | |
| 239 | + * @since v1.0 | |
| 240 | + */ | |
| 241 | + public static String imgConvertBase64(String imgUrl) throws Exception{ | |
| 242 | + URL url = null; | |
| 243 | + InputStream is = null; | |
| 244 | + ByteArrayOutputStream outStream = null; | |
| 245 | + HttpURLConnection httpUrl = null; | |
| 246 | + try{ | |
| 247 | + url = new URL(imgUrl); | |
| 248 | + httpUrl = (HttpURLConnection) url.openConnection(); | |
| 249 | + httpUrl.connect(); | |
| 250 | + httpUrl.getInputStream(); | |
| 251 | + is = httpUrl.getInputStream(); | |
| 252 | + | |
| 253 | + outStream = new ByteArrayOutputStream(); | |
| 254 | + //创建一个Buffer字符串 | |
| 255 | + byte[] buffer = new byte[1024]; | |
| 256 | + //每次读取的字符串长度,如果为-1,代表全部读取完毕 | |
| 257 | + int len = 0; | |
| 258 | + //使用一个输入流从buffer里把数据读取出来 | |
| 259 | + while( (len=is.read(buffer)) != -1 ){ | |
| 260 | + //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度 | |
| 261 | + outStream.write(buffer, 0, len); | |
| 262 | + } | |
| 263 | + // 对字节数组Base64编码 | |
| 264 | + return new BASE64Encoder().encode(outStream.toByteArray()); | |
| 265 | + }catch (Exception e) { | |
| 266 | + e.printStackTrace(); | |
| 267 | + } | |
| 268 | + finally{ | |
| 269 | + if(is != null) | |
| 270 | + { | |
| 271 | + try { | |
| 272 | + is.close(); | |
| 273 | + } catch (IOException e) { | |
| 274 | + e.printStackTrace(); | |
| 275 | + } | |
| 276 | + } | |
| 277 | + if(outStream != null) | |
| 278 | + { | |
| 279 | + try { | |
| 280 | + outStream.close(); | |
| 281 | + } catch (IOException e) { | |
| 282 | + e.printStackTrace(); | |
| 283 | + } | |
| 284 | + } | |
| 285 | + if(httpUrl != null) | |
| 286 | + { | |
| 287 | + httpUrl.disconnect(); | |
| 288 | + } | |
| 289 | + } | |
| 290 | + return imgUrl; | |
| 225 | 291 | } |
| 226 | 292 | |
| 227 | 293 | } |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/controller/TestController.java
View file @
052c949
| ... | ... | @@ -5147,62 +5147,13 @@ |
| 5147 | 5147 | } |
| 5148 | 5148 | |
| 5149 | 5149 | /** |
| 5150 | - * 将本地图片转换成Base64编码字符串 | |
| 5150 | + * 将图片转换成Base64编码字符串 | |
| 5151 | 5151 | * @return imgUrl |
| 5152 | 5152 | */ |
| 5153 | 5153 | @RequestMapping(method = RequestMethod.GET, value = "/convertBase64") |
| 5154 | 5154 | @ResponseBody |
| 5155 | - public String convertBase64(String imgUrl) { | |
| 5156 | - URL url = null; | |
| 5157 | - InputStream is = null; | |
| 5158 | - ByteArrayOutputStream outStream = null; | |
| 5159 | - HttpURLConnection httpUrl = null; | |
| 5160 | - try{ | |
| 5161 | - url = new URL(imgUrl); | |
| 5162 | - httpUrl = (HttpURLConnection) url.openConnection(); | |
| 5163 | - httpUrl.connect(); | |
| 5164 | - httpUrl.getInputStream(); | |
| 5165 | - is = httpUrl.getInputStream(); | |
| 5166 | - | |
| 5167 | - outStream = new ByteArrayOutputStream(); | |
| 5168 | - //创建一个Buffer字符串 | |
| 5169 | - byte[] buffer = new byte[1024]; | |
| 5170 | - //每次读取的字符串长度,如果为-1,代表全部读取完毕 | |
| 5171 | - int len = 0; | |
| 5172 | - //使用一个输入流从buffer里把数据读取出来 | |
| 5173 | - while( (len=is.read(buffer)) != -1 ){ | |
| 5174 | - //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度 | |
| 5175 | - outStream.write(buffer, 0, len); | |
| 5176 | - } | |
| 5177 | - // 对字节数组Base64编码 | |
| 5178 | - return new BASE64Encoder().encode(outStream.toByteArray()); | |
| 5179 | - }catch (Exception e) { | |
| 5180 | - e.printStackTrace(); | |
| 5181 | - } | |
| 5182 | - finally{ | |
| 5183 | - if(is != null) | |
| 5184 | - { | |
| 5185 | - try { | |
| 5186 | - is.close(); | |
| 5187 | - } catch (IOException e) { | |
| 5188 | - e.printStackTrace(); | |
| 5189 | - } | |
| 5190 | - } | |
| 5191 | - if(outStream != null) | |
| 5192 | - { | |
| 5193 | - try { | |
| 5194 | - outStream.close(); | |
| 5195 | - } catch (IOException e) { | |
| 5196 | - e.printStackTrace(); | |
| 5197 | - } | |
| 5198 | - } | |
| 5199 | - if(httpUrl != null) | |
| 5200 | - { | |
| 5201 | - httpUrl.disconnect(); | |
| 5202 | - } | |
| 5203 | - } | |
| 5204 | - return imgUrl; | |
| 5205 | - | |
| 5155 | + public String convertBase64(String imgUrl) throws Exception { | |
| 5156 | + return LymsEncodeUtil.imgConvertBase64(imgUrl); | |
| 5206 | 5157 | } |
| 5207 | 5158 | } |