16 changed files with 560 additions and 129 deletions
			
			
		| @ -0,0 +1,154 @@ | |||
| package com.elink.esua.epdc.commons.tools.utils; | |||
| 
 | |||
| import net.coobird.thumbnailator.Thumbnails; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| 
 | |||
| import javax.imageio.ImageIO; | |||
| import java.awt.image.BufferedImage; | |||
| import java.io.*; | |||
| import java.math.BigDecimal; | |||
| import java.net.HttpURLConnection; | |||
| import java.net.MalformedURLException; | |||
| import java.net.URL; | |||
| 
 | |||
| /** | |||
|  * @Description 压缩图片工具类 | |||
|  * @Author yinzuomei | |||
|  * @Date 2020/2/27 15:48 | |||
|  */ | |||
| public class CompressImgUtils { | |||
| 
 | |||
| 	/** | |||
| 	 * @param imgUrl | |||
| 	 * @return long 字节大小 | |||
| 	 * @Author yinzuomei | |||
| 	 * @Description 获取网络文件大小 | |||
| 	 * @Date 2020/2/27 15:52 | |||
| 	 **/ | |||
| 	public static long getFileLength(String imgUrl) { | |||
| 		if (StringUtils.isBlank(imgUrl)) { | |||
| 			return 0L; | |||
| 		} | |||
| 		HttpURLConnection conn = null; | |||
| 		try { | |||
| 			URL url = new URL(imgUrl); | |||
| 			conn = (HttpURLConnection) url.openConnection(); | |||
| 			conn.setRequestMethod("HEAD"); | |||
| 			conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows 7; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36 YNoteCef/5.8.0.1 (Windows)"); | |||
| 			return (long) conn.getContentLength(); | |||
| 		} catch (IOException e) { | |||
| 			e.printStackTrace(); | |||
| 			return 0L; | |||
| 		} finally { | |||
| 			conn.disconnect(); | |||
| 		} | |||
| 	} | |||
| 
 | |||
| 	/** | |||
| 	 * @param imgUrl | |||
| 	 * @return byte[] | |||
| 	 * @Author yinzuomei | |||
| 	 * @Description 根据图片地址压缩图片 | |||
| 	 * @Date 2020/2/27 16:44 | |||
| 	 **/ | |||
| 	public static byte[] compressImg(String imgUrl) { | |||
| 		byte[] afterJyBytes = new byte[0]; | |||
| 		URL url = null; | |||
| 		try { | |||
| 			url = new URL(imgUrl); | |||
| 			System.out.println(url.getFile()); | |||
| 			DataInputStream dataInputStream = new DataInputStream(url.openStream()); | |||
| 			byte[] bytes = toByteArray(dataInputStream); | |||
| 			afterJyBytes = commpressPicCycle(bytes, 1024, 0.5); | |||
| 			// 输出到文件
 | |||
| 			/*File desFile = new File("D:\\testcompressImg\\" + System.currentTimeMillis() + ".jpg"); | |||
| 			FileOutputStream fos = new FileOutputStream(desFile); | |||
| 			fos.write(afterJyBytes); | |||
| 			fos.close();*/ | |||
| 		} catch (MalformedURLException e) { | |||
| 			e.printStackTrace(); | |||
| 		} catch (IOException e) { | |||
| 			e.printStackTrace(); | |||
| 		} | |||
| 		return afterJyBytes; | |||
| 	} | |||
| 
 | |||
| 	/** | |||
| 	 * inputstream输入流转换成byte[]字节数组 | |||
| 	 * | |||
| 	 * @param input | |||
| 	 * @return byte[] | |||
| 	 * @author yinzuomei | |||
| 	 * @date 2020/2/27 16:09 | |||
| 	 */ | |||
| 	public static byte[] toByteArray(InputStream input) throws IOException { | |||
| 		ByteArrayOutputStream output = new ByteArrayOutputStream(); | |||
| 		byte[] buffer = new byte[1024 * 4]; | |||
| 		int n = 0; | |||
| 		while (-1 != (n = input.read(buffer))) { | |||
| 			output.write(buffer, 0, n); | |||
| 		} | |||
| 		return output.toByteArray(); | |||
| 	} | |||
| 
 | |||
| 	/** | |||
| 	 * 压缩图片返回字节数组 | |||
| 	 * | |||
| 	 * @param bytes       原图片字节数组 | |||
| 	 * @param desFileSize 指定图片大小,单位 kb | |||
| 	 * @param accuracy    精度,递归压缩的比率,建议小于0.9 | |||
| 	 * @return byte[] | |||
| 	 */ | |||
| 	public static byte[] commpressPicCycle(byte[] bytes, long desFileSize, double accuracy) throws IOException { | |||
| 		long fileSize = bytes.length; | |||
| 		System.out.println("=====fileSize======== " + fileSize); | |||
| 		// 判断图片大小是否小于指定图片大小
 | |||
| 		if (fileSize <= desFileSize * 1024) { | |||
| 			return bytes; | |||
| 		} | |||
| 		//计算宽高
 | |||
| 		BufferedImage bim = ImageIO.read(new ByteArrayInputStream(bytes)); | |||
| 		int imgWidth = bim.getWidth(); | |||
| 		int imgHeight = bim.getHeight(); | |||
| 		System.out.println("宽度imgWidth=" + imgWidth + ";高度imgHeight=" + imgHeight); | |||
| 		int desWidth = new BigDecimal(imgWidth).multiply(new BigDecimal(accuracy)).intValue(); | |||
| 		int desHeight = new BigDecimal(imgHeight).multiply(new BigDecimal(accuracy)).intValue(); | |||
| 		System.out.println("转换后宽度desWidth=" + desWidth + ";高度desHeight=" + desHeight); | |||
| 		ByteArrayOutputStream baos = new ByteArrayOutputStream(); //字节输出流(写入到内存)
 | |||
| 		Thumbnails.of(new ByteArrayInputStream(bytes)).size(desWidth, desHeight).outputQuality(accuracy).toOutputStream(baos); | |||
| 		//如果不满足要求,递归直至满足要求
 | |||
| 		return commpressPicCycle(baos.toByteArray(), desFileSize, accuracy); | |||
| 	} | |||
| 
 | |||
| 	/** | |||
| 	 * @param imgUrl eg:https://elink-esua-epdc.oss-cn-qingdao.aliyuncs.com/esua-epdc/test/20200226/95dd03ace7d04627a297abae7339de97.jpg
 | |||
| 	 * @param path   保存路径eg:d:/pic.jpg | |||
| 	 * @return void | |||
| 	 * @Author yinzuomei | |||
| 	 * @Description 根据图片url下载图片到本地地址 | |||
| 	 * @Date 2020/2/27 15:50 | |||
| 	 **/ | |||
| 	public static void downloadPicture(String imgUrl, String path) { | |||
| 		try { | |||
| 			URL url = new URL(imgUrl); | |||
| 			DataInputStream dataInputStream = new DataInputStream(url.openStream()); | |||
| 			FileOutputStream fileOutputStream = new FileOutputStream(new File(path)); | |||
| 			ByteArrayOutputStream output = new ByteArrayOutputStream(); | |||
| 
 | |||
| 			byte[] buffer = new byte[1024]; | |||
| 			int length; | |||
| 
 | |||
| 			while ((length = dataInputStream.read(buffer)) > 0) { | |||
| 				output.write(buffer, 0, length); | |||
| 			} | |||
| 			fileOutputStream.write(output.toByteArray()); | |||
| 			dataInputStream.close(); | |||
| 			fileOutputStream.close(); | |||
| 		} catch (MalformedURLException e) { | |||
| 			e.printStackTrace(); | |||
| 		} catch (IOException e) { | |||
| 			e.printStackTrace(); | |||
| 		} | |||
| 	} | |||
| 
 | |||
| } | |||
| @ -0,0 +1,30 @@ | |||
| package com.elink.esua.epdc.modules.feign; | |||
| 
 | |||
