1. 程式人生 > >對於根據圖片url的轉base64,下載圖片的工具類

對於根據圖片url的轉base64,下載圖片的工具類

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import sun.misc.BASE64Encoder;

public class ImageUtil {
	/**
	 *二維碼路徑 
	 */
	private static String QR_CODE = "/home/nginxFile/qr_code/";
	// private static String QR_CODE = "d:/upload/";

	/**
	 * PDF中頭像路徑
	 */
	public static String PDFIMGPATH = "/home/nginxFile/pdfImg/";
	
	/**
	 * 儲存檔案至本地
	 * 
	 * @param inputStream
	 * @param fileName
	 */
	public static void saveImgPic(InputStream inputStream, String fileName,String ImgPath) {

		OutputStream os = null;
		try {
			String path = ImgPath;
			// 2、儲存到臨時檔案
			// 1K的資料緩衝
			byte[] bs = new byte[1024];
			// 讀取到的資料長度
			int len;
			// 輸出的檔案流儲存到本地檔案

			File tempFile = new File(path);
			if (!tempFile.exists()) {
				tempFile.mkdirs();
			}
			os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
			// 開始讀取
			while ((len = inputStream.read(bs)) != -1) {
				os.write(bs, 0, len);
			}

		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 完畢,關閉所有連結
			try {
				os.close();
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 根據圖片連結轉為base64資料
	 * 
	 * @param imageUrl
	 * @return
	 * @throws Exception
	 */
	public static String getBase64ByUrl(String imageUrl) throws Exception {
		// new一個URL物件
		URL url = new URL(imageUrl);
		// 開啟連結
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		// 設定請求方式為"GET"
		conn.setRequestMethod("GET");
		// 超時響應時間為5秒
		conn.setConnectTimeout(5 * 1000);
		// 通過輸入流獲取圖片資料
		InputStream inStream = conn.getInputStream();
		// 得到圖片的二進位制資料,以二進位制封裝得到資料,具有通用性
		byte[] data = readInputStream(inStream);
		BASE64Encoder encode = new BASE64Encoder();
		String s = encode.encode(data);
		return s;
	}

	private static byte[] readInputStream(InputStream inStream) throws Exception {
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		// 建立一個Buffer字串
		byte[] buffer = new byte[1024];
		// 每次讀取的字串長度,如果為-1,代表全部讀取完畢
		int len = 0;
		// 使用一個輸入流從buffer裡把資料讀取出來
		while ((len = inStream.read(buffer)) != -1) {
			// 用輸出流往buffer裡寫入資料,中間引數代表從哪個位置開始讀,len代表讀取的長度
			outStream.write(buffer, 0, len);
		}
		// 關閉輸入流
		inStream.close();
		// 把outStream裡的資料寫入記憶體
		return outStream.toByteArray();
	}

	/**
	 * 根據url下載圖片到本地
	 * 
	 * @param urlList
	 * @param path
	 */
	private static void downloadPicture(String urlList, String id) {
		URL url = null;
		try {
			url = new URL(urlList);
			DataInputStream dataInputStream = new DataInputStream(url.openStream());

			FileOutputStream fileOutputStream = new FileOutputStream(new File(QR_CODE + id + ".png"));
			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();
		}
	}

	public static void main(String[] args) {
		String url = "https://mwkblog.oss-cn-qingdao.aliyuncs.com/images/headPortrait/ffe83772cdca4dfc88bb352c4e98734a.jpg";

		downloadPicture(url, "33333");
	}
}