JAVA处理图片常用公共类

来源:互联网 发布:奥运会男篮vs美国数据 编辑:程序博客网 时间:2024/04/30 04:50
//检测图片宽高 public static Map getPicWH(String imagePath) {    Map mp = new HashMap();   File _file = new File(imagePath);   Image src;   try {    src = ImageIO.read(_file);    //   图片的宽度     int width = src.getWidth(null); //读取文件的长宽    //   图片的高度     int height = src.getHeight(null);        mp.put("width", width);    mp.put("height", height);   } catch (IOException e) {    e.printStackTrace();   }   return mp; } //检测图片大小 public Long image2byte(String path) {  byte[] data = null;  FileImageInputStream input = null;  try {   input = new FileImageInputStream(new File(path));   ByteArrayOutputStream output = new ByteArrayOutputStream();   byte[] buf = new byte[1024];   int numBytesRead = 0;   while ((numBytesRead = input.read(buf)) != -1) {    output.write(buf, 0, numBytesRead);   }   data = output.toByteArray();   output.close();   input.close();  } catch (FileNotFoundException ex1) {   ex1.printStackTrace();  } catch (IOException ex1) {   ex1.printStackTrace();  }  return Long.valueOf(data.length); }//获取BLOB文件生成图片 public static boolean getBlobFile(InputStream in,String sFullName) {    File file = new File(sFullName);  RandomAccessFile rFile = null;  try {   rFile = new RandomAccessFile(file, "rw");   rFile.seek(0);   byte bytes[] = new byte[1024];   int ch = 0;  while ((ch = in.read(bytes)) != -1)   rFile.write(bytes, 0, ch);   rFile.close();   return true;  } catch (Exception ex) {   ex.printStackTrace();   return false;  }    }注:sFullName是保存路径,in是获取的BLOB库文件如:in = rs.getBinaryStream("content");//一个完整的插入BLOB文件的测试类import java.io.BufferedInputStream;   import java.io.FileInputStream;   import java.io.PrintStream;   import java.sql.Connection;   import java.sql.DriverManager;   import java.sql.ResultSet;import java.sql.SQLException;  import java.sql.Statement;public class Test {    private Connection conn;                   public Connection getConnection() {            try {                Class.forName("oracle.jdbc.driver.OracleDriver");                conn = DriverManager.getConnection(                        "jdbc:oracle:thin:@localhost:1521:cxh", "cxh", "cxh");            } catch (ClassNotFoundException e) {                // TODO Auto-generated catch block                e.printStackTrace();            } catch (SQLException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            return conn;        }     public int insertImage(String path) throws Exception {            int i = 0;            Statement st = null;            ResultSet rs = null;            conn=this.getConnection();                        conn.setAutoCommit(false);//设置数据库为不自动提交,必须的一步            st = conn.createStatement();            //先插入一个空对象,这里我调用了Empty_BLOB()函数            i = st.executeUpdate("insert into zzy_tb_mmscontent (contentid, content) values (seq_zzy_tb_mmscontent.Nextval,Empty_BLOB())");            //以行的方式锁定            rs = st.executeQuery("select content from zzy_tb_mmscontent where contentid=(select max(contentid) from zzy_tb_mmscontent) for update");            if (rs.next()) {                //得到流                oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(1);                //从得到的低级流构造一个高级流                PrintStream ps = new PrintStream(blob.getBinaryOutputStream());                BufferedInputStream bis = new BufferedInputStream(                        new FileInputStream(path));                byte[] buff = new byte[1024];                int n = 0;                //从输入到输出                while ((n = bis.read(buff)) != -1) {                    ps.write(buff, 0, n);                    }                //清空流的缓存                ps.flush();                //关闭流,注意一定要关                ps.close();                bis.close();            }            rs.close();            st.close();            conn.close();            return i;        }            public static void main(String[] args) throws Exception {            Test test=new Test();            test.insertImage("e://ni.gif");            System.out.println("OK");                }   }

原创粉丝点击