| import com.elink.esua.epdc.commons.tools.constant.ServiceConstant; | |||
| import com.elink.esua.epdc.commons.tools.utils.Result; | |||
| import com.elink.esua.epdc.modules.feign.fallback.OssFeignClientFallback; | |||
| import org.springframework.cloud.openfeign.FeignClient; | |||
| import org.springframework.web.bind.annotation.PostMapping; | |||
| 
 | |||
| import java.util.List; | |||
| 
 | |||
| /** | |||
|  * 文件对象模块 | |||
|  * | |||
|  * @Author yinzuomei | |||
|  * @Date 2020/02/27 18:24 | |||
|  */ | |||
| @FeignClient(name = ServiceConstant.EPDC_OSS_SERVER, fallback = OssFeignClientFallback.class) | |||
| public interface OssFeignClient { | |||
| 
 | |||
| 	/** | |||
| 	 * 压缩图片并上传返回新的url | |||
| 	 * | |||
| 	 * @Params: [base64] | |||
| 	 * @Return: com.elink.esua.epdc.commons.tools.utils.Result<java.lang.String> | |||
| 	 * @Author: yinzuomei | |||
| 	 * @Date: 2019/9/11 17:17 | |||
| 	 */ | |||
| 	@PostMapping(value = "oss/file/compressImg") | |||
| 	Result<List<String>> compressImg(List<String> imgUrlList); | |||
| } | |||
| @ -0,0 +1,22 @@ | |||
| package com.elink.esua.epdc.modules.feign.fallback; | |||
| 
 | |||
| import com.elink.esua.epdc.commons.tools.constant.ServiceConstant; | |||
| import com.elink.esua.epdc.commons.tools.utils.ModuleUtils; | |||
| import com.elink.esua.epdc.commons.tools.utils.Result; | |||
| import com.elink.esua.epdc.modules.feign.OssFeignClient; | |||
| import org.springframework.stereotype.Component; | |||
| 
 | |||
| import java.util.List; | |||
| 
 | |||
| /** | |||
|  * @author yinzuomei | |||
|  * @date 2020/02/27 18:24 | |||
|  */ | |||
| @Component | |||
| public class OssFeignClientFallback implements OssFeignClient { | |||
| 
 | |||
| 	@Override | |||
| 	public Result<List<String>> compressImg(List<String> imgUrlList) { | |||
| 		return ModuleUtils.feignConError(ServiceConstant.EPDC_OSS_SERVER, "compressImg", imgUrlList); | |||
| 	} | |||
| } | |||
					Loading…
					
					
				
		Reference in new issue