嗨,我建立一个动态的Web项目,其中欢迎页有struts2文件标记现在我想存储指定的文件到MySQL数据库会有人帮助我...如何将图像从浏览器存储到mysql数据库使用struts 2和休眠
在此先感谢。
这是我开发的代码,但它需要文件参数静态意味着手动我指定路径。但它应该采取的路径从Struts的文件标签看到Java类U将得到它..
public class FileUploadACtion
{
public String execute() throws IOException
{
System.out.println("Hibernate save image into database");
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
//save image into database
File file = new File("C:\\mavan-hibernate-image-mysql.gif");
byte[] bFile = new byte[(int) file.length()];
try {
FileInputStream fileInputStream = new FileInputStream(file);
//convert file into array of bytes
fileInputStream.read(bFile);
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
FileUpload tfile = new FileUpload();
avatar.setImage(bFile);
session.save(tfile);
//Get image from database
FileUpload tfile2 = (FileUpload)session.get(FileUpload.class,FileUpload.getAvatarId());
byte[] bAvatar = avatar2.getImage();
try{
FileOutputStream fos = new FileOutputStream("C:\\test.gif");
fos.write(bAvatar);
fos.close();
}catch(Exception e){
e.printStackTrace();
}
session.getTransaction().commit();
}
}
你的问题是不明确的all.add更多细节,使一些人可以帮你。 – 2012-02-02 10:20:00