2013-12-23 201 views
-1

我无法将我的blob转换为缓冲图像,所以我可以使用它。我从使用inputstream上传的数据库中获取blob(jpg图像)。在我的数据库中,它存储为BufferedInputStream,我注意到了。我得到的blob很好,它的一堆奇怪的符号,并说它的JPG,所以图像必须罚款。任何人都可以发现我做错了什么吗?也许我转换它错了?在image = ImageIO.read(new ByteArrayInputStream(data));图像返回null。将BufferedInputStream转换为图像

@GET 
@Path("{id}") 
@Produces("image/*") 
public Response post(@PathParam("id") String id) throws IOException { 
    Connection con = connection(); 
    Blob blob = getPhoto(con); 
    BufferedImage image = null; 
    byte[] data = null; 
    int blobLength = 0; 
    try { 
     blobLength = (int) blob.length(); 
     data = blob.getBytes(1, blobLength); 
     image = ImageIO.read(new ByteArrayInputStream(data)); 
    // ImageIO.write(image, "JPEG", new File("C:/Users/Nicolas/Desktop/image.jpg")); 
    } catch (SQLException e2) { 
     e2.printStackTrace(); 
    } 

    return Response.ok(image).build(); 
} 

我如何写数据库

public void postPhoto(Connection con, InputStream uploadedInputStream){ 

String mySQL = "INSERT INTO photos (photo) values (?)"; 
PreparedStatement pStmt = con.prepareStatement(mySQL); 
pStmt.setBlob(1, uploadedInputStream); 
pStmt.execute(); 
} 

我如何通过验证uploadedInputStream是一个有效的图像文件发送到我的servlet

var fileInput = document.getElementById('file'); 
var file = fileInput.files[0]; 
var formData = new FormData(); 
formData.append("file", file); 

var parameters="first="+firstName+"&last="+lastName+"&file="+file; 

xmlhttp.open("post","http://localhost:8080/restService/api/submitinfo",true); 
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
xmlhttp.send(formData); 
+0

数据是否填充了blob.getBytes()后面的字节? – MGorgon

+0

是的,它是填充字节和几乎相同的字节数量的原始jpg – NightSkyCode

+1

我会先检查文件。将'data'写入File并尝试用某个图像查看器打开它。如果你有有效的jpg文件,试着用'ImageIO.read(new File(...);'来读取它。我认为问题在于数据不是有效的图像。 – MGorgon

回答

3

开始,通过编写出使用ImageIO.write prehaps 。您可以随时使用ImageIO.read读取图像回和写回的ByteArrayInputStream;)

我没有使用H2数据库进行快速测试。

我注意到了一些事情。 Blob#length返回long,而Blob#getBytes需要int,这可能意味着您正在截断字节流。

此外,从H2的文档看来,Blob的内容似乎没有保存在内存中,所以我使用getBinaryStream来代替。

import java.awt.image.BufferedImage; 
import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.IOException; 
import java.sql.Blob; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JScrollPane; 

public class TestImageDatbase { 

    private Connection con; 

    public static void main(String[] args) { 
     new TestImageDatbase(); 
    } 

    public TestImageDatbase() { 
     try { 
      clearDatabase(); 
      saveImage(); 
      loadImage(); 
     } catch (ClassNotFoundException | SQLException | IOException exp) { 
      exp.printStackTrace(); 
     } 
    } 

    protected Connection getConnection() throws ClassNotFoundException, SQLException { 
     Class.forName("org.h2.Driver"); 
     return DriverManager.getConnection("jdbc:h2:d:\\Image", "sa", ""); 
    } 

    protected void clearDatabase() throws IOException, ClassNotFoundException, SQLException { 

     Connection con = null; 
     PreparedStatement stmt = null; 

     try { 

      con = getConnection(); 
      System.out.println("Cleaning database"); 
      stmt = con.prepareStatement("delete from images"); 
      int updated = stmt.executeUpdate(); 
      System.out.println("Updated " + updated + " rows"); 

     } finally { 
      try { 
       stmt.close(); 
      } catch (Exception e) { 
      } 
      try { 
       con.close(); 
      } catch (Exception e) { 
      } 
     } 

    } 

    protected void saveImage() throws IOException, ClassNotFoundException, SQLException { 

     Connection con = null; 
     PreparedStatement stmt = null; 
     ByteArrayOutputStream baos = null; 
     ByteArrayInputStream bais = null; 

     try { 

      baos = new ByteArrayOutputStream(); 

      File source = new File("/path/to/file"); 
      System.out.println("Source size = " + source.length()); 
      BufferedImage img = ImageIO.read(source); 
      ImageIO.write(img, "png", baos); 

      baos.close(); 

      bais = new ByteArrayInputStream(baos.toByteArray()); 

      con = getConnection(); 
      stmt = con.prepareStatement("insert into images (image) values (?)"); 
      stmt.setBinaryStream(1, bais); 
      int updated = stmt.executeUpdate(); 
      System.out.println("Updated " + updated + " rows"); 

     } finally { 
      try { 
       bais.close(); 
      } catch (Exception e) { 
      } 
      try { 
       baos.close(); 
      } catch (Exception e) { 
      } 
      try { 
       stmt.close(); 
      } catch (Exception e) { 
      } 
      try { 
       con.close(); 
      } catch (Exception e) { 
      } 
     } 

    } 

    protected void loadImage() throws IOException, ClassNotFoundException, SQLException { 

     Connection con = null; 
     PreparedStatement stmt = null; 
     ResultSet rs = null; 

     try { 

      con = getConnection(); 
      stmt = con.prepareStatement("select image from images"); 
      rs = stmt.executeQuery(); 

      while (rs.next()) { 

       System.out.println("Getting blob"); 
       Blob blob = rs.getBlob(1); 
       System.out.println("Reading image"); 
       BufferedImage img = ImageIO.read(blob.getBinaryStream()); 
       System.out.println("img = " + img); 
       JOptionPane.showMessageDialog(null, new JScrollPane(new JLabel(new ImageIcon(img)))); 

      } 

     } finally { 
      try { 
       rs.close(); 
      } catch (Exception e) { 
      } 
      try { 
       stmt.close(); 
      } catch (Exception e) { 
      } 
      try { 
       con.close(); 
      } catch (Exception e) { 
      } 
     } 

    } 

} 
+0

谢谢狂!最后有人能够看到我的纠结。试图学习,仍然是新的! – NightSkyCode

+1

自从我必须编写任何严重的SQL已经很长时间了,所以这对我来说是一条学习曲线;) – MadProgrammer

+0

@MadProgrammer您随时准备提供帮助,+ 1提供良好的答案,只要我看到一个摇摆问题I记住你 – SpringLearner