工具类
@Data
public class ImgUtils {
private final static int W_PIXEL = 1024;
final static Long M = 1 * 1000L;
public static InputStream netImageToStream(String netImagePath) {
ByteArrayOutputStream byteArrayOutputStream = null;
try {
URL url = new URL(netImagePath);
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
InputStream inputStream = conn.getInputStream();
byteArrayOutputStream = toByteArrayOutputStream(inputStream);
Integer size = byteArrayOutputStream.size() / 1024;
if (size < M) {
return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
}
return compressImage(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != byteArrayOutputStream) {
try {
byteArrayOutputStream.flush();
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
public static InputStream netImageToStream(InputStream inputStream) {
ByteArrayOutputStream byteArrayOutputStream = null;
try {
byteArrayOutputStream = toByteArrayOutputStream(inputStream);
Integer size = byteArrayOutputStream.size() / 1024;
if (size < M) {
return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
}
return compressImage(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != byteArrayOutputStream) {
try {
byteArrayOutputStream.flush();
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
private static InputStream compressImage(InputStream inputStream) throws IOException {
if (null == inputStream) {
return null;
}
ByteArrayOutputStream outputStream = null;
try {
BufferedImage image = ImageIO.read(inputStream);
int width = image.getWidth();
int height = image.getHeight();
int widthD = (width / W_PIXEL);
width = width / widthD;
height = height / widthD;
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Image compressedImage = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
bufferedImage.getGraphics().drawImage(compressedImage, 0, 0, null);
outputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "jpg", outputStream);
long outputSize = outputStream.size();
return new ByteArrayInputStream(outputStream.toByteArray());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != outputStream) {
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != inputStream) {
try {
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return new ByteArrayInputStream(outputStream.toByteArray());
}
public static ByteArrayOutputStream toByteArrayOutputStream(InputStream inputStream) {
ByteArrayOutputStream outputStream = null;
try {
outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
} catch (IOException i) {
i.printStackTrace();
} finally {
if (null != outputStream) {
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return outputStream;
}
}
调用
@GetMapping("/img/compress")
@ApiOperation(httpMethod = "GET", value = "测试图片压缩", notes = "测试图片压缩", produces = MediaType.APPLICATION_JSON_VALUE)
public void imgCompress(@RequestParam("url") String url, HttpServletResponse response) throws IOException {
InputStream inputStream = ImgUtils.netImageToStream(url);
response.getOutputStream().write(ImgUtils.toByteArrayOutputStream(inputStream).toByteArray());
}