使用jsf将网络摄像头图像保存到数据库中

时间:2013-04-03 06:55:21

标签: java spring jsf-2.2

这是我的jsf代码,它将从jQuery捕获图像,我可以将该图像交换到图像位置。当我通过浏览来从本地上传图像时,图像将完美地插入到数据库中。当我从网络摄像头捕获图像时,它会插入带有O B **的Blob .....

<h:inputFile id="photo" name="photo" value="#{user.imageFile}" class="view_current_photo_hidden" />
<h:commandButton value="Save" action="#{user.addUser()}"  class="bt_st_01 green_btn">

这是我的UserBean类......

public Blob image;
private Part imageFile;
public String addUser() throws IOException{
image = Hibernate.createBlob(convertInputStreamToByteArray(imageFile.getInputStream()));

......有些东西......

userImage.setImage(getImage());

....添加图像和其他文件的一些东西........

}

public byte[] convertInputStreamToByteArray(InputStream inputStream)
{
     byte[] bytes= null;

     try
     {
         ByteArrayOutputStream bos = new ByteArrayOutputStream();

         byte data[] = new byte[1024];
         int count;

         while ((count = inputStream.read(data)) != -1)
         {
             bos.write(data, 0, count);
         }

         bos.flush();
         bos.close();
         inputStream.close();

         bytes = bos.toByteArray();
     }
     catch (IOException e)
     {
         e.printStackTrace();
     }
     return bytes;
}

我正在尝试将网络摄像头图像作为Blob从上面的代码上传到数据库......

0 个答案:

没有答案
相关问题