sqlserver数据库存储图片

 1 /**
 2      * 存储图片方法测试
 3      */
 4     public static void pictureStoreSqlserver(){
 5         try {
 6             
 7             FileInputStream fis = null;
 8             File file = new File("C:/Users/v-guoxiao/Desktop/ReferencePicture/image1.jpeg");
 9             System.out.println(file);
10             fis = new FileInputStream(file);
11             
12             PreparedStatement preparedStatement = connection.prepareStatement("Insert into referencepicture (id,bytepic) values (?,?)");
13             preparedStatement.setString(1, "1");
14             preparedStatement.setBinaryStream(2, fis, file.length());
15             preparedStatement.executeUpdate();
16             preparedStatement.close();
17             fis.close();
18         } catch (SQLException e) {
19             // TODO Auto-generated catch block
20             e.printStackTrace();
21         } catch (FileNotFoundException e) {
22             // TODO Auto-generated catch block
23             e.printStackTrace();
24         } catch (IOException e) {
25             // TODO Auto-generated catch block
26             e.printStackTrace();
27         }
28     }

注意:将数据库中对应字段数据类型设置为 image 类型

 1 /**
 2      * 获取数据保存图片文件
 3      * ***************************直接按照路径保存,成功
 4      * @return
 5      * @throws Exception
 6      */
 7     @SuppressWarnings("unused")
 8     public static BufferedImage getImgFromDb() throws Exception {
 9         BufferedImage image = null;
10         try {
11 //            PreparedStatement stmt = connection.prepareStatement("select imagesample from reference_picture_test where registrationnumber='35721665'");
12             PreparedStatement stmt = connection.prepareStatement("select imagesample from reference_mark");
13             ResultSet rs = stmt.executeQuery();
14             while(rs.next()){
15                 image = ImageIO.read(rs.getBinaryStream(1));
16                 if(image != null){
17                     Image big = image.getScaledInstance(310, 270, Image.SCALE_DEFAULT);
18                     BufferedImage biginput = new BufferedImage(310, 270, BufferedImage.TYPE_INT_BGR);
19                     biginput.getGraphics().drawImage(image, 0, 0, 310, 270, null);
20                     ImageIO.write(biginput, "jpg", new File("C:/Users/v-guoxiao/Desktop/ReferencePicture/"+UUID.randomUUID()+".jpg"));
21                 }
22             }
23             rs.close();
24             stmt.close();
25             connection.close();
26         } catch (Exception e) {
27             e.printStackTrace();
28         }
29         return image;
30     }

 

基于base64存储图片,对应字段数据类型设置为 varchar(MAX)

 1 /**
 2 *  存储图片  
 3 *
 4 */
 5 static BASE64Encoder encoder = new sun.misc.BASE64Encoder();  
 6     static BASE64Decoder decoder = new sun.misc.BASE64Decoder();
 7     public static void storeBytePic(){
 8         File file = new File("C:/Users/v-guoxiao/Desktop/ReferencePicture/image1.png");
 9         try {
10             FileInputStream fis = new FileInputStream(file);
11             byte[] bs = new byte[fis.available()];
12             fis.read(bs);
13             fis.close();
14             BASE64Encoder encoders = new BASE64Encoder();
15             String endString = encoders.encode(bs);
16             
17             PreparedStatement preparedStatement = connection.prepareStatement("Insert into refpic (id,bytepic) values (?,?)");
18             preparedStatement.setString(1, "1");
19             preparedStatement.setString(2, endString);
20             preparedStatement.executeUpdate();
21             preparedStatement.close();
22         } catch (IOException e) {
23             // TODO Auto-generated catch block
24             e.printStackTrace();
25         } catch (SQLException e) {
26             // TODO Auto-generated catch block
27             e.printStackTrace();
28         }
29     }
 1 public static void getBase64Pic(){
 2         PreparedStatement stmt;
 3         try {
 4 //            stmt = connection.prepareStatement("select bytepic from refpic");
 5             stmt = connection.prepareStatement("sselect imagesample from reference_mark");
 6             ResultSet rs = stmt.executeQuery();
 7             while(rs.next()){
 8                 String base = rs.getString(1);
 9                 byte[] bs = decoder.decodeBuffer(base);
10                 
11                 for(int i = 0; i < bs.length; i++){
12                     if(bs[i] < 0){
13                         bs[i] += 256;
14                     }
15                 }
16                 FileOutputStream fos = new FileOutputStream("C:/Users/v-guoxiao/Desktop/ReferencePicture/"+UUID.randomUUID()+".jpg");
17                 fos.write(bs);
18                 fos.flush();
19                 fos.close();
20             }
21             
22         } catch (SQLException e) {
23             // TODO Auto-generated catch block
24             e.printStackTrace();
25         } catch (IOException e) {
26             // TODO Auto-generated catch block
27             e.printStackTrace();
28         }
29     }

 

posted @ 2019-09-24 10:56  奋进的小土豆  阅读(10294)  评论(0编辑  收藏  举报