package com.sinata.system.utils; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; public class ImgUtils { /** * 从URL中获取图片输入流 * 并创建本地文件 * * @param imageUrl * @param savePath * @return * @throws Exception */ public static File getImageFileFromUrl(String imageUrl, String savePath) throws Exception { // 从URL中获取图片输入流 URL url = new URL(imageUrl); InputStream in = url.openStream(); // 构建保存路径 String[] split = imageUrl.split("/"); String fileName = split[split.length - 1]; File file = new File(savePath + fileName); if (!file.exists()) { file.createNewFile(); } // 写入文件 FileOutputStream out = new FileOutputStream(file); byte[] buffer = new byte[4096]; int n = 0; while ((n = in.read(buffer)) != -1) { out.write(buffer, 0, n); } out.close(); in.close(); return file; } /** * 根据URL地址获取文件 * 得到file对象 * * @param path URL网络地址 * @return File */ public static File getFileByHttpURL(String path) { String newUrl = path.split("[?]")[0]; String[] suffix = newUrl.split("/"); //得到最后一个分隔符后的名字 String fileName = suffix[suffix.length - 1]; File file = null; InputStream inputStream = null; OutputStream outputStream = null; try { file = File.createTempFile("report", fileName);//创建临时文件 URL urlFile = new URL(newUrl); inputStream = urlFile.openStream(); outputStream = new FileOutputStream(file); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) { outputStream.write(buffer, 0, bytesRead); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != outputStream) { outputStream.close(); } if (null != inputStream) { inputStream.close(); } } catch (Exception e) { e.printStackTrace(); } } return file; } //顺时针旋转90度(通过交换图像的整数像素RGB 值) public static BufferedImage rotateClockwise90(BufferedImage bi) { int width = bi.getWidth(); int height = bi.getHeight(); BufferedImage bufferedImage = new BufferedImage(height, width, bi.getType()); for (int i = 0; i < width; i++) for (int j = 0; j < height; j++) bufferedImage.setRGB(height - 1 - j, width - 1 - i, bi.getRGB(i, j)); return bufferedImage; } //逆时针旋转90度(通过交换图像的整数像素RGB 值) public static BufferedImage rotateCounterclockwise90(BufferedImage bi) { int width = bi.getWidth(); int height = bi.getHeight(); BufferedImage bufferedImage = new BufferedImage(height, width, bi.getType()); for (int i = 0; i < width; i++) for (int j = 0; j < height; j++) bufferedImage.setRGB(j, i, bi.getRGB(i, j)); return bufferedImage; } //旋转180度(通过交换图像的整数像素RGB 值) public static BufferedImage rotate180(BufferedImage bi) { int width = bi.getWidth(); int height = bi.getHeight(); BufferedImage bufferedImage = new BufferedImage(width, height, bi.getType()); for (int i = 0; i < width; i++) for (int j = 0; j < height; j++) bufferedImage.setRGB(width - i - 1, height - j - 1, bi.getRGB(i, j)); return bufferedImage; } //水平翻转 public static BufferedImage rotateHorizon(BufferedImage bi) { int width = bi.getWidth(); int height = bi.getHeight(); BufferedImage bufferedImage = new BufferedImage(width, height, bi.getType()); for (int i = 0; i < width; i++) for (int j = 0; j < height; j++) bufferedImage.setRGB(width - i - 1, j, bi.getRGB(i, j)); return bufferedImage; } //垂直翻转 public static BufferedImage rotateVertical(BufferedImage bi) { int width = bi.getWidth(); int height = bi.getHeight(); BufferedImage bufferedImage = new BufferedImage(width, height, bi.getType()); for (int i = 0; i < width; i++) for (int j = 0; j < height; j++) bufferedImage.setRGB(i, height - 1 - j, bi.getRGB(i, j)); return bufferedImage; } public static void main(String[] args) throws Exception { //File file = ImgUtils.getImageFileFromUrl("【你的图片网络url】", "【你要保存的本地文件位置】"); File file = ImgUtils.getFileByHttpURL("【你的图片网络url】"); System.out.println("file.length() = " + file.length()); } }