Java实现向数据库中存放和读取图片



最近公司做个项目,要求把图片存放到数据库中然后再读取出来,在做的过程中老是出现这样或那样的问题,现把代码贴出来,仅供大家参考。

package com.swh.conImg;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ConImgDemo {

    /**
     * @param args
     */
    Connection conn=null;
    public ConImgDemo () {
        try {
            String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=swhImg";
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
            conn= DriverManager.getConnection(url,"sa","sa");     
        }
        catch(SQLException e) {
            e.printStackTrace();
        }
        catch(ClassNotFoundException ce) {
            ce.printStackTrace();
        }
    }
    public void Insert() {
        try {
            String sql="insert into picture values(?,?)";
            PreparedStatement ps=conn.prepareStatement(sql);
            File f =new File("E:/Pictures/1.jpg");
            
           
            FileInputStream input= new FileInputStream(f);
            ps.setString(1,"test");
            ps.setBinaryStream(2, input,(int)f.length());
            ps.executeUpdate();
            ps.close();
            input.close();
        }
        catch(SQLException e) {
            e.printStackTrace();
        }
        catch(IOException ie) {
            ie.printStackTrace();
        }
    }
   
    public void Read() {
        try {
            String sql="select picture from picture where name=?";
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1, "test");
            ResultSet rs=ps.executeQuery();
            byte [] b=new byte[10240*8];
           
            while(rs.next()) {
                InputStream in=rs.getBinaryStream("picture");
                in.read(b);
                File f=new File("D:Pictures/3.jpg");
                FileOutputStream out=new FileOutputStream(f);
                out.write(b, 0, b.length);
                out.close();
            }
           
        }
        catch(SQLException e) {
            e.printStackTrace();
        }
        catch(IOException ie) {
            ie.printStackTrace();
        }
    }
   
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ConImgDemo conImgDemo=new ConImgDemo ();
        conImgDemo.Insert();
        conImgDemo.Read();
    }

}
 其实,主要是用文件输入,输出流来进行操作。 


http://blog.csdn.net/sanfye/article/details/11788255

Java,您可以使用JDBC和JTS Topology Suite库来实现读取数据库两个GIS图层的数据进行交集取反的操作。下面是一种可能的实现方式: ```java import java.sql.*; import org.locationtech.jts.geom.*; import org.locationtech.jts.io.*; public class GISIntersection { public static void main(String[] args) { // 连接数据库 String url = "jdbc:postgresql://localhost:5432/mydb"; String user = "user"; String password = "password"; try (Connection conn = DriverManager.getConnection(url, user, password)) { // 读取两个GIS图层的数据 String sql1 = "SELECT id, geom FROM layer1"; String sql2 = "SELECT id, geom FROM layer2"; Statement stmt = conn.createStatement(); ResultSet rs1 = stmt.executeQuery(sql1); ResultSet rs2 = stmt.executeQuery(sql2); // 创建GeometryFactory和WKTReader对象 GeometryFactory factory = new GeometryFactory(); WKTReader reader = new WKTReader(factory); // 创建两个空的GeometryCollection对象 GeometryCollection gc1 = new GeometryCollection(null, factory); GeometryCollection gc2 = new GeometryCollection(null, factory); // 读取rs1的数据,将Geometry对象添加到gc1 while (rs1.next()) { String wkt = rs1.getString("geom"); Geometry geom = reader.read(wkt); gc1 = (GeometryCollection) gc1.union(geom); } // 读取rs2的数据,将Geometry对象添加到gc2 while (rs2.next()) { String wkt = rs2.getString("geom"); Geometry geom = reader.read(wkt); gc2 = (GeometryCollection) gc2.union(geom); } // 计算两个图层的交集 Geometry intersection = gc1.intersection(gc2); // 取反操作,即在gc1去掉intersection的几何体 gc1 = (GeometryCollection) gc1.difference(intersection); // 将结果保存到数据库 String sql = "UPDATE layer1 SET geom = ? WHERE id = ?"; PreparedStatement pstmt = conn.prepareStatement(sql); for (int i = 0; i < gc1.getNumGeometries(); i++) { Geometry geom = gc1.getGeometryN(i); String wkt = geom.toText(); int id = i + 1; pstmt.setString(1, wkt); pstmt.setInt(2, id); pstmt.executeUpdate(); } pstmt.close(); // 关闭rs1和rs2 rs1.close(); rs2.close(); } catch (SQLException e) { e.printStackTrace(); } } } ``` 上面的代码首先连接数据库,然后使用JDBC读取两个GIS图层的数据。接着,创建GeometryFactory和WKTReader对象,以便将WKT格式的几何体转换成Geometry对象。然后,创建两个空的GeometryCollection对象,并读取rs1和rs2的数据,将Geometry对象添加到相应的GeometryCollection对象。接着,计算两个图层的交集,然后使用difference函数从gc1去掉intersection的几何体。最后,使用JDBC将结果保存到数据库。 需要注意的是,上面的代码只计算了两个图层的交集取反,如果您需要计算更复杂的几何体操作,可以使用JTS Topology Suite库提供的其他函数,例如union、difference、symDifference等。另外,数据库的几何体数据通常是以WKT格式存储的,因此需要使用WKTReader和WKTWriter对象进行转换。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值