从网络获取图片保存临时文件,添加到数据库中

package com.hous.test;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.imageio.ImageIO;

public class StoreImage {

	/**
	 * 本地图片存储
	 * @throws SQLException
	 * @throws IOException
	 */
	public void insertBlobLocal() throws Exception {  
        Connection conn = null;  
        PreparedStatement stmt = null;  
        conn = ConnDb.getConnection(  
                "jdbc:mysql://localhost:3306/数据库", "用户名",  
                "密码");  
        String sql = "insert into image(img, title) values(?,?)";  
        stmt = conn.prepareStatement(sql);  
        File file = new File("D:\\hsTemp\\1.png");  
        FileInputStream fis = new FileInputStream(file);  
        stmt.setBinaryStream(1, fis, (int) file.length());  
        stmt.setString(2, "myimg");  
        stmt.execute();  
        fis.close();  
    }
	
	/**
	 * 远程图片存储
	 * @throws SQLException
	 * @throws IOException
	 */
	public void insertBlobRemote() throws Exception {  
		String url = "http://ec4.images-amazon.com/images/I/51XMC7MVcFL._SX258_BO1,204,203,200_.jpg";
		String ext = url.substring(url.lastIndexOf("."));
		Connection conn = null;  
		PreparedStatement stmt = null;  
		conn = ConnDb.getConnection(  
				"jdbc:mysql://localhost:3306/数据库", "用户名",  
				"密码");  
		String sql = "insert into image(img, title) values(?,?)";  
		stmt = conn.prepareStatement(sql);  
		//File file = new File("e:\\img\\1.jpg");
		//获取图片
		InputStream inputStream = getImgFromUrl(url);
		//保存成File
		File file = inputstreamtofile(inputStream, ext);
		FileInputStream fis = new FileInputStream(file);  
		stmt.setBinaryStream(1, fis, (int) file.length());  
		stmt.setString(2, "myimg");  
		stmt.execute();  
		fis.close();
		//删除临时文件
		file.deleteOnExit();
	}
	
	public InputStream getImgFromUrl(String url) throws IOException{
		//Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("10.167.129.102", 8080));
		URL myUrl = new URL(url);
		HttpURLConnection con = (HttpURLConnection) myUrl.openConnection();
		con.setConnectTimeout(5*1000);
		InputStream is = con.getInputStream();
		return is;
	}
	
	/*public InputStream getImgFromUrl() throws IOException{
		System.getProperties().setProperty("proxySet", "true");
		//设置你的主机 
		System.getProperties().setProperty("http.proxyHost", "127.0.0.1");
		//设置你的端口
		System.getProperties().setProperty("http.proxyPort", "8080");
		String urlStr2="http://ec4.images-amazon.com/images/I/51XMC7MVcFL._SX258_BO1,204,203,200_.jpg";
		URL url2 = new URL(urlStr2); 
		HttpURLConnection connection = (HttpURLConnection) url2.openConnection();
		connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
		return connection.getInputStream(); 
	}*/
	
	
	public File inputstreamtofile(InputStream ins, String ext) throws IOException{
		File f = new File("d:\\Temp");
		File file = File.createTempFile("temp", ext, f);  
		OutputStream os = new FileOutputStream(file);
		int bytesRead = 0;
		byte[] buffer = new byte[8192];
		while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
		os.write(buffer, 0, bytesRead);
		}
		os.close();
		ins.close();
		return file;
	}
    
    public static void main(String[] args) {
    	StoreImage storeImage = new StoreImage();
    	try {
//			storeImage.insertBlobLocal();
			storeImage.insertBlobRemote();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 数据库连接

package com.hous.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnDb {
	
	private static String driver = "com.mysql.jdbc.Driver";

	public static Connection getConnection(String url, String user, String password) throws Exception {
		Connection connection = null;
		Class.forName(driver);
		connection = DriverManager.getConnection(url, user, password);
		return connection;
	}
	
}

猜你喜欢

转载自shuizhongyue.iteye.com/blog/2265131