利用struts与hibernate做的上传到数据库图片

jsp文件的字段:
<td>设备图片</td>
		  <td><input type="file" name="picture"/> </td>
		 </tr>

在action里面的处理:
FormFile file=ef.getPicture();     //ef是actionForm
		String pictureName=file.getFileName();           //文件名
		 int i = pictureName.indexOf(".");
	        String type = pictureName.substring(i+1,pictureName.length());  // 得到文件的后缀名
	        ev.setPictureName(type);
		int size=file.getFileSize();                     // 得到文件的大小

		   InputStream stream;
		try {
			 stream = file.getInputStream();          //创建文件流
			 byte[] b=new byte[size];              //得到2进制文件
			 stream.read(b);
			 ev.setPicture(b);    //pojo对象                   
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  


hibernate配置文件
<property name="picture" type="byte[]">
            <column name="PICTURE"  />
         </property>

你可能感兴趣的:(java,Hibernate,jsp,struts)