高鹏工具类

<pre name="code" class="java">package com.haier.adThird.util;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;

import javax.imageio.ImageIO;

public class GPUtil {
	/**
	 * 读取Stream到磁盘上
	 * 
	 * @param inputStream
	 * @param filePath
	 * @throws Exception
	 */
	public static void readStreamToDisk(InputStream inputStream, String filePath)
			throws Exception {
		byte[] bs = new byte[1024 * 2];
		int len;
		OutputStream os = new FileOutputStream(filePath);
		while ((len = inputStream.read(bs)) != -1) {
			os.write(bs, 0, len);
		}
		os.close();
	}

	/**
	 * 读取URL获取连接
	 * 
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static HttpURLConnection readURL(String url) throws Exception {
		URL picUrl = new URL(url);
		HttpURLConnection httpURLConnection = (HttpURLConnection) picUrl
				.openConnection();
		httpURLConnection.setConnectTimeout(3000);
		httpURLConnection.setDoInput(true);// 从服务器获取数据
		return httpURLConnection;
	}

	/**
	 * 读取URL获取连接(输入流)
	 * 
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static InputStream readURL_InputStream(String url) throws Exception {
		HttpURLConnection httpURLConnection = readURL(url);
		return httpURLConnection.getInputStream();
	}

	/**
	 * 读取本地图片信息
	 * 
	 * @param filePath
	 * @return
	 * @throws Exception
	 */
	public static List getDiskPicInfo(String filePath) throws Exception {
		File picture = new File(filePath);
		BufferedImage sourceImg = ImageIO.read(new FileInputStream(picture));

		List<String> list = new LinkedList<String>();
		// 图片大小
		list.add(String.format("%.1f", picture.length() / 1024.0));
		list.add(sourceImg.getWidth() + "");
		list.add(sourceImg.getHeight() + "");
		return list;
	}

	/**
	 * 判断是否是Baseline格式的图片,true:是Baseline格式
	 * 
	 * @param fis
	 * @return
	 * @throws IOException
	 */
	public static Boolean isBaselineInputJPEG(InputStream fis)
			throws IOException {
		ByteArrayInputStream bais = null;
		BufferedInputStream bf = new BufferedInputStream(fis);
		byte[] byteArr;
		try {
			byteArr = new byte[bf.available()];
			System.out.println("bf.available():" + bf.available());
			bf.read(byteArr);
		} finally {
			bf.close();
		}
		bais = new ByteArrayInputStream(byteArr);
		System.out.println("bais.available():" + bais.available());
		if (bais.available() < 2) {
			System.out.println("File length is too small");
			return false;
		}
		while (bais.available() > 0) {
			int iFirstByte = bais.read() & 0xff;
			if (0xff == iFirstByte) {
				int iSecondByte = bais.read() & 0xff;
				if (0xc0 == iSecondByte) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 将字节写到磁盘路径下
	 * 
	 * @param b
	 * @param targetPath
	 * @throws Exception
	 */
	public static void byteToPath(byte[] b, String targetPath) throws Exception {
		FileOutputStream fout = new FileOutputStream(targetPath);
		// 将字节写入文件
		fout.write(b);
		fout.close();
	}
	
	public static void main(String[] args){
		try {
			System.out.println(isBaselineInputJPEG(readURL_InputStream("http://210.51.17.76/file/IMGzadfad23fa123dsk123jfk123/img20140828051121770423.jpg")));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


 

你可能感兴趣的:(工具类)